How can I use different Redis connection with Laravel and phpredis, however, I followed the laravel documentation using-multiple-redis-connections but whenever I call this connection it always return the Redis default connection.
here's my Redis array in the database.php config file
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
'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'),
],
'redisCustomConnection' => [
'url' => env('REDIS_CUSTOM_URL'),
'host' => env('REDIS_CUSTOM_HOST', '127.0.0.1'),
'password' => env('REDIS_CUSTOM_PASSWORD', null),
'port' => env('REDIS_CUSTOM_HOST', '6379'),
'database' => env('REDIS_CUSTOM_DB', '0'),
],
],
here's .env
file
REDIS_CUSTOM_URL=mycustom value
REDIS_CUSTOM_HOST=mycustom value
REDIS_CUSTOM_PASSWORD=mycustom value
REDIS_CUSTOM_HOST=mycustom value
REDIS_CUSTOM_DB=mycustom value
But it did not work it always return the default connection when I use
Redis::connection('redisCustomConnection');
#output
Illuminate\Redis\Connections\PhpRedisConnection {#3315
#connector: Closure() {#3316
class: "Illuminate\Redis\Connectors\PhpRedisConnector"
this: Illuminate\Redis\Connectors\PhpRedisConnector {#3317 …}
use: {
$config: array:4 [
"host" => "127.0.0.1"
"password" => null
"port" => "6379"
"database" => "0"
]
$options: array:3 [
"cluster" => "redis"
"prefix" => "buraq_database_"
"parameters" => []
]
}
file: "./vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php"
line: "26 to 30"
}
#config: array:4 [
"host" => "127.0.0.1"
"password" => null
"port" => "6379"
"database" => "0"
]
#client: Redis {#3314
isConnected: true
host: "127.0.0.1"
port: 6379
auth: null
mode: ATOMIC
dbNum: 0
timeout: 0.0
lastError: null
persistentId: null
options: {
TCP_KEEPALIVE: 0
READ_TIMEOUT: 0.0
COMPRESSION: NONE
SERIALIZER: NONE
PREFIX: "buraq_database_"
SCAN: NORETRY
}
}
#name: "redisCustomConnection"
#events: null
#supportsPacking: null
#supportsLzf: null
#supportsZstd: null
}
My Answer:
[
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'cache' => [
'host' => env('REDIS_CACHE_HOST', '127.0.0.1'),
'password' => env('REDIS_CACHE_PASSWORD', null),
'port' => env('REDIS_CACHE_PORT', 6379),
'database' => 1,
],
'session' => [
'host' => env('REDIS_SESSION_HOST', '127.0.0.1'),
'password' => env('REDIS_SESSION_PASSWORD', null),
'port' => env('REDIS_SESSION_PORT', 6379),
'database' => 2,
],
]
In your code, you can specify which Redis connection to use by passing the connection name to the Redis facade or using the Redis::connection() method. Here's an example:
php
// Using the Redis facade
$value = Redis::connection('cache')->get('key');
// Using the Redis::connection() method
$redis = Redis::connection('session');
$value = $redis->get('key');
Make sure to update your .env file with the appropriate values for the Redis connections:
REDIS_CACHE_HOST=127.0.0.1
REDIS_CACHE_PASSWORD=null
REDIS_CACHE_PORT=6379
REDIS_SESSION_HOST=127.0.0.1
REDIS_SESSION_PASSWORD=null
REDIS_SESSION_PORT=6379
By specifying the connection name when using the Redis facade or the Redis::connection() method, you can access different Redis connections in your Laravel application.
Rate this post
3 of 5 based on 3054 votesComments