Use PHP memcache in yii2 - php

In yii2 I need to use memcache plugin I've installed this also will be shown in phpinfo but I get the following error when running the project:
Invalid Configuration – yii\base\InvalidConfigException - MemCache requires PHP memcache extension to be loaded.
Cache configuration
'cache' => [
'class' => 'yii\caching\MemCache',
'servers' => [
[
'host' => '127.0.0.1',
'port' => 11211,
'weight' => 60,
],
],
]

Maybe you have got memcached instead of memcache. Configuration in this case should be:
'cache' => [
'class' => 'yii\caching\MemCache',
'useMemcached' => true, // <--- here
'servers' => [
[
'host' => '127.0.0.1',
'port' => 11211,
'weight' => 60,
],
],
],

Related

Fatal error: Trait 'Laminas\Db\Adapter\Laminas\Db\Adapter\AdapterInterface' not found in /var/www/vendor/laminas/laminas-db/src/Adapter/Adapter.php

I keep getting this error when I'm trying to open Album application from Laminas MVC tutorial. I use multicontainer configuration in Docker which consists of linked containers. These are laminas-mvc-tutorial container and mysql database container. PDO mysql is enabled but I think it is something with my Adapter configuration issues.
Here is global.php config array:
use Laminas\Db\Adapter;
return [
'service_manager' => [
'abstract_factories' => [
Adapter\AdapterAbstractServiceFactory::class
],
'factories' => [
Adapter\AdapterInterface::class => Adapter\AdapterServiceFactory::class,
],
'aliases' => [
Adapter\Adapter::class => Adapter\AdapterInterface::class
]
],
'db' => [
'driver' => 'Pdo',
'adapters' => [
mysqlAdapter::class => [
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=Laminas;host=localhost;charset=utf8',
'username' => 'root',
'port' => '3306',
'password' => 'pass1234',
'driver_options' => [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
],
],
],
],
];
As far as I am aware. All of this:
'abstract_factories' => [
Adapter\AdapterAbstractServiceFactory::class
],
'factories' => [
Adapter\AdapterInterface::class => Adapter\AdapterServiceFactory::class,
],
'aliases' => [
Adapter\Adapter::class => Adapter\AdapterInterface::class
]
Is not needed. If you are following the docs why are you not in development mode instead of production? Since in development mode this should be read from the local.php file instead of global.php?
'db' => [
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=your_dbname;host=localhost;charset=utf8',
'username' => 'your_username',
'password' => 'your_password',
],
Which is literally the only thing needed to connect to MySQL for laminas/laminas-db
The error you get:
'Laminas\Db\Adapter\Laminas\Db\Adapter\AdapterInterface' not found
I think there is a namespace issue.
Try to use only:
use Laminas\Db;
insted of the one you're currently using.

Laravel Redis - artisan cache:clear - Connection refused [unix:/path/.redis/redis.sock]

 I have configured Redis (using a socket) in the Laravel in my hosting server. Everything works fine (I have tested reading from cache, sessions etc.), I have one database for a cache and a second one for users sessions.
 However, when I run "php artisan cache:clear" it shows the error:
"In AbstractConnection.php line 155: Connection refused [unix:/path/.redis/redis.sock]".
This error also occures when I run any command which uses Redis, for example "php73 artisan cron:updateForeignPrices".
.env
CACHE_DRIVER=redis
SESSION_DRIVER=redis
REDIS_HOST=/path/.redis/redis.sock
REDIS_PASSWORD=null
REDIS_PORT=0
REDIS_CACHE_DB=0
REDIS_SESSION_DB=1
config/database.php
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'cluster' => true,
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'),
'prefix' => Str::slug(env('APP_NAME'), '_').'_',
'parameters' => ['password' => env('REDIS_PASSWORD', null)],
],
'default' => [
'scheme' => 'unix',
'path' => env('REDIS_HOST'),
'host' => env('REDIS_HOST'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT'),
'database' => env('REDIS_CACHE_DB', 0)
],
'cache' => [
'scheme' => 'unix',
'path' => env('REDIS_HOST'),
'host' => env('REDIS_HOST'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT'),
'database' => env('REDIS_CACHE_DB', 0),
],
'session' => [
'scheme' => 'unix',
'path' => env('REDIS_HOST'),
'host' => env('REDIS_HOST'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT'),
'database' => env('REDIS_SESSION_DB', 1),
]
]
Hosting provider's info about Redis (translated):
Socket: /path-to-my-directory/.redis/redis.sock
User and password: (none)
Port: 0
RAM: 128 MB
Instruction on WordPress Litespeed:
In the „Host” field paste address from the panel, for example: /home/klient.dhosting.pl/dhtutorial/.redis/redis.sock
In the „Port” field remove a default value and type "0".
Leave "user" and "password" empty.
 It seems like everything works correctly in a direct use of Redis, but not via console. Anyone has an idea how to fix it?
Thanks in advance, I have searched whole Internet.
REDIS_HOST should point to the address where the Redis server is hosted whether it's hosted on a local machine or cloud service. somethings like below:
REDIS_HOST=12.0.0.1
REDIS_PASSWORD=password
REDIS_PORT=6379
set REDIS_HOST=127.0.0.1 or your host address
Try to use the following configuration.
.env
CACHE_DRIVER=redis
SESSION_DRIVER=redis
REDIS_SCHEME=unix
REDIS_PATH=/path/.redis/redis.sock
REDIS_CACHE_DB=0
REDIS_SESSION_DB=1
config.database.php
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'cluster' => true,
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'),
'prefix' => Str::slug(env('APP_NAME'), '_').'_',
'parameters' => ['password' => null],
],
'default' => [
'scheme' => env('REDIS_SCHEME'),
'path' => env('REDIS_PATH'),
'database' => env('REDIS_CACHE_DB', 0)
],
'cache' => [
'scheme' => env('REDIS_SCHEME'),
'path' => env('REDIS_PATH'),
'database' => env('REDIS_CACHE_DB', 0),
],
'session' => [
'scheme' => env('REDIS_SCHEME'),
'path' => env('REDIS_PATH'),
'database' => env('REDIS_SESSION_DB', 1),
]
]

