Reputation: 6499
I need to have a list of recent visitors at website (authorized users that opened a page in last N minutes). To do it I implemented a code that handles all page calls and sends a pair (user_id, timestamp) to the storage. I don't want to update database table for it each time, so I want to use a cache for it. I can store python dictionary in cache as one object, fetch and update it, but it is not very efficient. I tried to look at Redis data structures, from one side Hash looks good (user_id -> timestamp) but looks like I can't use Redis efficiently to fetch all uids based on range of timestamp. So I'll need to fetch all keys and values, iterate on keys and check related values. Also looks like there is no command in Redis to remove multiple keys from hash. Is it possible to handle such data structure using Redis build-in structures? Thanks!
Upvotes: 0
Views: 359
Reputation: 50052
Instead of Hashes, look into using Sorted Sets.
Keep your ids as the set's members and use the timestamp (epoch) as score. Retrieve recent visitors based on timestamp with Z[REV]RANGEBYSCORE
and delete old visitors with ZREMBYSCORE
.
Upvotes: 1