Home  >  Q&A  >  body text

redis的list类型做分页索引的排序问题

我做了一个list来做id的索引,一个hash存储具体的数据

list

key value
lists app_id:12
lists app_id:13
lists app_id:14

hash

key field value
app_id:12 app_name 天气预报
app_sort 1
app_id:13 app_name 游戏推荐
app_sort 3
app_id:14 app_name 新闻评论
app_sort 2

我之前是lrange操作list获得相关的app_id,然后去hash使用getall获得具体的数据
现在加了个排序的功能,暂时没有相关思路,不知道怎么处理???

PHP中文网PHP中文网2738 days ago646

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-22 09:02:34

    php$apple_ids = $redis->ZREVRANGE('lists', 0, -1);
    if(empty($apple_ids)){
        return array();
    }
    
    $_         = $redis->HMGET('hash', $apple_ids);
    
    
    

    reply
    0
  • 迷茫

    迷茫2017-04-22 09:02:34

    nosql is essentially different from relational databases,
    Hash is the original data,
    Your list is equivalent to a clustered index,
    If you want to sort, you must traverse the data, then sort in memory, and then output. In fact, mysql does this too,
    But why is mysql fast?
    Because there is an index,
    So just add the index,
    Create a new key, type is zset or list,
    zset is more convenient, the list must be refreshed regularly,
    First get the id from zset or list, and then get the data from the hash.
    Of course, you can also directly convert the data into json and store it in zset or list.

    reply
    0
  • Cancelreply