Class: OptionalRedisCacheStore

Inherits:
Object
  • Object
show all
Defined in:
lib/cache_store_redis/optional_redis_cache_store.rb

Overview

This class is used to define a redis cache store that logs failures as warnings but does not raise errors for cache connections

Instance Method Summary collapse

Constructor Details

#initialize(namespace: nil, config: nil, logger: nil) ⇒ OptionalRedisCacheStore

Returns a new instance of OptionalRedisCacheStore.



6
7
8
9
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 6

def initialize(namespace: nil, config: nil, logger: nil)
  @cache_store = RedisCacheStore.new(namespace, config)
  @logger = logger || Logger.new(STDOUT)
end

Instance Method Details

#configure(host = 'localhost', port = 6379, db = 'default', password = nil, driver: nil, url: nil, connect_timeout: 0.5, read_timeout: 1, write_timeout: 0.5) ⇒ Object

This method is called to configure the connection to the cache store.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 16

def configure(
  host = 'localhost',
  port = 6379,
  db = 'default',
  password = nil,
  driver: nil,
  url: nil,
  connect_timeout: 0.5,
  read_timeout: 1,
  write_timeout: 0.5)
  redis_store.configure(
    host,
    port,
    db,
    password,
    driver: driver,
    url: url,
    connect_timeout: connect_timeout,
    read_timeout: read_timeout,
    write_timeout: write_timeout
  )
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 78

def exist?(key)
  redis_store.exist?(key)
rescue => e
  @logger.error(
    "[#{self.class}] - An error occurred checking if a key exists in the cache. " \
"Key: #{key} | Error: #{e.message} | Backtrace: #{e.backtrace}"
  )
  false
end

#get(key, expires_in = 0, &block) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 49

def get(key, expires_in = 0, &block)
  value = optional_get(key, expires_in)

  if value.nil? && block_given?
    value = yield
    set(key, value, expires_in)
  end

  value
end

#optional_get(key, expires_in = 0) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 39

def optional_get(key, expires_in = 0)
  redis_store.get(key, expires_in)
rescue => e
  @logger.error(
    "[#{self.class}] - An error occurred requesting data from the cache. " \
"Key: #{key} | Error: #{e.message} | Backtrace: #{e.backtrace}"
  )
  nil
end

#pingObject



88
89
90
91
92
93
94
95
96
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 88

def ping
  redis_store.ping
rescue => e
  @logger.error(
    "[#{self.class}] - An error occurred checking pinging the cache. " \
"Error: #{e.message} | Backtrace: #{e.backtrace}"
  )
  false
end

#redis_storeObject



11
12
13
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 11

def redis_store
  @cache_store
end

#remove(key) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 69

def remove(key)
  redis_store.remove(key)
rescue => e
  @logger.error(
    "[#{self.class}] - An error occurred removing data from the cache. " \
"Key: #{key} | Error: #{e.message} | Backtrace: #{e.backtrace}"
  )
end

#set(key, value, expires_in = 0) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 60

def set(key, value, expires_in = 0)
  redis_store.set(key, value, expires_in)
rescue => e
  @logger.error(
    "[#{self.class}] - An error occurred storing data in the cache. " \
"Key: #{key} | Error: #{e.message} | Backtrace: #{e.backtrace}"
  )
end

#shutdownObject



98
99
100
101
102
103
104
105
106
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 98

def shutdown
  redis_store.shutdown
rescue => e
  @logger.error(
    "[#{self.class}] - An error occurred checking shutting down the connection pool. " \
"Error: #{e.message} | Backtrace: #{e.backtrace}"
  )
  false
end