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

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

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).

Parameters:

  • key (Key)
  • timeout (Integer) (defaults to: 0)

See Also:



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

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).

Parameters:

  • key (Key)
  • timeout (Integer) (defaults to: 0)

See Also:



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

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).

Parameters:

  • source (Key)
  • destination (Key)
  • timeout (Integer)

See Also:



49
50
51
# File 'lib/protocol/redis/methods/lists.rb', line 49

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).

Parameters:

  • key (Key)
  • index (Integer)

See Also:



57
58
59
# File 'lib/protocol/redis/methods/lists.rb', line 57

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).

Parameters:

  • key (Key)
  • where (Enum)
  • pivot (String)
  • element (String)

See Also:



67
68
69
70
71
72
73
74
75
# File 'lib/protocol/redis/methods/lists.rb', line 67

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).

Parameters:

  • key (Key)

See Also:



80
81
82
# File 'lib/protocol/redis/methods/lists.rb', line 80

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

#lpop(key) ⇒ Object

Remove and get the first element in a list. O(1).

Parameters:

  • key (Key)

See Also:



87
88
89
# File 'lib/protocol/redis/methods/lists.rb', line 87

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.

Parameters:

  • key (Key)
  • element (String)

See Also:



95
96
97
98
99
100
101
102
103
104
# File 'lib/protocol/redis/methods/lists.rb', line 95

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.

Parameters:

  • key (Key)
  • element (String)

See Also:



110
111
112
# File 'lib/protocol/redis/methods/lists.rb', line 110

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.

Parameters:

  • key (Key)
  • start (Integer)
  • stop (Integer)

See Also:



119
120
121
# File 'lib/protocol/redis/methods/lists.rb', line 119

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.

Parameters:

  • key (Key)
  • count (Integer)
  • element (String)

See Also:



128
129
130
# File 'lib/protocol/redis/methods/lists.rb', line 128

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).

Parameters:

  • key (Key)
  • index (Integer)
  • element (String)

See Also:



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

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.

Parameters:

  • key (Key)
  • start (Integer)
  • stop (Integer)

See Also:



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

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).

Parameters:

  • key (Key)

See Also:



153
154
155
# File 'lib/protocol/redis/methods/lists.rb', line 153

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).

Parameters:

  • source (Key)
  • destination (Key) (defaults to: nil)

See Also:



161
162
163
164
165
# File 'lib/protocol/redis/methods/lists.rb', line 161

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.

Parameters:

  • key (Key)
  • element (String)

See Also:



171
172
173
174
175
176
177
178
179
180
# File 'lib/protocol/redis/methods/lists.rb', line 171

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.

Parameters:

  • key (Key)
  • element (String)

See Also:



186
187
188
# File 'lib/protocol/redis/methods/lists.rb', line 186

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