Kapila
Kapila

Reputation: 87

Expiry time for member in Redis

I'm using Jedis client to store geo coordinates in Redis.

Is there any way to set expiry time for member in Redis? I know, I can set the expiry time for a key.

For example, I added below three coordinates and now I want to expire "Bahn" member in 10 secs.

redis.geoadd(key, 8.6638775, 49.5282537, "Weinheim"); 
redis.geoadd(key, 8.3796281, 48.9978127, "EFS9");
redis.geoadd(key, 8.665351, 49.553302, "Bahn"); 

Upvotes: 1

Views: 1061

Answers (1)

Pascal Le Merrer
Pascal Le Merrer

Reputation: 5981

Behind the scene, GEOADD uses a ZSET to store its data.

You can store the same data (without geolocation) in a second ZSET, with a unix timestamp as score this time, using a regular ZADD command.

ZADD expirationzset <expiration date> <data> 

You can get the expired data from this second ZSET, using

ZRANGEBYSCORE expirationzset -inf <current unix timestamp>

Then you have to remove them from both ZSETs, using ZREM for the geolocation ZSET, and ZREMRANGEBYSCORE for the expiration zset:

ZREM geolocationzset <expired_data1> <expired_data3> <expired_data3>...
ZREMRANGEBYSCORE expirationzset -inf <current unix timestamp>

Upvotes: 4

Related Questions