Class: SplitIoClient::Cache::Adapters::RedisAdapter
- Inherits:
-
Object
- Object
- SplitIoClient::Cache::Adapters::RedisAdapter
- Defined in:
- lib/cache/adapters/redis_adapter.rb
Overview
Redis adapter used to provide interface to Redis
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_set(key, val) ⇒ Object
- #append_to_string(key, val) ⇒ Object
- #bool(key) ⇒ Object
- #delete(key) ⇒ Object
- #delete_from_map(key, field) ⇒ Object
- #delete_from_set(key, val) ⇒ Object
-
#exists?(key) ⇒ Boolean
General.
- #find_in_map(key, field) ⇒ Object
- #find_strings_by_prefix(prefix) ⇒ Object (also: #find_sets_by_prefix)
- #get_all_from_set(key) ⇒ Object
- #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
- #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.
11 12 13 14 15 |
# File 'lib/cache/adapters/redis_adapter.rb', line 11 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.
9 10 11 |
# File 'lib/cache/adapters/redis_adapter.rb', line 9 def redis @redis end |
Instance Method Details
#add_to_map(key, field, value) ⇒ Object
22 23 24 |
# File 'lib/cache/adapters/redis_adapter.rb', line 22 def add_to_map(key, field, value) @redis.hset(key, field, value) end |
#add_to_set(key, val) ⇒ Object
80 81 82 |
# File 'lib/cache/adapters/redis_adapter.rb', line 80 def add_to_set(key, val) @redis.sadd(key, val) end |
#append_to_string(key, val) ⇒ Object
63 64 65 |
# File 'lib/cache/adapters/redis_adapter.rb', line 63 def append_to_string(key, val) @redis.append(key, val) end |
#bool(key) ⇒ Object
72 73 74 |
# File 'lib/cache/adapters/redis_adapter.rb', line 72 def bool(key) @redis.get(key) == 'true' end |
#delete(key) ⇒ Object
115 116 117 118 119 |
# File 'lib/cache/adapters/redis_adapter.rb', line 115 def delete(key) return nil if key == [] @redis.del(key) end |
#delete_from_map(key, field) ⇒ Object
30 31 32 |
# File 'lib/cache/adapters/redis_adapter.rb', line 30 def delete_from_map(key, field) @redis.hdel(key, field) end |
#delete_from_set(key, val) ⇒ Object
84 85 86 |
# File 'lib/cache/adapters/redis_adapter.rb', line 84 def delete_from_set(key, val) @redis.srem(key, val) end |
#exists?(key) ⇒ Boolean
General
111 112 113 |
# File 'lib/cache/adapters/redis_adapter.rb', line 111 def exists?(key) @redis.exists(key) end |
#find_in_map(key, field) ⇒ Object
26 27 28 |
# File 'lib/cache/adapters/redis_adapter.rb', line 26 def find_in_map(key, field) @redis.hget(key, field) end |
#find_strings_by_prefix(prefix) ⇒ Object Also known as: find_sets_by_prefix
55 56 57 |
# File 'lib/cache/adapters/redis_adapter.rb', line 55 def find_strings_by_prefix(prefix) @redis.keys("#{prefix}*") end |
#get_all_from_set(key) ⇒ Object
96 97 98 |
# File 'lib/cache/adapters/redis_adapter.rb', line 96 def get_all_from_set(key) @redis.smembers(key) end |
#get_map(key) ⇒ Object
42 43 44 |
# File 'lib/cache/adapters/redis_adapter.rb', line 42 def get_map(key) @redis.hgetall(key) end |
#get_set(key) ⇒ Object
88 89 90 |
# File 'lib/cache/adapters/redis_adapter.rb', line 88 def get_set(key) @redis.smembers(key) end |
#in_map?(key, field) ⇒ Boolean
34 35 36 |
# File 'lib/cache/adapters/redis_adapter.rb', line 34 def in_map?(key, field) @redis.hexists(key, field) end |
#in_set?(key, val) ⇒ Boolean
92 93 94 |
# File 'lib/cache/adapters/redis_adapter.rb', line 92 def in_set?(key, val) @redis.sismember(key, val) end |
#inc(key, inc = 1) ⇒ Object
121 122 123 |
# File 'lib/cache/adapters/redis_adapter.rb', line 121 def inc(key, inc = 1) @redis.incrby(key, inc) end |
#initialize_map(key) ⇒ Object Also known as: initialize_set
Map
18 19 20 |
# File 'lib/cache/adapters/redis_adapter.rb', line 18 def initialize_map(key) # No need to initialize hash/map in Redis end |
#map_keys(key) ⇒ Object
38 39 40 |
# File 'lib/cache/adapters/redis_adapter.rb', line 38 def map_keys(key) @redis.hkeys(key) end |
#multiple_strings(keys) ⇒ Object
59 60 61 |
# File 'lib/cache/adapters/redis_adapter.rb', line 59 def multiple_strings(keys) Hash[keys.zip(@redis.mget(keys))] end |
#random_set_elements(key, count) ⇒ Object
106 107 108 |
# File 'lib/cache/adapters/redis_adapter.rb', line 106 def random_set_elements(key, count) @redis.srandmember(key, count) end |
#set_bool(key, val) ⇒ Object
Bool
68 69 70 |
# File 'lib/cache/adapters/redis_adapter.rb', line 68 def set_bool(key, val) @redis.set(key, val.to_s) end |
#set_string(key, str) ⇒ Object
51 52 53 |
# File 'lib/cache/adapters/redis_adapter.rb', line 51 def set_string(key, str) @redis.set(key, str) end |
#string(key) ⇒ Object
String
47 48 49 |
# File 'lib/cache/adapters/redis_adapter.rb', line 47 def string(key) @redis.get(key) end |
#union_sets(set_keys) ⇒ Object
100 101 102 103 104 |
# File 'lib/cache/adapters/redis_adapter.rb', line 100 def union_sets(set_keys) return [] if set_keys == [] @redis.sunion(set_keys) end |