Get the index of an item by value in a redis list

前端 未结 6 1980
臣服心动
臣服心动 2020-12-31 08:32

I have a redis list I have created, I am using it as a queue at the moment that reverses once in a while. My problem is that I would like to be able to get the index of an i

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-31 09:24

    Use sorted sets to implement a queue.

    Add members and use timestamp as score.

    > ZADD queue 1326990501 foo 1326990502 bar 1326990503 baz 1326990504 qux
    (integer) 4
    

    You can return members in FIFO and LIFO order by the use of ZRANGE and ZREVRANGE respectively.

    FIFO:

    > ZRANGE queue 0 0
    "foo"
    

    LIFO:

    > ZREVRANGE queue 0 0
    "qux"
    

    To find the index of a member use ZRANK. ZRANK op is O(log(N))

    > ZRANK queue bar
    (integer) 1
    

提交回复
热议问题