Class: SplitIoClient::Cache::Adapters::RedisAdapter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#redisObject (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



29
30
31
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 29

def add_to_map(key, field, value)
  @redis.hset(key, field, value)
end

#add_to_queue(key, val) ⇒ Object

Queue



132
133
134
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 132

def add_to_queue(key, val)
  @redis.rpush(key, val)
end

#add_to_set(key, val) ⇒ Object



101
102
103
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 101

def add_to_set(key, val)
  @redis.sadd?(key, val)
end

#append_to_string(key, val) ⇒ Object



84
85
86
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 84

def append_to_string(key, val)
  @redis.append(key, val)
end

#bool(key) ⇒ Object



93
94
95
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 93

def bool(key)
  @redis.get(key) == 'true'
end

#clear(prefix) ⇒ Object



168
169
170
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 168

def clear(prefix)
  # noop
end

#delete(key) ⇒ Object



152
153
154
155
156
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 152

def delete(key)
  return nil if key == []

  @redis.del(key)
end

#delete_from_map(key, field) ⇒ Object



37
38
39
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 37

def delete_from_map(key, field)
  @redis.hdel(key, field)
end

#delete_from_set(key, val) ⇒ Object



105
106
107
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 105

def delete_from_set(key, val)
  @redis.srem(key, val)
end

#exists?(key) ⇒ Boolean

General

Returns:

  • (Boolean)


148
149
150
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 148

def exists?(key)
  @redis.exists?(key)
end

#expire(key, seconds) ⇒ Object



172
173
174
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 172

def expire(key, seconds)
  @redis.expire(key, seconds)
end

#find_in_map(key, field) ⇒ Object



33
34
35
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 33

def find_in_map(key, field)
  @redis.hget(key, field)
end

#find_strings_by_pattern(pattern) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 66

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



62
63
64
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 62

def find_strings_by_prefix(prefix)
  find_strings_by_pattern("#{prefix}*")
end

#get_all_from_set(key) ⇒ Object



117
118
119
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 117

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



137
138
139
140
141
142
143
144
145
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 137

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



49
50
51
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 49

def get_map(key)
  @redis.hgetall(key)
end

#get_set(key) ⇒ Object



109
110
111
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 109

def get_set(key)
  @redis.smembers(key)
end

#hincrby(key, field, increment) ⇒ Object



20
21
22
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 20

def hincrby(key, field, increment)
  @redis.hincrby(key, field, increment)
end

#in_map?(key, field) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 41

def in_map?(key, field)
  @redis.hexists(key, field)
end

#in_set?(key, val) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 113

def in_set?(key, val)
  @redis.sismember(key, val)
end

#inc(key, inc = 1) ⇒ Object



158
159
160
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 158

def inc(key, inc = 1)
  @redis.incrby(key, inc)
end

#initialize_map(key) ⇒ Object Also known as: initialize_set

Map



25
26
27
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 25

def initialize_map(key)
  # No need to initialize hash/map in Redis
end

#map_keys(key) ⇒ Object



45
46
47
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 45

def map_keys(key)
  @redis.hkeys(key)
end

#multiple_strings(keys) ⇒ Object



80
81
82
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 80

def multiple_strings(keys)
  Hash[keys.zip(@redis.mget(keys))]
end

#pipelinedObject



162
163
164
165
166
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 162

def pipelined
  @redis.pipelined do |pipeline|
    yield
  end
end

#random_set_elements(key, count) ⇒ Object



127
128
129
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 127

def random_set_elements(key, count)
  @redis.srandmember(key, count)
end

#set_bool(key, val) ⇒ Object

Bool



89
90
91
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 89

def set_bool(key, val)
  @redis.set(key, val.to_s)
end

#set_string(key, str) ⇒ Object



58
59
60
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 58

def set_string(key, str)
  @redis.set(key, str)
end

#string(key) ⇒ Object

String



54
55
56
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 54

def string(key)
  @redis.get(key)
end

#union_sets(set_keys) ⇒ Object



121
122
123
124
125
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 121

def union_sets(set_keys)
  return [] if set_keys == []

  @redis.sunion(set_keys)
end