簡體   English   中英

Redis,分頁排序集

[英]Redis, sorted set with pagination

我目前在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]);

然后,當我要按年齡對前10個用戶進行排序時,如果超過10個,我應該能夠傳遞一個offset ,該offset允許我使用分頁。

我目前擁有的是以下

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

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

我對如何繼續執行此操作有些困惑,我知道可以對列表進行排序,但是我無法弄清楚在特定的偏移量之后如何僅獲得10個用戶。

我正在使用Node Redis客戶端: https : //github.com/NodeRedis/node_redis

要使用排序集進行分頁,請使用ZRANGE ,而不要使用ZRANGEBYSCORE 參數是等級,因此要獲取前10個用戶,請使用ZRANGE user:user_by_age 0 9 ,要獲取后10個ZRANGE user:user_by_age 10 19請使用ZRANGE user:user_by_age 10 19 ,等等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM