I have a single number in redis which I want to increment counts of, however, I want each increment to expire and be removed after a TTL.
Essentially I want the behavior to be like so:
Time 1: Increment 5 (TTL 5), value is 5
Time 2: Increment 3 (TTL 5), value is 8
Time 6: Increment 5 expired, value becomes 3
Is this behavior possible?
2
Answers
GET singleNumber
.SET singleNumber 8 EX 5
whereEX 5
says that it will expire in 5 seconds.SET singleNumber 3 EX 5
.If you don’t want to reset the TTL with each increment, you could use
KEEPTTL
option for theSET
command.I think what you store in redis should be logs of operations. Then compute the final score with logs whose ttls are not reached.
Since you can
I would recommend using a sorted set like below:
Increment
as the main key[increment score]_[timestamp/uuid]
as a member in setFor example:
Suppose the times are:
Get total score
Suppose it is Time 7 now
remove all expired data