Redis Expiration based on value

2019-08-30 01:50发布

问题:

I have an application which stores key values in Redis, the key is an 8 bytes Long and the value is a list of Integers. my challenge is to finding a way to expire old values for a given key. the worst thing I can do is to add a timestamp to every value and evict them in the client side. Is there a clean way to do this at server side or use another solution instead of Redis?

Update:

we can do this by lua.

get items:

local currenttime=redis.call('time')[1]
local indexes = redis.call("smembers", KEYS[1])
local values=''
for k,v in pairs(indexes) do
    local value, unixtime=string.match(v, "(.*)%:(.*)")
    if(tonumber(unixtime)+60>tonumber(currenttime)) then
        values=value..values..':'
    else
         redis.call("SREM",k,v)
    end
end
return values

add items:

local time=redis.call('time')
local r=redis.call("sadd", KEYS[1],KEYS[2]..":"..time[1])
return r
标签: java redis lua