Class: SplitIoClient::Cache::Adapters::RedisAdapter
- Inherits:
-
Object
- Object
- SplitIoClient::Cache::Adapters::RedisAdapter
- Defined in:
- lib/splitclient-rb/cache/adapters/redis_adapter.rb
Overview
Redis adapter used to provide interface to Redis
Constant Summary collapse
- SCAN_SLICE =
5000
Instance Attribute Summary collapse
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
- #add_to_map(key, field, value) ⇒ Object
-
#add_to_queue(key, val) ⇒ Object
Queue.
- #add_to_set(key, val) ⇒ Object
- #append_to_string(key, val) ⇒ Object
- #bool(key) ⇒ Object
- #clear(prefix) ⇒ Object
- #delete(key) ⇒ Object
- #delete_from_map(key, field) ⇒ Object
- #delete_from_set(key, val) ⇒ Object
-
#exists?(key) ⇒ Boolean
General.
- #expire(key, seconds) ⇒ Object
- #find_in_map(key, field) ⇒ Object
- #find_strings_by_pattern(pattern) ⇒ Object
- #find_strings_by_prefix(prefix) ⇒ Object (also: #find_sets_by_prefix)
- #get_all_from_set(key) ⇒ Object
-
#get_from_queue(key, count) ⇒ Object
count = 0 will result in lrange(0,-1), fetching all items.
- #get_map(key) ⇒ Object
- #get_set(key) ⇒ Object
- #in_map?(key, field) ⇒ Boolean
- #in_set?(key, val) ⇒ Boolean
- #inc(key, inc = 1) ⇒ Object
-
#initialize(redis_url) ⇒ RedisAdapter
constructor
A new instance of RedisAdapter.
-
#initialize_map(key) ⇒ Object
(also: #initialize_set)
Map.
- #map_keys(key) ⇒ Object
- #multiple_strings(keys) ⇒ Object
- #pipelined ⇒ Object
- #random_set_elements(key, count) ⇒ Object
-
#set_bool(key, val) ⇒ Object
Bool.
- #set_string(key, str) ⇒ Object
-
#string(key) ⇒ Object
String.
- #union_sets(set_keys) ⇒ Object
Constructor Details
#initialize(redis_url) ⇒ RedisAdapter
Returns a new instance of RedisAdapter.
14 15 16 17 18 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 14 def initialize(redis_url) connection = redis_url.is_a?(Hash) ? redis_url : { url: redis_url } @redis = Redis.new(connection) end |
Instance Attribute Details
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
12 13 14 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 12 def redis @redis end |
Instance Method Details
#add_to_map(key, field, value) ⇒ Object
25 26 27 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 25 def add_to_map(key, field, value) @redis.hset(key, field, value) end |
#add_to_queue(key, val) ⇒ Object
Queue
128 129 130 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 128 def add_to_queue(key, val) @redis.rpush(key, val) end |
#add_to_set(key, val) ⇒ Object
97 98 99 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 97 def add_to_set(key, val) @redis.sadd(key, val) end |
#append_to_string(key, val) ⇒ Object
80 81 82 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 80 def append_to_string(key, val) @redis.append(key, val) end |
#bool(key) ⇒ Object
89 90 91 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 89 def bool(key) @redis.get(key) == 'true' end |
#clear(prefix) ⇒ Object
164 165 166 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 164 def clear(prefix) # noop end |
#delete(key) ⇒ Object
148 149 150 151 152 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 148 def delete(key) return nil if key == [] @redis.del(key) end |
#delete_from_map(key, field) ⇒ Object
33 34 35 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 33 def delete_from_map(key, field) @redis.hdel(key, field) end |
#delete_from_set(key, val) ⇒ Object
101 102 103 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 101 def delete_from_set(key, val) @redis.srem(key, val) end |
#exists?(key) ⇒ Boolean
General
144 145 146 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 144 def exists?(key) @redis.exists?(key) end |
#expire(key, seconds) ⇒ Object
168 169 170 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 168 def expire(key, seconds) @redis.expire(key, seconds) end |
#find_in_map(key, field) ⇒ Object
29 30 31 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 29 def find_in_map(key, field) @redis.hget(key, field) end |
#find_strings_by_pattern(pattern) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 62 def find_strings_by_pattern(pattern) memo = { items: [], cursor: 0 } loop do memo[:cursor], items = @redis.scan(memo[:cursor], match: "#{pattern}", count: SCAN_SLICE) memo[:items].push(*items) break if memo[:cursor] == '0' end memo[:items] end |
#find_strings_by_prefix(prefix) ⇒ Object Also known as: find_sets_by_prefix
58 59 60 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 58 def find_strings_by_prefix(prefix) find_strings_by_pattern("#{prefix}*") end |
#get_all_from_set(key) ⇒ Object
113 114 115 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 113 def get_all_from_set(key) @redis.smembers(key) end |
#get_from_queue(key, count) ⇒ Object
count = 0 will result in lrange(0,-1), fetching all items
133 134 135 136 137 138 139 140 141 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 133 def get_from_queue(key, count) items = @redis.lrange(key, 0, count - 1) fetched_count = items.size items_to_remove = fetched_count == count ? count : fetched_count @redis.ltrim(key, items_to_remove, -1) items end |
#get_map(key) ⇒ Object
45 46 47 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 45 def get_map(key) @redis.hgetall(key) end |
#get_set(key) ⇒ Object
105 106 107 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 105 def get_set(key) @redis.smembers(key) end |
#in_map?(key, field) ⇒ Boolean
37 38 39 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 37 def in_map?(key, field) @redis.hexists(key, field) end |
#in_set?(key, val) ⇒ Boolean
109 110 111 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 109 def in_set?(key, val) @redis.sismember(key, val) end |
#inc(key, inc = 1) ⇒ Object
154 155 156 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 154 def inc(key, inc = 1) @redis.incrby(key, inc) end |
#initialize_map(key) ⇒ Object Also known as: initialize_set
Map
21 22 23 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 21 def initialize_map(key) # No need to initialize hash/map in Redis end |
#map_keys(key) ⇒ Object
41 42 43 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 41 def map_keys(key) @redis.hkeys(key) end |
#multiple_strings(keys) ⇒ Object
76 77 78 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 76 def multiple_strings(keys) Hash[keys.zip(@redis.mget(keys))] end |
#pipelined ⇒ Object
158 159 160 161 162 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 158 def pipelined @redis.pipelined do yield end end |
#random_set_elements(key, count) ⇒ Object
123 124 125 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 123 def random_set_elements(key, count) @redis.srandmember(key, count) end |
#set_bool(key, val) ⇒ Object
Bool
85 86 87 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 85 def set_bool(key, val) @redis.set(key, val.to_s) end |
#set_string(key, str) ⇒ Object
54 55 56 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 54 def set_string(key, str) @redis.set(key, str) end |
#string(key) ⇒ Object
String
50 51 52 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 50 def string(key) @redis.get(key) end |
#union_sets(set_keys) ⇒ Object
117 118 119 120 121 |
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 117 def union_sets(set_keys) return [] if set_keys == [] @redis.sunion(set_keys) end |