I have set up Laravel with Redis as the session driver. I'm having a hard time understanding the difference between calling Session facade and Redis facade. If session is set up to use Redis, then Session::get('test') should return the same output as Redis::get('test'), right?
This is what I have tried with Tinker:
>>> Session::put('test',123);
=> null
>>> Session::get('test');
=> 123
// Why is this not 123?
>>> Redis::get('test');
=> null
// Verify that Session is using Redis
>>> Session::getDefaultDriver();
=> redis
.env file:
REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379
SESSION_DRIVER=redis
SESSION_CONNECTION=session
database.php
'redis' => [
'cluster' => false,
'client' => 'predis',
'default' => [
'host' => env('DATA_REDIS_HOST', env('REDIS_HOST', '127.0.0.1')),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'session' => [
'host' => env('DATA_REDIS_HOST', env('REDIS_HOST', '127.0.0.1')),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 1,
],
],
Related
I would like to use different connection for reading and writing to session and cache.
I am using redis as cache and session storage.
Here are my env configs :
REDIS_HOST=192.168.1.230
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_HOST_READ=192.168.1.13
REDIS_PASSWORD_READ=null
REDIS_PORT_READ=6380
CACHE_DRIVER=redis
SESSION_DRIVER=redis
My cache.php(config/cache.php) modifications :
'stores' => [
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
'redis-read' => [
'driver' => 'redis',
'connection' => 'read',
]
]
My database.php(config/database.php) modifications :
'redis' => [
'client' => 'phpredis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'persistent'=> 1
],
'session' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 1,
'persistent'=> 1
],
'read' => [
'host' => env('REDIS_HOST_READ', '127.0.0.1'),
'password' => env('REDIS_PASSWORD_READ', null),
'port' => env('REDIS_PORT_READ', 6380),
'database' => 0,
'persistent'=> 1
],
],
I am able to read from redis read host as follows :
Cache::driver('redis-read')->get('general_data');
But I don't want to define this every time when I am reading cache/session.
Is there any other way to do this ? I mean whenever I am reading cache/session, it will use "redis-read" and whenever I am writing to cache/session, it will use "redis".
Background
In the past I was able to use a non-clustered redis just fine in my config like so:
'redis' => [
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => 6379,
'database' => 0,
'cluster' => true,
]
],
However due to the load on our redis servers, I have to cluster my redis, This config works fine when the only redis connection I have is clustered (figured it out after a lot of work):
'redis' => [
'client' => 'predis',
'cluster' => true,
'options' => [
'cluster' => 'redis',
'parameters' => [
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_DEFAULT_PORT', 6379),
'database' => 0,
],
],
'clusters' => [
'default' => [
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_1_PORT', 6379),
'database' => 0,
],
'shard2' => [
'host' => env('REDIS_SHARD_2_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_2_PORT', 6379),
'database' => 0,
],
'shard3' => [
'host' => env('REDIS_SHARD_3_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_3_PORT', 6379),
'database' => 0,
],
'options' => [
'cluster' => 'redis'
],
]
]
any my env file looks like this (For my localhost, anyway):
QUEUE_DRIVER=redis // cluster compatible
BROADCAST_DRIVER=redis // cluster compatible
CACHE_CONNECTION=redis // cluster incompatible
REDIS_CLUSTER=true
REDIS_HOST=localhost
REDIS_DEFAULT_PORT=7000
REDIS_SHARD_1_HOST=localhost
REDIS_SHARD_2_HOST=localhost
REDIS_SHARD_3_HOST=localhost
REDIS_SHARD_1_PORT=7000
REDIS_SHARD_2_PORT=7001
REDIS_SHARD_3_PORT=7002
Problem
The fact is that currently, we use the non-clustered redis for the following:
Cache: supports redis clustering
Queue/Jobs: supports redis clustering
Broadcast (ie websockets): does not support redis clustering
That's why we need to have both redis connections simultaneously, so that we can use the clustered connection for caching/queues, and non-clustered connection for websockets.
But this isn't working:
'redis' => [
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => 6379,
'database' => 0,
'cluster' => true,
],
'clustered' => [
'client' => 'predis',
'cluster' => true,
'options' => [
'cluster' => 'redis',
'parameters' => [
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_DEFAULT_PORT', 6379),
'database' => 0,
],
],
'clusters' => [
'default' => [
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_1_PORT', 6379),
'database' => 0,
],
'shard2' => [
'host' => env('REDIS_SHARD_2_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_2_PORT', 6379),
'database' => 0,
],
'shard3' => [
'host' => env('REDIS_SHARD_3_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_3_PORT', 6379),
'database' => 0,
],
'options' => [
'cluster' => 'redis'
],
]
further, some users state that such a task is simply impossible for redis. Is that true?
update
I tried this
in Cache.php
'redis' => [
'driver' => 'redis',
'connection' => 'clustered',
],
note: in the above connection i simply couldn't just copy/paste the cluster options, since it would crash if i didn't put a driver option
In database.php I was inspired by this answer and simply put different connections under the redis key: (ie `database.redis.connection-1, database.redis.connection-2 etc)
'redis' => [
'clustered' => [
// clustered settings copied from above
],
],
'default' => [
// non clustered settings
],
]
To test, I ran the following tinker
>>> use Illuminate\Support\Facades\Cache;
>>> Cache::put('foo','bar',1);
Predis/Response/ServerException with message 'MOVED 7837 127.0.0.1:7001'
The move error is a known one, it's simply saying that i'm dealing with a non-clustered redis connection.
thoughts?
I was able to fix this problem with this PR.
This is what my config looks like now:
'redis' => [
'clustered' => [
'client' => 'predis',
'cluster' => true,
'options' => [ 'cluster' => 'redis' ],
'clusters' => [
[
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_1_PORT', 6379),
'database' => 0,
],
[
'host' => env('REDIS_SHARD_2_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_2_PORT', 6379),
'database' => 0,
],
[
'host' => env('REDIS_SHARD_3_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_3_PORT', 6379),
'database' => 0,
],
],
],
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => 6379,
'database' => 0,
'cluster' => false,
],
]
Will be sending PR to Laravel itself shortly.
I am trying to use ElasticCache Redis Cluster(Cluster mode enabled not sentinel) on Laravel-5.4.32 but getting below error:
1/1) ServerException
MOVED 13491 10.0.1.199:6379
My database.php looks like below:
'redis' => [
'client' => 'predis',
'cluster' => true,
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
REDIS_HOST value is been provided using .env file.
My application works fine with single redis instance.
Below configuration worked for me:
'redis' => [
'client' => 'predis',
'options' => [
'cluster' => 'redis',
],
'clusters' => [
'default' => [
[
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
],
],
Actually, this is clearly mentioned in laravel documentation: https://laravel.com/docs/5.4/redis#configuration
I'm running Laravel in a GlusterFS cluster, and I want to setup a redis cluster too. I'm not able to find out how should I configure Laravel.
This is the example config file:
'redis' => [
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
How can I add multiple servers?
I'm assuming you just add another server there and update the cluster boolean:
'redis' => [
'cluster' => true,
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'secondary' => [
'host' => '127.0.0.1',
'password' => 'p#ssw0rd',
'port' => 6379,
'database' => 0,
],
],
I want to put session and cache data into redis. This is my configuration in database.php:
'redis' => array(
'cluster' => false,
'default' => array(
'host' => '192.168.56.101',
'port' => 6379,
'database' => 0,
),
'session' => array(
'host' => '192.168.56.101',
'port' => 6379,
'database' => 1,
),
),
session.php:
return array(
'driver' => 'redis',
'connection' => 'session',
);
cache.php:
'driver' => 'redis',
However, where I write code like this:
Cache::remember('aa',1,function(){
return 'bb';
});
cache driver uses the same redis database as session driver does, which results in:
127.0.0.1:6379[1]> keys *
1) "aa"
2) "e0606244bec40b0352fb2b7b65d98049e49f6189"
Anyone knows how to force cache to use a specific redis connection? Or I have to mix them up together?
Introduction
Here is my note, for some other guy who running in to this problem, I think this is should be in the docs.
By default, redis gives you 16 separate databases, but laravel out of the box will try to use database 0 for both sessions and cache.
Our solution is to let Redis caching using database 0, and database 1 for Session, there for solving the session clear by running php artisan cache:clear problem.
Note: Tested at Laravel 5.1
1. Setting up Session Redis connection
Modify config/database.php, add session key to the redis option:
'redis' => [
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'session' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 1,
],
],
2. Make use of the session connection
Modify config/session.php, change the following:
'connection' => null,
to:
'connection' => 'session',
3. Using Redis as session driver
Modify .env, change SESSION_DRIVER:
SESSION_DRIVER=redis
4. Testing out
Execute the following artisan command, then check your login state:
php artisan cache:clear
If the login state persists, voilĂ !
Laravel 5.5:
database.php should look like 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,
],
'session' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 1,
],
],
And in the session.php you have to update also the key "connection" to the right key. In this case 'session'
'connection' => 'session',
Laravel 5 now supports this.
https://github.com/laravel/framework/commit/d10a840514d122fa638eb5baa24c8eae4818da3e
You can select redis connection by modifying config/cache.php
'stores' => [
'redis' => [
'driver' => 'redis',
'connection' => 'your-connection-name',
],
],
Laravel 4 CacheManager does not support selecting redis connection.
What you need to do is to modify/extend CacheManager and override createRedisDriver() method.
Modify this line
return $this->repository(new RedisStore($redis, $this->getPrefix()));
To
return $this->repository(
new RedisStore($redis, $this->getPrefix(),
$this->app['config']['cache.redis'])
);
Now you can define your configuration in cache.php
'redis' => 'your-connection-name'