Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove element at specific index from redis list

Tags:

ruby

redis

Is it possible to remove an element at a specific index in an redis list ? Not really finding what I want. There is trim which allows you to select an specific set of elements, LREM allows you to delete an item in a list by value, but I dont have the value.

I found an hack where you use LSET to change the value of the element to a UID or string e.g. 'DELETED', and the you call LREM on this value. This just feels a bit dirty though.

like image 330
Donovan Thomson Avatar asked Jul 23 '15 07:07

Donovan Thomson


People also ask

How do I remove a specific value from a list index?

You can use the pop() method to remove specific elements of a list. pop() method takes the index value as a parameter and removes the element at the specified index. Therefore, a[2] contains 3 and pop() removes and returns the same as output.

How do I empty a list in Redis?

If the end index is greater than length of the list, redis treats it as equal to last index. This is why ltrim mylist 0, -(n +1) clear the list. It does so because (-n) is equivalent to index 0. Adding 1 to it leaves no element within that range since that will be before the first element.

What is LPOP in Redis?

Removes and returns the first elements of the list stored at key . By default, the command pops a single element from the beginning of the list. When provided with the optional count argument, the reply will consist of up to count elements, depending on the list's length.

How do I create a list in Redis?

Creating Lists Redis reads lists from left to right, and you can add new list elements to the head of a list (the “left” end) with the lpush command or the tail (the “right” end) with rpush . You can also use lpush or rpush to create a new list: lpush key value.


1 Answers

So the only way to accomplish what I wanted was to set the value at the index to a pre determined string and then do an removal by value.

see discussion here https://groups.google.com/forum/#!topic/redis-db/c-IpJ0YWa9I

in ruby as follows

@redis.lset("#{@namespace}/#{specified_queue}", index, "DELETED")
@redis.lrem("#{@namespace}/#{specified_queue}", 1, "DELETED")

LSET docs http://redis.io/commands/lset LREM docs http://redis.io/commands/lrem

like image 172
Donovan Thomson Avatar answered Sep 24 '22 07:09

Donovan Thomson