class OptionalRedisCacheStore
This class is used to define a redis cache store that logs failures as warnings but does not raise errors for cache connections
Public Class Methods
new(namespace: nil, config: nil, logger: nil)
click to toggle source
# 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
Public Instance Methods
configure( host = 'localhost', port = 6379, db = 'default', password = nil, driver: nil, url: nil, connect_timeout: 0.5, read_timeout: 1, write_timeout: 0.5)
click to toggle source
This method is called to configure the connection to the cache store.
# 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)
click to toggle source
# 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) { || ... }
click to toggle source
# 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)
click to toggle source
# 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
ping()
click to toggle source
# 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_store()
click to toggle source
# File lib/cache_store_redis/optional_redis_cache_store.rb, line 9 def redis_store @cache_store end
remove(key)
click to toggle source
# 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)
click to toggle source
# 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
shutdown()
click to toggle source
# 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
Private Instance Methods
handle_error(ex, msg)
click to toggle source
# File lib/cache_store_redis/optional_redis_cache_store.rb, line 90 def handle_error(ex, msg) @logger.error do "[#{self.class}] - An error occurred #{msg}. " \ "Error: #{ex.message} | Backtrace: #{ex.backtrace}" end end