Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expire gelocations on redis

I am using geo-support on Redis.

Adding new geolocations this way:

"GEOADD" "report-geo-set" "30.52439985197" "50.56539003041" "john"

I want to expire john key from report-geo-set after X hours.

Any suggestions doing that?

Thank you, ray.

like image 802
rayman Avatar asked Jan 12 '16 10:01

rayman


People also ask

How do I set Redis to expire?

To create a Redis with an expiration time, use the SET command and the EX option to set the expiration time. The EX option takes a number in seconds and sets the number of seconds the key is valid until expiration. You can also use PX to specify the expiration time in Milliseconds.

Does Redis data expire?

Normally Redis keys are created without an associated time to live. The key will simply live forever, unless it is removed by the user in an explicit way, for instance using the DEL command.

What is the default expiry of Redis?

There is no default TTL. By default, keys are set to live forever.

What technique does Redis use to compute a value for longitude latitude pairs?

How does it work? The way the sorted set is populated is using a technique called Geohash. Latitude and Longitude bits are interleaved to form a unique 52-bit integer.


1 Answers

Not possible with built-in commands. Keep in mind that geo-support based on zset and your question is look`s like "How to use TTL for individual keys in ZSET".

You may use something like that:

  1. Add "john" to additional special timeout ZSET with time() + X hours score.
  2. From time to time run script/worker which get all obsolete keys from timeout zset and execute ZREM for your "john" key.

Example of given suggestion. Add items:

MULTI
GEOADD report-geo-set 30.52439985197 50.56539003041 john
ZADD geo-timeout 1452600528 john //1452600528 is unix time stamp current + X hours 
EXEC

Clean up script called from time to time (with 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;
like image 99
Nick Bondarenko Avatar answered Oct 18 '22 06:10

Nick Bondarenko