如何使Redis上的地理位置过期

ray*_*man 5 java maps geolocation redis

我在Redis上使用地理支持。

通过以下方式添加新的地理位置:

"GEOADD" "report-geo-set" "30.52439985197" "50.56539003041" "john"
Run Code Online (Sandbox Code Playgroud)

我想在X个小时后使john key从report-geo-set过期。

有什么建议吗?

谢谢雷。

mis*_*ion 7

内置命令无法实现。请记住,基于zset的地理支持和您的问题看起来像是“如何对ZSET中的各个键使用TTL”。

您可以使用类似这样的方法:

  1. 将“ john”添加到具有time()+ X小时分数的其他特殊超时ZSET中。
  2. 时不时地运行脚本/工作程序,这些脚本/工作程序将从超时zset中获取所有过时的密钥,并为您的“ john”密钥执行ZREM。

给定建议的示例。添加项目:

MULTI
GEOADD report-geo-set 30.52439985197 50.56539003041 john
ZADD geo-timeout 1452600528 john //1452600528 is unix time stamp current + X hours 
EXEC
Run Code Online (Sandbox Code Playgroud)

清理不时调用的脚本(使用LUA):

local currentTime = redis.call('TIME');
local list = redis.call('ZRANGEBYSCORE', 'geo-timeout', 0, currentTime[0]);
local keysRemoved = 0;
for i, name in ipairs(list) do
    redis.call('ZREM', 'geo-timeout', name);
    redis.call('ZREM', 'report-geo-set', name);
    keysRemoved = keysRemoved + 1;
end
return keysRemoved;
Run Code Online (Sandbox Code Playgroud)