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

#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



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

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

#add_to_set(key, val) ⇒ Object



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

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

#append_to_string(key, val) ⇒ Object



76
77
78
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 76

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

#bool(key) ⇒ Object



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

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

#clear(prefix) ⇒ Object



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

def clear(prefix)
  keys = @redis.keys("#{prefix}*")

  keys.map { |key| @redis.del(key) }
end

#delete(key) ⇒ Object



143
144
145
146
147
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 143

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



97
98
99
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 97

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

#exists?(key) ⇒ Boolean

General

Returns:

  • (Boolean)


139
140
141
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 139

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

#expire(key, seconds) ⇒ Object



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

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_prefix(prefix) ⇒ Object Also known as: find_sets_by_prefix



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 58

def find_strings_by_prefix(prefix)
  memo = { items: [], cursor: 0 }

  loop do
    memo[:cursor], items = @redis.scan(memo[:cursor], match: "#{prefix}*", count: SCAN_SLICE)

    memo[:items].push(*items)

    break if memo[:cursor] == '0'
  end

  memo[:items]
end

#get_all_from_set(key) ⇒ Object



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

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

#get_from_queue(key, count) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 128

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



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

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

#in_map?(key, field) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


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

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

#inc(key, inc = 1) ⇒ Object



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

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



72
73
74
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 72

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

#pipelined ⇒ Object



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

def pipelined
  @redis.pipelined do
    yield
  end
end

#random_set_elements(key, count) ⇒ Object



119
120
121
# File 'lib/splitclient-rb/cache/adapters/redis_adapter.rb', line 119

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

#set_bool(key, val) ⇒ Object

Bool



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

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



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

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

  @redis.sunion(set_keys)
end