Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert value by index in Redis list

Tags:

redis

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

LINSERT can do it by value, but that seems a little backward in that the same value could appear multiple times in the list.

like image 313
Brad Avatar asked Feb 11 '14 03:02

Brad


1 Answers

There is a work around which may be a little slow, since all are O(n) operations.

  • Use LIndex to save old value at client.
  • Use LSet to set a tag value which would never inserted in the list on the index.
  • Use LINSERT to insert 2 values(new value, old saved value) after the tag value.
  • Use LRem to delete the tag value.
  • All operations should be in a transaction.
like image 134
yinqiwen Avatar answered Sep 20 '22 16:09

yinqiwen