Module: RedisCluster::Function::List

Included in:
RedisCluster::Function
Defined in:
lib/redis_cluster/function/list.rb

Overview

List implement redis lists commands. There will be some adjustment for cluster. see redis.io/commands#list. Most of the code are copied from github.com/redis/redis-rb/blob/master/lib/redis.rb.

SETTER = [:linsert, :lpop, :lpush, :lpushx, :lrem, :lset, :ltrim, :rpop, :rpush, :rpushx] GETTER = [:lindex, :llen, :lrange]

Instance Method Summary collapse

Instance Method Details

#lindex(key, index) ⇒ String

Get an element from a list by its index.

Parameters:

Returns:



79
80
81
# File 'lib/redis_cluster/function/list.rb', line 79

def lindex(key, index)
  call(key, [:lindex, key, index], read: true)
end

#linsert(key, where, pivot, value) ⇒ Fixnum

Insert an element before or after another element in a list.

Parameters:

Returns:

  • (Fixnum)

    length of the list after the insert operation, or ‘-1` when the element `pivot` was not found



91
92
93
# File 'lib/redis_cluster/function/list.rb', line 91

def linsert(key, where, pivot, value)
  call(key, [:linsert, key, where, pivot, value])
end

#llen(key) ⇒ Fixnum

Get the length of a list.

Parameters:

Returns:

  • (Fixnum)


18
19
20
# File 'lib/redis_cluster/function/list.rb', line 18

def llen(key)
  call(key, [:llen, key], read: true)
end

#lpop(key) ⇒ String

Remove and get the first element in a list.

Parameters:

Returns:



62
63
64
# File 'lib/redis_cluster/function/list.rb', line 62

def lpop(key)
  call(key, [:lpop, key])
end

#lpush(key, value) ⇒ Fixnum

Prepend one or more values to a list, creating the list if it doesn’t exist

Parameters:

  • key (String)
  • value (String, Array)

    string value, or array of string values to push

Returns:

  • (Fixnum)

    the length of the list after the push operation



27
28
29
# File 'lib/redis_cluster/function/list.rb', line 27

def lpush(key, value)
  call(key, [:lpush, key, value])
end

#lpushx(key, value) ⇒ Fixnum

Prepend a value to a list, only if the list exists.

Parameters:

Returns:

  • (Fixnum)

    the length of the list after the push operation



36
37
38
# File 'lib/redis_cluster/function/list.rb', line 36

def lpushx(key, value)
  call(key, [:lpushx, key, value])
end

#lrange(key, start, stop) ⇒ Array<String>

Get a range of elements from a list.

Parameters:

  • key (String)
  • start (Fixnum)

    start index

  • stop (Fixnum)

    stop index

Returns:



101
102
103
# File 'lib/redis_cluster/function/list.rb', line 101

def lrange(key, start, stop)
  call(key, [:lrange, key, start, stop], read: true)
end

#lrem(key, count, value) ⇒ Fixnum

Remove elements from a list.

Parameters:

  • key (String)
  • count (Fixnum)

    number of elements to remove. Use a positive value to remove the first ‘count` occurrences of `value`. A negative value to remove the last `count` occurrences of `value`. Or zero, to remove all occurrences of `value` from the list.

  • value (String)

Returns:

  • (Fixnum)

    the number of removed elements



114
115
116
# File 'lib/redis_cluster/function/list.rb', line 114

def lrem(key, count, value)
  call(key, [:lrem, key, count, value])
end

#lset(key, index, value) ⇒ String

Set the value of an element in a list by its index.

Parameters:

Returns:



124
125
126
# File 'lib/redis_cluster/function/list.rb', line 124

def lset(key, index, value)
  call(key, [:lset, key, index, value])
end

#ltrim(key, start, stop) ⇒ String

Trim a list to the specified range.

Parameters:

  • key (String)
  • start (Fixnum)

    start index

  • stop (Fixnum)

    stop index

Returns:



134
135
136
# File 'lib/redis_cluster/function/list.rb', line 134

def ltrim(key, start, stop)
  call(key, [:ltrim, key, start, stop])
end

#rpop(key) ⇒ String

Remove and get the last element in a list.

Parameters:

Returns:



70
71
72
# File 'lib/redis_cluster/function/list.rb', line 70

def rpop(key)
  call(key, [:rpop, key])
end

#rpush(key, value) ⇒ Fixnum

Append one or more values to a list, creating the list if it doesn’t exist

Parameters:

Returns:

  • (Fixnum)

    the length of the list after the push operation



45
46
47
# File 'lib/redis_cluster/function/list.rb', line 45

def rpush(key, value)
  call(key, [:rpush, key, value])
end

#rpushx(key, value) ⇒ Fixnum

Append a value to a list, only if the list exists.

Parameters:

Returns:

  • (Fixnum)

    the length of the list after the push operation



54
55
56
# File 'lib/redis_cluster/function/list.rb', line 54

def rpushx(key, value)
  call(key, [:rpushx, key, value])
end