laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

Redis queue prefix #848

Open Snake231088 opened 6 years ago

Snake231088 commented 6 years ago

Laravel Version: 5.5 PHP Version: 7.1 Description:

add possibility to specify a prefix for queues with redis driver. Now the key on redis is: queues:#QUEUE_NAME# Thx

dstensnes commented 6 years ago

+1

dstensnes commented 6 years ago

Never mind. Fixed it with this in config/database.php:

    'redis' => [
        'cluster' => false,
        'client'  => 'phpredis',
        'default' => [
            'host'     => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port'     => env('REDIS_PORT', 6379),
            'database' => 0,
            'prefix'   => 'cc5home3:',
        ],
    ],

This "redis" key (shown above) should be in the outermost array directly following the "return" statement. This requires the php-redis dpkg to be installed though, but that is probably fine

Snake231088 commented 6 years ago

+1

mfn commented 6 years ago

The proposed fix in https://github.com/laravel/internals/issues/848#issuecomment-339942437 matches the official docs at https://laravel.com/docs/5.5/redis

PhpRedis supports the following additional connection parameters: persistent, prefix, read_timeout and timeout.

IMHO there's nothing else to be done => what the user wanted to talk about is supported.

f3cp commented 6 years ago

The config example by @dstensnes for adding the prefix only works with the phpredis client.

If you want to add a prefix using the predis client the config is different. You must add the prefix within the options key of the redis config e.g.

'redis' => [
        'client' => 'predis',
        'cluster' => false,
        'options'=>[
            'prefix' => 'YOUR_PREFIX_HERE'
        ],
        'default' => [
            'host'     => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port'     => env('REDIS_PORT', 6379),
            'database' => 0,
        ],
    ],
gerardnll commented 6 years ago

@f3cp this adds the prefix to all redis keys before the one specified in cache.php, so it's like a double prefix in sessions and cache databases.

I really don't understand why sessions and cache have prefix (only listed in cache.php) but the queue service doesn't...

crazyfree commented 6 years ago

I also wanna separate cache prefix and session prefix out, using predis. Any solution?

erlangparasu commented 6 years ago

+1

sisve commented 6 years ago

We can set the session prefix separately by extending the SessionManager and through some slightly unclean code interact with the store. You would also need to extend the SessionServiceProvider to use this Much Cooler (tm) implementation. Change your config/app.php to use your CustomSessionServiceProvider instead of original one. The following example is based on Laravel 5.5, but I presume it would be very similar in newer releases too.

class CustomSessionManager extends SessionManager {
    protected function createCacheHandler($driver) {
        $handler = parent::createCacheHandler($driver);

        $repository = $handler->getCache();
        $store = $repository->getStore();

        if ($store instanceof RedisStore) {
            $store->setPrefix('prefix-goes-here');
        }

        return $handler;
    }
}

class CustomSessionServiceProvider extends SessionServiceProvider {
    protected function registerSessionManager() {
        $this->app->singleton('session', function ($app) {
            return new CustomSessionManager($app);
        });
    }
}
paras-malhotra commented 6 years ago

+1 for queue Redis prefix. We definitely need this for servers running multiple Laravel apps with queues

atrauzzi commented 5 years ago

It would be nice to be able to enforce a prefix at the queue level rather than the connection level.