如何从PHP调用KeyDB的EXPIREMEMBER方法

huangapple go评论74阅读模式
英文:

How to call KeyDB's EXPIREMEMBER method from PHP

问题

我正在替换我的应用程序中的Redis,以利用KeyDB的EXPIREMEMBER功能。然而,PHPRedis中没有包括这种方法。

是否有用于PHPRedis的KeyDB替代方案,可以添加这个功能?或者,是否有办法在PHPRedis中调用这个功能,即使它没有内置的函数?

英文:

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?

答案1

得分: 1

无法找到具有KeyDB 'EXPIREMEMBER'函数的现成PHP客户端库,因此我最终使用了Redis对Lua脚本的支持来解决这个问题。以下是一个简单(哈哈哈)的示例:

$redis = new Redis();
$redis->connect('127.0.0.1', 6379, 1);

// 添加一个要过期的有序集合成员
$redis->zAdd('key', 1, 'val1');

// 准备Lua脚本以过期上面添加的成员
$script = "return redis.call('EXPIREMEMBER', KEYS[1], ARGV[1], ARGV[2])";
$sha = sha1($script);

// 假设脚本已经缓存在Redis服务器中,但如果第一次尝试失败,则加载它
$result = $redis->evalSha($sha, ['key', 'val1', 60]);
if ($result === false) {
    $redis->script('load', $script);
    $result = $redis->evalSha($sha, ['key', 'val1', 60]);
}

请注意,使用Redis 7时,使用函数而不是短暂的脚本更可取,但在PHPRedis中尚不可用(尽管已经添加到PRedis中)。

有关使用脚本的更多信息,请参阅以下链接:https://github.com/phpredis/phpredis#script 和 https://redis.io/docs/manual/programmability/

英文:

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/

huangapple
  • 本文由 发表于 2023年3月4日 08:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定