Class: Redis::List

Inherits:
BaseObject show all
Includes:
Enumerable, Helpers::CoreCommands, Helpers::Serialize
Defined in:
lib/redis/list.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Serialize

#from_redis, #to_redis

Methods included from Helpers::CoreCommands

#exists?, #expire, #expireat, #move, #persist, #rename, #renamenx, #sort, #ttl, #type

Methods inherited from BaseObject

#initialize, #redis

Constructor Details

This class inherits a constructor from Redis::BaseObject

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



16
17
18
# File 'lib/redis/list.rb', line 16

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/redis/list.rb', line 16

def options
  @options
end

Instance Method Details

#<<(value) ⇒ Object

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



19
20
21
22
# File 'lib/redis/list.rb', line 19

def <<(value)
  push(value)
  self  # for << 'a' << 'b'
end

#==(x) ⇒ Object



141
142
143
# File 'lib/redis/list.rb', line 141

def ==(x)
  values == x
end

#[](index, length = nil) ⇒ Object Also known as: slice

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.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/redis/list.rb', line 73

def [](index, length=nil)
  if index.is_a? Range
    last = index.exclude_end? ? (index.last - 1) : index.last
    range(index.first, last)
  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

#[]=(index, value) ⇒ Object

Same functionality as Ruby arrays.



90
91
92
# File 'lib/redis/list.rb', line 90

def []=(index, value)
  redis.lset(key, index, to_redis(value))
end

#at(index) ⇒ Object

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



116
117
118
# File 'lib/redis/list.rb', line 116

def at(index)
  from_redis redis.lindex(key, index)
end

#delete(name, count = 0) ⇒ Object

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



98
99
100
# File 'lib/redis/list.rb', line 98

def delete(name, count=0)
  redis.lrem(key, count, to_redis(name))  # weird api
end

#each(&block) ⇒ Object

Iterate through each member of the set. Redis::Objects mixes in Enumerable, so you can also use familiar methods like collect, detect, and so forth.



104
105
106
# File 'lib/redis/list.rb', line 104

def each(&block)
  values.each(&block)
end

#empty?Boolean

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

Returns:

  • (Boolean)


137
138
139
# File 'lib/redis/list.rb', line 137

def empty?
  length == 0
end

#firstObject

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



121
122
123
# File 'lib/redis/list.rb', line 121

def first
  at(0)
end

#insert(where, pivot, value) ⇒ Object

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



25
26
27
# File 'lib/redis/list.rb', line 25

def insert(where,pivot,value)
  redis.linsert(key,where,to_redis(pivot),to_redis(value))
end

#lastObject

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



126
127
128
# File 'lib/redis/list.rb', line 126

def last
  at(-1)
end

#lengthObject Also known as: size

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



131
132
133
# File 'lib/redis/list.rb', line 131

def length
  redis.llen(key)
end

#popObject

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



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

def pop
  from_redis redis.rpop(key)
end

#push(*values) ⇒ Object

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



30
31
32
33
# File 'lib/redis/list.rb', line 30

def push(*values)
  redis.rpush(key, values.map{|v| to_redis(v) })
  redis.ltrim(key, -options[:maxlength], -1) if options[:maxlength]
end

#range(start_index, end_index) ⇒ Object

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



110
111
112
# File 'lib/redis/list.rb', line 110

def range(start_index, end_index)
  from_redis redis.lrange(key, start_index, end_index)
end

#rpoplpush(destination) ⇒ Object

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



48
49
50
# File 'lib/redis/list.rb', line 48

def rpoplpush(destination)
  from_redis redis.rpoplpush(key, destination.is_a?(Redis::List) ? destination.key : destination.to_s)
end

#shiftObject

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



59
60
61
# File 'lib/redis/list.rb', line 59

def shift
  from_redis redis.lpop(key)
end

#to_sObject



145
146
147
# File 'lib/redis/list.rb', line 145

def to_s
  values.join(', ')
end

#unshift(*values) ⇒ Object

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



53
54
55
56
# File 'lib/redis/list.rb', line 53

def unshift(*values)
  redis.lpush(key, values.map{|v| to_redis(v) })
  redis.ltrim(key, 0, options[:maxlength] - 1) if options[:maxlength]
end

#valuesObject Also known as: get

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



64
65
66
67
# File 'lib/redis/list.rb', line 64

def values
  v = from_redis range(0, -1)
  v.nil? ? [] : v
end