Laravel: how to separate cache and session into different redis database? - php

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'

Related

Laravel Session and Cache Read/Write Driver

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".

Laravel session with Redis

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,
],
],

Connection refused [tcp://127.0.0.1:6379] Laravel Predis

I'm trying to setup Redis client to connect to the Redis server in Laravel 6.0 version, but when I try to change the REDIS_HOST in database.php file in config it gives me an error:
Connection refused 127.0.0.1:6379
This is how my code looks like in routes api.php file
Route::get('test',function(Request $request){
$redis=Redis::connection();
$redis->set("key1","keyValue");
$a=$redis->get("key1");
return response()->json( ['test'=>$a,]);
});
and this is how my database.php file looks like
'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),
],
],
So when i change the REDIS_HOST its gives me error,any idea is my code problem the configurations?
Thanks !
I encountered this on my MacBook and fixed it by installing Redis using homebrew as directed here
PS: I'm working with Laravel 8.40

laravel session on different server

I have 2 servers running same laravel app (laravel 5.3) and both are behind reverse proxy(nginx). for the session I set it to use redis. Also, I use cartalyst sentinel for user authentication.
let say the reverse Proxy is A, while servers for laravel are B and C.
The problem is:
When I log in A which is forwarding the request to B, and then the next request in A is forwarded to C.
When the request is forwarded to C, i can't pass the request because C see me as a guest not as an authenticate user.
how can I share session behind reverse proxy in different server?
config/cache.php
'default' => env('CACHE_DRIVER', 'redis'),
'stores' => [
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
],
config/session.php
'driver' => env('SESSION_DRIVER', 'redis'),
'connection' => 'session',
config/database.php
'redis' => [
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', '192.168.1.102'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'prefix' => env('REDIS_PREFIX', 'prod'),
],
'session' => [
'host' => env('REDIS_HOST', '192.168.1.102'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 1,
'prefix' => env('REDIS_PREFIX', 'prod_session'),
],
update: 11 Oct 2017
I try using laravel default Auth and it's work. from my view point the problem is in cartalyst sentinel that not ready for use in horizontal scaling server.

Configuring AWS ElasticCache redis Cluster-3.2.4 with Laravel-5.4.32 using Predis

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

Categories