I am working on replacing Redis with KeyDB in my application, in order to utilise the KeyDB EXPIREMEMBER feature. However, this method is not included with PHPRedis.
Is there a KeyDB drop-in replacement for PHPRedis that adds this? Alternatively, is there a means in PHPRedis to invoke this, despite it not having the function built in?
I was not able to find any ready-built PHP client library with the KeyDB 'EXPIREMEMBER' function implemented, so I ended up using Redis' support for Lua scripting to solve this instead. Simple (hahaha) example below:
$redis = new Redis();
$redis->connect('127.0.0.1', 6379, 1);
// Add a Sorted Set member that we want to expire
$redis->zAdd('key', 1, 'val1');
// Prepare our Lua script to expire the member added above
$script = "return redis.call('EXPIREMEMBER', KEYS[1], ARGV[1], ARGV[2])";
$sha = sha1($script);
// Assume script is already cached in Redis server, but load it if first attempt fails
$result = $redis->evalSha($sha, ['key', 'val1', 60]);
if ($result === false) {
$redis->script('load', $script);
$result = $redis->evalSha($sha, ['key', 'val1', 60]);
}
Note that with Redis 7, use of functions instead of ephemeral scripts would be preferable, but this support is not yet available with PHPRedis (though it is already added to PRedis).
More information on using scripts here: https://github.com/phpredis/phpredis#script and here: https://redis.io/docs/manual/programmability/