Search code examples
phpsessionredispredis

Prefix only redis session keys


Is there a way to use one Predis client connection throughout an application but only prefix the session keys?

The Predis library only allows a key prefix to be defined by setting the options parameter when instantiating a client object:

$sessionClient = new Predis\Client($config, array('prefix' => 'session-'));

I'm storing more than just sessions in one Redis instance. So to store other values without the prefix I would think this would work:

$client = new Predis\Client($config);

But keys are still being prefixed. I assume Predis is sharing the connection and options across client instances. So how can one prefix just the session keys?


Solution

  • Found a related issue on Github with a response from the author that this won't be supported.

    Predis\Session\SessionHandler accepts a client instance only for the sake of injection, but you shouldn't really reuse that client for anything else... Obviously, for very specific scenarios nothing stops you from writing your own session handler.