Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run custom Redis commands with Symfony

Tags:

redis

symfony

I'm using Symfony RedisAdapter in a project in order to handle cached values, and it's already deeply used inside the project.

Now I would like to add new redis keys which store some numerical counts, which can be updated very often. So I want to use INCR and DECR redis commands in order to do it fast.

But the RedisAdapter don't seem to allow custom redis commands, you can only fetch, check key existence, delete and save keys. Of course I could fetch the count value, increment it in php, then save it again, but it's not very optimized considering that there is already a solution implemented in redis for this.

Is it possible to run custom redis commands, while keeping the redis abstraction layer offered by Symfony?

like image 646
yolenoyer Avatar asked Oct 18 '25 17:10

yolenoyer


1 Answers

Unfortunately Symfony's redis adapter doesn't allow for this functionality, as it's nothing something that would be shared between the rest of symfony's caching adapters. You'd need to access the underlying redis client the adapter is using and use it to make the call you'd like. For instance with predis:

$predis = new Predis\Client(/* Configuration */);

$predis->incr($my_key);

Obviously this is far from ideal as one is now tying their coupling their application to Redis and whatever Redis client is being used.

like image 57
Will Avatar answered Oct 21 '25 15:10

Will