Class: TranslationDiff::Stores::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/translation_diff/stores/redis.rb

Constant Summary collapse

ONE_WEEK =
60 * 60 * 24 * 7
DEFAULT_NAMESPACE =
"translation-diff".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_pool, timeout: ONE_WEEK, namespace: DEFAULT_NAMESPACE) ⇒ Redis

connection_pool is duck-typed to #with; neither connection_pool nor redis-namespace is a hard dependency.



10
11
12
13
14
# File 'lib/translation_diff/stores/redis.rb', line 10

def initialize(connection_pool, timeout: ONE_WEEK, namespace: DEFAULT_NAMESPACE)
  @connection_pool = connection_pool
  @timeout = timeout
  @namespace = namespace
end

Class Method Details

.build(config) ⇒ Object



5
6
7
# File 'lib/translation_diff/stores/redis.rb', line 5

def self.build(config)
  new(config.redis_pool, timeout: config.cache_ttl, namespace: config.cache_namespace)
end

Instance Method Details

#read_multi(keys) ⇒ Object



16
17
18
# File 'lib/translation_diff/stores/redis.rb', line 16

def read_multi(keys)
  redis { |redis| redis.mget(*keys) }
end

#write(key, value) ⇒ Object

A non-positive or nil timeout means never expires, the same rule the SQL store applies to cache_ttl.



21
22
23
# File 'lib/translation_diff/stores/redis.rb', line 21

def write(key, value)
  redis { |redis| write_one(redis, key, value) }
end

#write_multi(pairs) ⇒ Object



25
26
27
28
29
30
# File 'lib/translation_diff/stores/redis.rb', line 25

def write_multi(pairs)
  return pairs if pairs.empty?

  redis { |redis| redis.pipelined { |p| pairs.each { |key, value| write_one(p, key, value) } } }
  pairs
end