Predis with laravel 5.5 "No connections available in the pool in Aggregate/RedisCluster.php:337 "

I've setup redis-cluster on Amazon ElastiCache for cache, using Laravel 5.5 and Predis package, I get the following error.
Predis\ClientException: No connections available in the pool in vendor/predis/predis/src/Connection/Aggregate/RedisCluster.php:337
Trace:
Predis\ClientException: No connections available in the pool in vendor/predis/predis/src/Connection/Aggregate/RedisCluster.php:337
Stack trace:
0 vendor/predis/predis/src/Connection/Aggregate/RedisCluster.php(411):
Predis\Connection\Aggregate\RedisCluster->guessNode(153)
1 vendor/predis/predis/src/Connection/Aggregate/RedisCluster.php(388):
Predis\Connection\Aggregate\RedisCluster->getConnectionBySlot(153)
2 vendor/predis/predis/src/Connection/Aggregate/RedisCluster.php(550):
Predis\Connection\Aggregate\RedisCluster->getConnection(Object(Predis\Command\StringSetExpire))
3 vendor/predis/predis/src/Connection/Aggregate/RedisCluster.php(593):
Predis\Connection\Aggregate\RedisCluster->retryCommandOnFailure(Object(Predis\Command\StringSetExpire),
'executeCommand')
4 vendor/predis/predis/src/Client.php(331): Predis\Connection\Aggregate\RedisCluster->executeCommand(Object(Predis\Command\StringSetExpire))
5 vendor/predis/predis/src/Client.php(314): Predis\Client->executeCommand(Object(Predis\Command\StringSetExpire))
6 vendor/laravel/framework/src/Illuminate/Redis/Connections/Connection.php(96):
Predis\Client->__call('setex', Array)
7 vendor/laravel/framework/src/Illuminate/Redis/Connections/Connection.php(108):
Illuminate\Redis\Connections\Connection->command('setex', Array)
8 vendor/laravel/framework/src/Illuminate/Cache/RedisStore.php(93): Illuminate\Redis\Connections\Connection->__call('setex', Array)
9 vendor/laravel/framework/src/Illuminate/Cache/Repository.php(195): Illuminate\Cache\RedisStore->put('5rr44TBjIPEgJSx...',
'a:1:{s:6:"_flas...', 480)
10 vendor/laravel/framework/src/Illuminate/Session/CacheBasedSessionHandler.php(66):
Illuminate\Cache\Repository->put('5rr44TBjIPEgJSx...',
'a:1:{s:6:"_flas...', 480)
11 vendor/laravel/framework/src/Illuminate/Session/Store.php(128): Illuminate\Session\CacheBasedSessionHandler->write('5rr44TBjIPEgJSx...',
'a:1:{s:6:"_flas...')
12 vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(87):
Illuminate\Session\Store->save()
13 vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(218):
Illuminate\Session\Middleware\StartSession->terminate(Object(Illuminate\Http\Request),
Object(Illuminate\Http\Response))
14 vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(189):
Illuminate\Foundation\Http\Kernel->terminateMiddleware(Object(Illuminate\Http\Request),
Object(Illuminate\Http\Response))
15 public/index.php(58): Illuminate\Foundation\Http\Kernel->terminate(Object(Illuminate\Http\Request),
Object(Illuminate\Http\Response))
16 {main}
Technical stack I used:
PHP7.0
Laravel5.5
OS (Debian GNU/Linux 9.6 (stretch)), Release 9.6
I tried the following configs in config/database.php:
redis=> [
'client' => 'predis',
'options' => [
'cluster' => 'redis',
],
'clusters' => [
'default' => [
[
'host' => env('REDIS_CLUSTER_HOST', 'localhost'),
'password' => env('REDIS_CLUSTER_PASSWORD', null),
'port' => env('REDIS_CLUSTER_PORT', 6379),
'database' => 0,
],
],
'cache' => [
[
'host' => env('REDIS_CLUSTER_HOST', 'localhost'),
'password' => env('REDIS_CLUSTER_PASSWORD', null),
'port' => env('REDIS_CLUSTER_PORT', 6379),
'database' => 0,
],
],
]
]
I also tried with timeout => 0|5|60 but every time I get the same error.
Any help would be highly appreciated!
This is working in in our env:
'redis' => [
'cluster' => true,
'client' => 'predis',
'options' => [
'cluster' => 'redis',
'parameters' => [
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'timeout' => 15,
],
],
'clusters' => [
'default' => [
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'timeout' => 15,
],
],
],
Based on info found here: https://github.com/nrk/predis/issues/480 - but needed to also replicate all 'default' config values in the options -> parameters group
REDIS_HOST, is pointing to the configuration endpoint in AWS elasticache's redis cluster.
I think the problem is that I'm trying to switch amz cloud service by service... ANd redis can only be accessed through the vpc which I'm not in this case.
I need to test is from an ec2 inside the vpc for testing.

