最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Redis, sorted set with pagination - Stack Overflow

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论