Class: Split::RedisInterface
- Inherits:
-
Object
- Object
- Split::RedisInterface
- Defined in:
- lib/split/redis_interface.rb
Overview
Simplifies the interface to Redis.
Instance Method Summary collapse
- #add_to_list(list_name, value) ⇒ Object
- #add_to_set(set_name, value) ⇒ Object
-
#initialize ⇒ RedisInterface
constructor
A new instance of RedisInterface.
- #list_length(list_name) ⇒ Object
- #make_list_length(list_name, new_length) ⇒ Object
- #persist_list(list_name, list_values) ⇒ Object
- #remove_last_item_from_list(list_name) ⇒ Object
- #set_list_index(list_name, index, value) ⇒ Object
Constructor Details
#initialize ⇒ RedisInterface
Returns a new instance of RedisInterface.
5 6 7 |
# File 'lib/split/redis_interface.rb', line 5 def initialize self.redis = Split.redis end |
Instance Method Details
#add_to_list(list_name, value) ⇒ Object
22 23 24 |
# File 'lib/split/redis_interface.rb', line 22 def add_to_list(list_name, value) redis.rpush(list_name, value) end |
#add_to_set(set_name, value) ⇒ Object
42 43 44 |
# File 'lib/split/redis_interface.rb', line 42 def add_to_set(set_name, value) redis.sadd(set_name, value) unless redis.sismember(set_name, value) end |
#list_length(list_name) ⇒ Object
30 31 32 |
# File 'lib/split/redis_interface.rb', line 30 def list_length(list_name) redis.llen(list_name) end |
#make_list_length(list_name, new_length) ⇒ Object
38 39 40 |
# File 'lib/split/redis_interface.rb', line 38 def make_list_length(list_name, new_length) redis.ltrim(list_name, 0, new_length - 1) end |
#persist_list(list_name, list_values) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/split/redis_interface.rb', line 9 def persist_list(list_name, list_values) max_index = list_length(list_name) - 1 list_values.each_with_index do |value, index| if index > max_index add_to_list(list_name, value) else set_list_index(list_name, index, value) end end make_list_length(list_name, list_values.length) list_values end |
#remove_last_item_from_list(list_name) ⇒ Object
34 35 36 |
# File 'lib/split/redis_interface.rb', line 34 def remove_last_item_from_list(list_name) redis.rpop(list_name) end |
#set_list_index(list_name, index, value) ⇒ Object
26 27 28 |
# File 'lib/split/redis_interface.rb', line 26 def set_list_index(list_name, index, value) redis.lset(list_name, index, value) end |