Module: Protocol::Redis::Methods::Lists

Defined in:
lib/protocol/redis/methods/lists.rb

Overview

Methods for managing Redis lists.

Instance Method Summary collapse

Instance Method Details

#blpop(*keys, timeout: 0) ⇒ Object

Remove and get the first element in a list, or block until one is available. O(1). See <redis.io/commands/blpop> for more details.



16
17
18
# File 'lib/protocol/redis/methods/lists.rb', line 16

def blpop(*keys, timeout: 0)
  call("BLPOP", *keys, timeout)
end

#brpop(*keys, timeout: 0) ⇒ Object

Remove and get the last element in a list, or block until one is available. O(1). See <redis.io/commands/brpop> for more details.



24
25
26
# File 'lib/protocol/redis/methods/lists.rb', line 24

def brpop(*keys, timeout: 0)
  call("BRPOP", *keys, timeout)
end

#brpoplpush(source, destination, timeout) ⇒ Object

Pop an element from a list, push it to another list and return it; or block until one is available. O(1). See <redis.io/commands/brpoplpush> for more details.



33
34
35
# File 'lib/protocol/redis/methods/lists.rb', line 33

def brpoplpush(source, destination, timeout)
  call("BRPOPLPUSH", source, destination, timeout)
end

#lindex(key, index) ⇒ Object

Get an element from a list by its index. O(N) where N is the number of elements to traverse to get to the element at index. This makes asking for the first or the last element of the list O(1). See <redis.io/commands/lindex> for more details.



41
42
43
# File 'lib/protocol/redis/methods/lists.rb', line 41

def lindex(key, index)
  call("LINDEX", key, index)
end

#linsert(key, position = :before, index, value) ⇒ Object

Insert an element before or after another element in a list. O(N) where N is the number of elements to traverse before seeing the value pivot. This means that inserting somewhere on the left end on the list (head) can be considered O(1) and inserting somewhere on the right end (tail) is O(N). See <redis.io/commands/linsert> for more details.



51
52
53
54
55
56
57
58
59
# File 'lib/protocol/redis/methods/lists.rb', line 51

def linsert(key, position=:before, index, value)
  if position == :before
    offset = "BEFORE"
  else
    offset = "AFTER"
  end
  
  call("LINSERT", key, offset, index, value)
end

#llen(key) ⇒ Object

Get the length of a list. O(1). See <redis.io/commands/llen> for more details.



64
65
66
# File 'lib/protocol/redis/methods/lists.rb', line 64

def llen(key)
  call("LLEN", key)
end

#lpop(key) ⇒ Object

Remove and get the first element in a list. O(1). See <redis.io/commands/lpop> for more details.



71
72
73
# File 'lib/protocol/redis/methods/lists.rb', line 71

def lpop(key)
  call("LPOP", key)
end

#lpush(key, value, *values) ⇒ Object

Prepend one or multiple elements to a list. O(1) for each element added, so O(N) to add N elements when the command is called with multiple arguments. See <redis.io/commands/lpush> for more details.



79
80
81
82
83
84
85
86
87
88
# File 'lib/protocol/redis/methods/lists.rb', line 79

def lpush(key, value, *values)
  case value
  when Array
    values = value
  else
    values = [value] + values
  end
  
  call("LPUSH", key, *values)
end

#lpushx(key, value) ⇒ Object

Prepend an element to a list, only if the list exists. O(1) for each element added, so O(N) to add N elements when the command is called with multiple arguments. See <redis.io/commands/lpushx> for more details.



94
95
96
# File 'lib/protocol/redis/methods/lists.rb', line 94

def lpushx(key, value)
  call("LPUSHX", key, value)
end

#lrange(key, start, stop) ⇒ Object

Get a range of elements from a list. O(S+N) where S is the distance of start offset from HEAD for small lists, from nearest end (HEAD or TAIL) for large lists; and N is the number of elements in the specified range. See <redis.io/commands/lrange> for more details.



103
104
105
# File 'lib/protocol/redis/methods/lists.rb', line 103

def lrange(key, start, stop)
  call("LRANGE", key, start, stop)
end

#lrem(key, count, value) ⇒ Object

Remove elements from a list. O(N+M) where N is the length of the list and M is the number of elements removed. See <redis.io/commands/lrem> for more details.



112
113
114
# File 'lib/protocol/redis/methods/lists.rb', line 112

def lrem(key, count, value)
  call("LREM", key, count, value)
end

#lset(key, index, values) ⇒ Object

Set the value of an element in a list by its index. O(N) where N is the length of the list. Setting either the first or the last element of the list is O(1). See <redis.io/commands/lset> for more details.



121
122
123
# File 'lib/protocol/redis/methods/lists.rb', line 121

def lset(key, index, values)
  call("LSET", key, index, values)
end

#ltrim(key, start, stop) ⇒ Object

Trim a list to the specified range. O(N) where N is the number of elements to be removed by the operation. See <redis.io/commands/ltrim> for more details.



130
131
132
# File 'lib/protocol/redis/methods/lists.rb', line 130

def ltrim(key, start, stop)
  call("LTRIM", key, start, stop)
end

#rpop(key) ⇒ Object

Remove and get the last element in a list. O(1). See <redis.io/commands/rpop> for more details.



137
138
139
# File 'lib/protocol/redis/methods/lists.rb', line 137

def rpop(key)
  call("RPOP", key)
end

#rpoplpush(source, destination = nil) ⇒ Object

Remove the last element in a list, prepend it to another list and return it. O(1). See <redis.io/commands/rpoplpush> for more details.



145
146
147
148
149
# File 'lib/protocol/redis/methods/lists.rb', line 145

def rpoplpush(source, destination=nil)
  destination = source if destination.nil?
  
  call("RPOPLPUSH", source, destination)
end

#rpush(key, value, *values) ⇒ Object

Append one or multiple elements to a list. O(1) for each element added, so O(N) to add N elements when the command is called with multiple arguments. See <redis.io/commands/rpush> for more details.



155
156
157
158
159
160
161
162
163
164
# File 'lib/protocol/redis/methods/lists.rb', line 155

def rpush(key, value, *values)
  case value
  when Array
    values = value
  else
    values = [value] + values
  end
  
  call("RPUSH", key, *values)
end

#rpushx(key, value) ⇒ Object

Append an element to a list, only if the list exists. O(1) for each element added, so O(N) to add N elements when the command is called with multiple arguments. See <redis.io/commands/rpushx> for more details.



170
171
172
# File 'lib/protocol/redis/methods/lists.rb', line 170

def rpushx(key, value)
  call("RPUSHX", key, value)
end