Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

berseck's avatar

Redis prefix

Hello guys,

I have been testing a new way to do prefix for Sessions and Cache using REDIS I tried creating new driver and many different other functionalities... and all defaulted to the same issue.

\Illuminate\Cache\CacheManager

    protected function getPrefix(array $config)
    {
        return Arr::get($config, 'prefix') ?: $this->app['config']['cache.prefix'];
    }

All I needed actually is this to not get the cache prefix from the config/cache.php prefix variable... But actually get it from the connection since it says it actually supports but it's not supporting this at all.

'redis' => [

        'client' => 'predis',

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
       'prefix' => ''
        ],

        'session' => [
            'host'     => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port'     => env('REDIS_PORT', 6379),
            'prefix' => 'redis_session',
            'database' => 1,
        ]
    ],

The prefix on those connections is never been called or set. Anywhere for the connection...

Any ideas how I can force to set those?

Thanks,

0 likes
4 replies
denvit's avatar

You can get config variable like

$prefix = config('cache.stores.redis.session.prefix');
berseck's avatar

Hey @denvit

To do that I would need to actually change the Cache manager or overwrite the function, with I don't wanna do right now.

Do you know any way that I can set this prefix in a provider... or middleware or any other method in order to keep the main code intact?

Thanks,

berseck's avatar

Well...

If anyone is interested...

I found a hack, since I could not set any other driver to properly work with redis.

I did this:

'apc' => [
    'driver' => 'redis',
    'connection' => 'session',
    'prefix' => 'my_session',
],

'redis' => [
    'driver' => 'redis',
    'connection' => 'default',
    'prefix' => 'redis',
],

APC is default from laravel... I just changed the default configuration for the redis one.

Resulting basically in what I want like:

1) "redis:3818661:index"
2) "my_session:d5WHxYXQxFXqpoZnrq3BHR325N4k2fw5mVqT1mQK"
3) "redis:3818661:shopping-index"

O know it's wrong... but well it worked out by now

j__candre's avatar

The simplest way i've found to add a prefix to my sessions is to create a new cache store in config/cache.php

    'sessions' => [
        'driver' => 'redis',
        'connection' => 'cache',
        'lock_connection' => 'default',
        // override cache prefix below
        'prefix' => env('SESSION_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_') . '_session_'),
    ],

    'apc' => [
        'driver' => 'apc',
    ],

After which you can set SESSION_STORE='sessions' in your .env (or update the store in config/session.php).

key prefixes in Redis will look something like this:

"laravel_database_laravel_cache_:tag:enquiryBedroomCount:entries" "laravel_database_laravel_cache_:e7cf66797159dc3cd3e85f72e15bb551:timer" "laravel_database_laravel_session_:4ZyGKqAyzWSF9fwcy9mkSCZkmEpLtckDXTGyOmUo"

Please or to participate in this conversation.