Reputation: 18869
I'm currently exploring caching software for my application and considered Redis (geospatial).
My cache should allow data to be queried by both geolocation (items within radius) as well as time (items posted within region AND e.g. for the last hour, or day). I will probably expire cached data after some time like a week or so.
As I understand, for both geolocation and time-based queries it is advised to use a sorted set (GEOADD in case of geolocation).
Structurally I'm not certain how to approach this problem with Redis since it is just a key-value store and secondary indexes don't seems straightforward.
I've been looking on the internet but haven't found an answer.
Is this even possible?
Upvotes: 2
Views: 1045
Reputation: 49942
To do that with Redis you'll indeed need two Sorted Sets - one for storing the geo data and one for storing the timestamp.
This example shows one way to do that:
127.0.0.1:6379> GEOADD locations 0 0 id:1 1 1 id:2 0.1 0.0 id:3
(integer) 3
127.0.0.1:6379> ZADD timestamps 1000 id:1 100 id:2 800 id:3
(integer) 3
127.0.0.1:6379> GEORADIUS locations 0.2 0.0 100 km STORE tmp
(integer) 2
127.0.0.1:6379> ZINTERSTORE tmp 2 tmp timestamps WEIGHTS 0 1 AGGREGATE SUM
(integer) 2
127.0.0.1:6379> ZRANGEBYSCORE tmp 900 1100
1) "id:1"
In this case, expiration should be done periodically by scanning the 'timestamps' Sorted Set for old entries and removing them from all relevant data structures.
Upvotes: 4