Michael Joseph Aubry
Michael Joseph Aubry

Reputation: 13462

Redis sort by likes inside list of hashes?

Sorry if my terminology is wrong, but I have a list of feed hashes.

So ie feed:1, feed:2, feed:3 inside those hashes I have some keys and values. ie inside feed:1 I have likes:300.

I have a list called feeds:fid which lists all the feed ids. So if I want to grab all the feeds I can just do a method like this in my node.js

module.getObjects = function(keys, callback) {
    helpers.multiKeys(redisClient, 'hgetall', keys, callback);
};

I am not sure how I can sort them so I get all feed items sorted by most liked? Ideally I just want to get the "hottest feed" items.

I am curious how I can go about this in redis?

Upvotes: 0

Views: 489

Answers (1)

DhruvPathak
DhruvPathak

Reputation: 43245

This would be difficult to do in your current set of things. You can however use a single sorted set to store likes along with feed ids. So,whenever a like happens, you store the like in your hash, but also do an ZINCRBY operation on the same feed key in the sorted set.

-- At any point of time the sorted set will contain the feed ids as keys, and number of likes on the key as the score.

-- To get top or hottest feeds, you just do a ZREVRANGE operation, which will give you top N items with maximum likes.

-- To keep both your operations atomic, you shall use redis transactions to have data always synched between hash and sorted set.

Upvotes: 1

Related Questions