Class: Redis::List
- Inherits:
-
BaseObject
- Object
- BaseObject
- Redis::List
- Includes:
- Enumerable, Helpers::CoreCommands
- 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
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#<<(value) ⇒ Object
Works like push.
- #==(x) ⇒ Object
-
#[](index, length = nil) ⇒ Object
(also: #slice)
Same functionality as Ruby arrays.
-
#[]=(index, value) ⇒ Object
Same functionality as Ruby arrays.
- #as_json ⇒ Object
-
#at(index) ⇒ Object
Return the value at the given index.
-
#delete(name, count = 0) ⇒ Object
Delete the element(s) from the list that match name.
-
#each(&block) ⇒ Object
Iterate through each member of the set.
-
#empty? ⇒ Boolean
Returns true if there are no elements in the list.
-
#first ⇒ Object
Return the first element in the list.
-
#insert(where, pivot, value) ⇒ Object
Add a member before or after pivot in the list.
-
#last ⇒ Object
Return the last element in the list.
-
#length ⇒ Object
(also: #size)
Return the length of the list.
-
#pop(n = nil) ⇒ Object
Remove a member from the end of the list.
-
#push(*values) ⇒ Object
Add a member to the end of the list.
-
#range(start_index, end_index) ⇒ Object
Return a range of values from
start_index
toend_index
. -
#rpoplpush(destination) ⇒ Object
Atomically pops a value from one list, pushes to another and returns the value.
-
#shift(n = nil) ⇒ Object
Remove a member from the start of the list.
- #to_s ⇒ Object
-
#unshift(*values) ⇒ Object
Add a member to the start of the list.
-
#values ⇒ Object
(also: #get, #value)
Return all values in the list.
Methods included from Helpers::CoreCommands
#exists?, #expire, #expireat, #marshal, #move, #persist, #rename, #renamenx, #sort, #ttl, #type, #unmarshal
Methods inherited from BaseObject
#allow_expiration, #initialize, #redis, #set_expiration, #to_hash, #to_json
Constructor Details
This class inherits a constructor from Redis::BaseObject
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
14 15 16 |
# File 'lib/redis/list.rb', line 14 def key @key end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/redis/list.rb', line 14 def @options end |
Instance Method Details
#<<(value) ⇒ Object
Works like push. Can chain together: list << ‘a’ << ‘b’
17 18 19 20 |
# File 'lib/redis/list.rb', line 17 def <<(value) push(value) # marshal in push() self # for << 'a' << 'b' end |
#==(x) ⇒ Object
165 166 167 |
# File 'lib/redis/list.rb', line 165 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.
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/redis/list.rb', line 96 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 |
#[]=(index, value) ⇒ Object
Same functionality as Ruby arrays.
112 113 114 115 116 |
# File 'lib/redis/list.rb', line 112 def []=(index, value) allow_expiration do redis.lset(key, index, marshal(value)) end end |
#as_json ⇒ Object
173 174 175 |
# File 'lib/redis/list.rb', line 173 def as_json(*) to_hash end |
#at(index) ⇒ Object
Return the value at the given index. Can also use familiar list syntax. Redis: LINDEX
140 141 142 |
# File 'lib/redis/list.rb', line 140 def at(index) unmarshal 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
122 123 124 |
# File 'lib/redis/list.rb', line 122 def delete(name, count=0) redis.lrem(key, count, marshal(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.
128 129 130 |
# File 'lib/redis/list.rb', line 128 def each(&block) values.each(&block) end |
#empty? ⇒ Boolean
Returns true if there are no elements in the list. Redis: LLEN == 0
161 162 163 |
# File 'lib/redis/list.rb', line 161 def empty? length == 0 end |
#first ⇒ Object
Return the first element in the list. Redis: LINDEX(0)
145 146 147 |
# File 'lib/redis/list.rb', line 145 def first at(0) end |
#insert(where, pivot, value) ⇒ Object
Add a member before or after pivot in the list. Redis: LINSERT
23 24 25 26 27 |
# File 'lib/redis/list.rb', line 23 def insert(where,pivot,value) allow_expiration do redis.linsert(key,where,marshal(pivot),marshal(value)) end end |
#last ⇒ Object
Return the last element in the list. Redis: LINDEX(-1)
150 151 152 |
# File 'lib/redis/list.rb', line 150 def last at(-1) end |
#length ⇒ Object Also known as: size
Return the length of the list. Aliased as size. Redis: LLEN
155 156 157 |
# File 'lib/redis/list.rb', line 155 def length redis.llen(key) end |
#pop(n = nil) ⇒ Object
Remove a member from the end of the list. Redis: RPOP
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/redis/list.rb', line 39 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) ⇒ Object
Add a member to the end of the list. Redis: RPUSH
30 31 32 33 34 35 36 |
# File 'lib/redis/list.rb', line 30 def push(*values) allow_expiration do count = redis.rpush(key, values.map{|v| marshal(v) }) redis.ltrim(key, -[:maxlength], -1) if [:maxlength] count end 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
134 135 136 |
# File 'lib/redis/list.rb', line 134 def range(start_index, end_index) redis.lrange(key, start_index, end_index).map{|v| unmarshal(v) } 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
59 60 61 |
# File 'lib/redis/list.rb', line 59 def rpoplpush(destination) unmarshal redis.rpoplpush(key, destination.is_a?(Redis::List) ? destination.key : destination.to_s) end |
#shift(n = nil) ⇒ Object
Remove a member from the start of the list. Redis: LPOP
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/redis/list.rb', line 73 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 |
#to_s ⇒ Object
169 170 171 |
# File 'lib/redis/list.rb', line 169 def to_s values.join(', ') end |
#unshift(*values) ⇒ Object
Add a member to the start of the list. Redis: LPUSH
64 65 66 67 68 69 70 |
# File 'lib/redis/list.rb', line 64 def unshift(*values) allow_expiration do count = redis.lpush(key, values.map{|v| marshal(v) }) redis.ltrim(key, 0, [:maxlength] - 1) if [:maxlength] count end end |
#values ⇒ Object Also known as: get, value
Return all values in the list. Redis: LRANGE(0,-1)
86 87 88 89 |
# File 'lib/redis/list.rb', line 86 def values vals = range(0, -1) vals.nil? ? [] : vals end |