Reputation: 8376
I work with a MongoDB connection and have a Redis Elastic Cache at hand. There's a Mongo collection which essentially changes a few times per week and it includes a totalPoints
for which I'd like to sort some or even all of them, this collection has a huge number of records.
So, sorting demands much work to DB, I really can't pretend to order them at a query, so I'd like to have a TOP list.
I would like to get way to push records, either srtings or objects and order them as a bucket of max size n for the top of those.
Upvotes: 4
Views: 4104
Reputation: 742
You can use the pattern described by anitrez here : http://oldblog.antirez.com/post/take-advantage-of-redis-adding-it-to-your-stack.html
Basically just use a sorted set and add the collection values with score as totalPoints.
zadd toplist totalPoints <collectionItemIdentifier>
Then fetch top 100 as
ZREVRANGE toplist 0 99
You can keep the size in check (it should not be required) by periodically using ZREMRANGEBYSCORE
and remove elements whose score is in a certain range.
Check http://redis.io/commands#sorted_set for more information and commands available on sorted set.
Upvotes: 6