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.



4
5
6
7
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 4

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.



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

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)


67
68
69
70
71
72
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 67

def exist?(key)
  redis_store.exist?(key)
rescue => e
  handle_error(e, 'checking if a key exists in the cache')
  false
end

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



44
45
46
47
48
49
50
51
52
53
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 44

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



37
38
39
40
41
42
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 37

def optional_get(key, expires_in = 0)
  redis_store.get(key, expires_in)
rescue => e
  handle_error(e, 'requesting data from the cache')
  nil
end

#pingObject



74
75
76
77
78
79
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 74

def ping
  redis_store.ping
rescue => e
  handle_error(e, 'pinging the cache')
  false
end

#redis_storeObject



9
10
11
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 9

def redis_store
  @cache_store
end

#remove(key) ⇒ Object



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

def remove(key)
  redis_store.remove(key)
rescue => e
  handle_error(e, 'removing data from the cache')
end

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



55
56
57
58
59
# File 'lib/cache_store_redis/optional_redis_cache_store.rb', line 55

def set(key, value, expires_in = 0)
  redis_store.set(key, value, expires_in)
rescue => e
  handle_error(e, 'storing data in the cache')
end

#shutdownObject



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

def shutdown
  redis_store.shutdown
rescue => e
  handle_error(e, 'shutting down the connection pool')
  false
end