class Redis::List

Class representing a Redis list. Instances of Redis::List are designed to behave as much like Ruby arrays as possible.

Public Instance Methods

<<(value) click to toggle source

Works like push. Can chain together: list << ‘a’ << ‘b’

# File lib/redis/list.rb, line 10
def <<(value)
  push(value) # marshal in push()
  self  # for << 'a' << 'b'
end
==(x) click to toggle source
# File lib/redis/list.rb, line 152
def ==(x)
  values == x
end
[](index, length=nil) click to toggle source

Same functionality as Ruby arrays. If a single number is given, return just the element at that index using Redis: LINDEX. Otherwise, return a range of values using Redis: LRANGE.

# File lib/redis/list.rb, line 89
def [](index, length=nil)
  if index.is_a? Range
    range(index.first, index.max)
  elsif length
    case length <=> 0
    when 1  then range(index, index + length - 1)
    when 0  then []
    when -1 then nil  # Ruby does this (a bit weird)
    end
  else
    at(index)
  end
end
Also aliased as: slice
[]=(index, value) click to toggle source

Same functionality as Ruby arrays.

# File lib/redis/list.rb, line 105
def []=(index, value)
  allow_expiration do
    redis.lset(key, index, marshal(value))
  end
end
at(index) click to toggle source

Return the value at the given index. Can also use familiar list syntax. Redis: LINDEX

# File lib/redis/list.rb, line 127
def at(index)
  unmarshal redis.lindex(key, index)
end
delete(name, count=0) click to toggle source

Delete the element(s) from the list that match name. If count is specified, only the first-N (if positive) or last-N (if negative) will be removed. Use .del to completely delete the entire key. Redis: LREM

# File lib/redis/list.rb, line 115
def delete(name, count=0)
  redis.lrem(key, count, marshal(name))  # weird api
end
empty?() click to toggle source

Returns true if there are no elements in the list. Redis: LLEN == 0

# File lib/redis/list.rb, line 148
def empty?
  length == 0
end
first() click to toggle source

Return the first element in the list. Redis: LINDEX(0)

# File lib/redis/list.rb, line 132
def first
  at(0)
end
get()
Alias for: values
insert(where,pivot,value) click to toggle source

Add a member before or after pivot in the list. Redis: LINSERT

# File lib/redis/list.rb, line 16
def insert(where,pivot,value)
  allow_expiration do
    redis.linsert(key,where,marshal(pivot),marshal(value))
  end
end
last() click to toggle source

Return the last element in the list. Redis: LINDEX(-1)

# File lib/redis/list.rb, line 137
def last
  at(-1)
end
length() click to toggle source

Return the length of the list. Aliased as size. Redis: LLEN

# File lib/redis/list.rb, line 142
def length
  redis.llen(key)
end
Also aliased as: size
pop(n=nil) click to toggle source

Remove a member from the end of the list. Redis: RPOP

# File lib/redis/list.rb, line 32
def pop(n=nil)
  if n
    result, = redis.multi do
      redis.lrange(key, -n, -1)
      redis.ltrim(key, 0, -n - 1)
    end
    unmarshal result
  else
    unmarshal redis.rpop(key)
  end
end
push(*values) click to toggle source

Add a member to the end of the list. Redis: RPUSH

# File lib/redis/list.rb, line 23
def push(*values)
  allow_expiration do
    count = redis.rpush(key, values.map{|v| marshal(v) })
    redis.ltrim(key, -options[:maxlength], -1) if options[:maxlength]
    count
  end
end
range(start_index, end_index) click to toggle source

Return a range of values from start_index to end_index. Can also use the familiar list Ruby syntax. Redis: LRANGE

# File lib/redis/list.rb, line 121
def range(start_index, end_index)
  redis.lrange(key, start_index, end_index).map{|v| unmarshal(v) }
end
rpoplpush(destination) click to toggle source

Atomically pops a value from one list, pushes to another and returns the value. Destination can be a String or a Redis::List

list.rpoplpush(destination)

Returns the popped/pushed value.

Redis: RPOPLPUSH

# File lib/redis/list.rb, line 52
def rpoplpush(destination)
  unmarshal redis.rpoplpush(key, destination.is_a?(Redis::List) ? destination.key : destination.to_s)
end
shift(n=nil) click to toggle source

Remove a member from the start of the list. Redis: LPOP

# File lib/redis/list.rb, line 66
def shift(n=nil)
  if n
    result, = redis.multi do
      redis.lrange(key, 0, n - 1)
      redis.ltrim(key, n, -1)
    end
    unmarshal result
  else
    unmarshal redis.lpop(key)
  end
end
size()
Alias for: length
slice(index, length=nil)
Alias for: []
to_s() click to toggle source
# File lib/redis/list.rb, line 156
def to_s
  values.join(', ')
end
unshift(*values) click to toggle source

Add a member to the start of the list. Redis: LPUSH

# File lib/redis/list.rb, line 57
def unshift(*values)
  allow_expiration do
    count = redis.lpush(key, values.map{|v| marshal(v) })
    redis.ltrim(key, 0, options[:maxlength] - 1) if options[:maxlength]
    count
  end
end
value()
Alias for: values
values() click to toggle source

Return all values in the list. Redis: LRANGE(0,-1)

# File lib/redis/list.rb, line 79
def values
  vals = range(0, -1)
  vals.nil? ? [] : vals
end
Also aliased as: get, value