javascript - Redis, sorted set with pagination - Stack Overflow

I currently have the following data structure in Redisclient.hmset('user:' + user.id, 'i

I currently have the following data structure in Redis

client.hmset('user:' + user.id, 'id', user.id, 'user', JSON.stringify(meta));
client.zadd(['user:user_by_created_time', meta.created_time, 'user:' + user.id]);
client.zadd(['user:user_by_age', meta.age, 'user:' + user.id]);

I then when to get the first 10 users sorted by age, when there are more than 10, I should be able to pass an offset that allows me to use pagination.

What I currently have is the following

client.zrangebyscore(['user:user_by_age', '-inf', '+inf'], (err, results) => {
    const multi = client.multi();
    results.forEach(result => {
        multi.hgetall(result);
    });

    multi.exec((err, results) => { ... });
});

I'm a bit stuck on how to continue with this, I know it's possible to sort a list, but I can't figure out how to only get 10 users after a specific offset.

I'm using the Node Redis client:

I currently have the following data structure in Redis

client.hmset('user:' + user.id, 'id', user.id, 'user', JSON.stringify(meta));
client.zadd(['user:user_by_created_time', meta.created_time, 'user:' + user.id]);
client.zadd(['user:user_by_age', meta.age, 'user:' + user.id]);

I then when to get the first 10 users sorted by age, when there are more than 10, I should be able to pass an offset that allows me to use pagination.

What I currently have is the following

client.zrangebyscore(['user:user_by_age', '-inf', '+inf'], (err, results) => {
    const multi = client.multi();
    results.forEach(result => {
        multi.hgetall(result);
    });

    multi.exec((err, results) => { ... });
});

I'm a bit stuck on how to continue with this, I know it's possible to sort a list, but I can't figure out how to only get 10 users after a specific offset.

I'm using the Node Redis client: https://github./NodeRedis/node_redis

Share Improve this question asked Oct 18, 2016 at 1:40 woutr_bewoutr_be 9,73225 gold badges82 silver badges130 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

To paginate with sorted sets use ZRANGE, not ZRANGEBYSCORE. The arguments are the ranks, so to get the first 10 users you use ZRANGE user:user_by_age 0 9, to get the next 10 you use ZRANGE user:user_by_age 10 19, etc.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745140651a4613415.html

相关推荐

  • javascript - Redis, sorted set with pagination - Stack Overflow

    I currently have the following data structure in Redisclient.hmset('user:' + user.id, 'i

    1小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信