Insert index value into Redis list

Is there a way to insert a value into a Redis list by index?

LINSERT can do this by value, but it is a little behind in that the same value can appear several times in the list.

+4
source share
3 answers

There is work that can be a little slow, since all operations are O (n).

  • Use LIndex to save the old value on the client.
  • Use LSet to set a tag value that has never been inserted into a list in an index.
  • Use LINSERT to insert 2 values ​​(new value, old saved value) after the tag value.
  • Use LRem to remove the tag value.
  • All operations must be performed in a transaction.
+1

LSET .

:

redis> RPUSH mylist "foo"
(integer) 1
redis> RPUSH mylist "bar"
(integer) 2
redis> LSET mylist 1 "baz"
OK
redis> LRANGE mylist 0 -1
1) "foo"
2) "baz"
+1

Redis , , , . pushin '' n poppin '.

+1

Source: https://habr.com/ru/post/1526281/


All Articles