How to use memcached and apc together in laravel?

I want to use both memcached and apc at the same time for caching, how can I configure and use it in laravel.
By using the Cache facade you can specify what cache type you want to use.
Cache::store('memcached')->put('bar', 'baz', 10); // Using memcached
Cache::store('apc')->put('bar', 'baz', 10); // Using apc
As you can see in your app/config/cache.php there is already some preconfigured cache types set up:
'stores' => [
'apc' => [
'driver' => 'apc',
],
'array' => [
'driver' => 'array',
],
'database' => [
'driver' => 'database',
'table' => 'cache',
'connection' => null,
],
'file' => [
'driver' => 'file',
'path' => storage_path('framework/cache'),
],
'memcached' => [
'driver' => 'memcached',
'servers' => [
[
'host' => env('MEMCACHED_HOST', '127.0.0.1'),
'port' => env('MEMCACHED_PORT', 11211),
'weight' => 100,
],
],
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
],
You now need to make sure, memcached and APC are correctly installed on your system.
Using the Memcached cache requires the Memcached PECL package to be installed.
Using APC cache requires the APC package on your system

How to save session in redis with cakephp 3.0?

Hello guy's I newbie in cakephp 3.0. I know little bit cakephp 2.0. I configured my redis with cakephp 2.0 with this help of url But I don't know how to configure in cakephp 3.0 please help me
Since Cakephp 4 is out, I am currently studying it (which seems exists in Cakphp 3 as well) and seems like there is a way to do it. Not tested yet.
Following this: https://book.cakephp.org/4/en/development/sessions.html#cache-sessions
app.php
'Session' => [
'defaults' => 'cache', //Use the Cache class to save session
'handler' => [
'config' => 'session'
]
],
Following this link: https://book.cakephp.org/4/en/core-libraries/caching.html#redisengine-options
app.php
/*
* Configure the cache adapters.
*/
'Cache' => [
'session' => [
'className' => RedisEngine::class,
//`port` The port your Redis server is running on.
//`host` The host your Redis server is running on.
//`database` The database number to use for connection.
//`password` Redis server password.
//`persistent` Should a persistent connection be made to Redis.
//`timeout` Connection timeout for Redis.
//`unix_socket` Path to a unix socket for Redist.
],
],
I am going to test this out later on and make update if needed, but it really seems promising.
Update 2020-05-20: Tested, it work fine
You would set your session to use cache sessions inside the app.php file:
'Session' => [
'defaults' => 'cache'
]
Then, you would set your cache to redis:
'Cache' => [
'default' => [
'className' => 'Redis',
],
]
In vendor\cakephp\cakephp\src\Network\Session.php
you can see the default type of session.
They are listed as php, cake,cache , database.
$defaults = [
'php' => [
'cookie' => 'CAKEPHP',
'ini' => [
'session.use_trans_sid' => 0,
]
],
'cake' => [
'cookie' => 'CAKEPHP',
'ini' => [
'session.use_trans_sid' => 0,
'session.serialize_handler' => 'php',
'session.use_cookies' => 1,
'session.save_path' => TMP . 'sessions',
'session.save_handler' => 'files'
]
],
'cache' => [
'cookie' => 'CAKEPHP',
'ini' => [
'session.use_trans_sid' => 0,
'session.use_cookies' => 1,
'session.save_handler' => 'user',
],
'handler' => [
'engine' => 'CacheSession',
'config' => 'default'
]
],
'database' => [
'cookie' => 'CAKEPHP',
'ini' => [
'session.use_trans_sid' => 0,
'session.use_cookies' => 1,
'session.save_handler' => 'user',
'session.serialize_handler' => 'php',
],
'handler' => [
'engine' => 'DatabaseSession'
]
]
]
Here is cache using default config of Cache. Maybe you want to use defualt as a File Cache.
In app.php
create a new cache config
'redis' => [
'className' => 'Redis',
'server'=>'127.0.0.1',
'port'=>6379
],
Then you can use your new cache config here.
'Session' => [
'cookie' => 'herewego',
'ini' => [
'session.use_trans_sid' => 0,
'session.use_cookies' => 1,
'session.save_handler' => 'user',
],
'handler' => [
'engine' => 'CacheSession',
'config' => 'redis'
]
],

Categories