Redis Facade cannot get session data - laravel

I set the session driver to redis, but I don't get session data from Redis Facade.
This is config/session.php
'driver' => env('SESSION_DRIVER', 'file'),
'connection' => env('SESSION_CONNECTION'),
This is .env
SESSION_DRIVER=redis
SESSION_CONNECTION="session-connection"
This is config/database.php
'redis' => [
'session-connection' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
'prefix' => 'session:',
],
],
These are codes which run in the php artisan tinker:
>>> session(['key'=>'value']);
=> null
>>> session('key');
=> "value"
>>> use Illuminate\Support\Facades\Redis;
>>> Redis::connection('session-connection')->get('key')
=> null
>>> Redis::connection('session-connection')->keys('*')
=> []
But Redis Facade can get Cache data, and I don't know why session cannot.
These are settings related to the cache.
This is config/cache.php
'default' => env('CACHE_DRIVER', 'file'),
'stores' => [
'redis' => [
'driver' => 'redis',
'connection' => 'cache-connection',
'lock_connection' => 'cache-lock',
],
],
//'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache_'),
This is .env
CACHE_DRIVER=redis
This is config/database.php
'redis' => [
'cache-connection' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
'prefix' => 'cache:',
],
'cache-lock' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => '1',
'prefix' => 'cache-lock:',
],
],
These are codes which run in the php artisan tinker:
>>> Cache::set('cachekey',1);
=> true
>>> Cache::get('cachekey');
=> "1"
>>> use Illuminate\Support\Facades\Redis;
>>> Redis::connection('cache-connection')->get('cachekey')
=> "1"
>>> Redis::connection('cache-connection')->keys('*')
=> [
"cache:cachekey",
]
Any ideas? Thank you!

Related

Laravel redis cluster prefix doesn't works

I have redis cluster config, in laravel 5.8, with one node and two shards.
I need to add prefix for session and cache, but now I have all keys without prefix.
Tis is my laravel database config:
'redis' => [
'client' => 'predis',//I also tried phpredis, same problem
'cluster' => env('REDIS_CLUSTER_ENABLED', true),
'clusters' => [
'default' => [
[
'host' => 'node-endpoint-1',
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
'options' => [
'prefix' => 'help_session:',
]
],
[
'host' => 'node-endpoint-2',
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
'options' => [
'prefix' => 'help_session:',
]
],
],
'cache' => [
[
'host' => 'node-endpoint-1',
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
'options' => [
'prefix' => 'cache:',
]
],
[
'host' => 'node-endpoint-2',
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
'options' => [
'prefix' => 'cache:',
]
]
],
],
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
],
],
I cannot find a different configuration. Is it possible?
Thank you

Class 'Redis' not found in Laravel 7

Here i am having issue with importing Redis class. I've already imported this class
use Illuminate\Support\Facades\Redis;.
Redis config in .env file
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_CLIENT=predis
inside config/database.php
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
],
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
],
],
These are all the configuration i used but also i am getting the same issue of "Class 'Redis' not found, A class import is missing".
To use the predis Redis client you must install composer require predis/predis.
The other, more performant option, is to use the phpredis client which requires the redis PHP extension. https://github.com/phpredis/phpredis/blob/develop/INSTALL.markdown

No connections available in the pool, Laravel predis

I have the error "No connections available in the pool" under laravel 7, php7.4 and redis server on AWZ elasticache.
Here my .env :
REDIS_CLIENT=predis
REDIS_SCHEME=tls
REDIS_HOST=clustercfg.xxx.xxx.xxx.cache.amazonaws.com
REDIS_PASSWORD=xxx
REDIS_PORT=6379
my config/database.php :
'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,
]
],
'cache' => [
[
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
]
],
],
],
clustercfg.xxx.xxx.xxx.cache.amazonaws.com is my configuration endpoint and REDIS_PASSWORD my AUTH token
I have basically copied the solution of Predis with laravel 5.5 "No connections available in the pool in Aggregate/RedisCluster.php:337 "
but problem is not resolved.
I have looked at the vpc security group in amazon and basically all ports are opened :

Import databases infos from mysql in database.php [duplicate]

This question already has answers here:
Laravel: connect to databases dynamically
(8 answers)
Closed 3 years ago.
I would like to get databases informations stored in my database and add it in the database.php.
I tried to get all infos in the database but I always have an error like this :
[2019-04-18 20:22:20] laravel.ERROR: Call to a member function
connection() on null {"exception":"[object]
(Symfony\Component\Debug\Exception\FatalThrowableError(code: 0):
Call to a member function connection() on null at
/var/www/vhosts/xxxx.net/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1249)
I added this on the top of database.php :
<?php
use App\Campaign;
$database_infos = Campaign::all();
$campains_db = array();
foreach ($database_infos as $info) {
if (isset($info->db_name)) {
$campains_db[$info->keyword] = array(
'driver' => 'mysql',
'host' => $info->db_host,
'database' => $info->db_name,
'username' => $info->db_user,
'password' => decrypt($info->db_password),
'prefix' => $info->db_prefix,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'strict' => false,
'engine' => null,
);
}
}
return [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
$campains_db,
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'wordpress' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
],
'migrations' => 'migrations',
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'),
],
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
];
I found this on an other post :
The simplest solution is to set your database config at runtime.
Laravel might expect these settings to be loaded from the
config/database.php file, but that doesn't mean you can't set or
change them later on.
The config loaded from config/database.php is stored as database in
Laravel config. Meaning, the connections array inside
config/database.php is stored at database.connections.
Config::set("database.connections.mysql", [
"host" => "...",
"database" => "...",
"username" => "...",
"password" => "..."
]);

What does the setting queue at Laravel 5.5 config/queue.php do?

What does the 'queue' => 'default' setting do at config/queue.php?
It seems like it does not affect anything.
In config/queue.php my queue configuration is ga-fetcher-queue.
This is my config:
return [
'default' => 'redis',
'connections' => [
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => 'ga-fetcher-queue',
'retry_after' => 360,
],
],
'failed' => []
];
My redis config at config/database.php is this:
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
]

Resources