Class: Alephant::Broker::Cache::Client
- Inherits:
-
Object
- Object
- Alephant::Broker::Cache::Client
- Includes:
- Logger
- Defined in:
- lib/alephant/broker/cache/client.rb
Constant Summary collapse
- DEFAULT_TTL =
2_592_000
Instance Method Summary collapse
- #get(key) ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #set(key, value, custom_ttl = nil) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/alephant/broker/cache/client.rb', line 12 def initialize if config_endpoint.nil? logger.debug 'Broker::Cache::Client#initialize: No config endpoint, NullClient used' logger.metric 'NoConfigEndpoint' @client = NullClient.new else @elasticache ||= ::Dalli::ElastiCache.new(config_endpoint, expires_in: ttl) @client ||= @elasticache.client end end |
Instance Method Details
#get(key) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/alephant/broker/cache/client.rb', line 23 def get(key) versioned_key = versioned(key) result = @client.get(versioned_key) logger.info("Broker::Cache::Client#get key: #{versioned_key} - #{result ? 'hit' : 'miss'}") logger.metric('GetKeyMiss') unless result return result if result set(key, yield) if block_given? rescue StandardError => error logger.info(event: 'ErrorCaught', method: "#{self.class}#get", error: error) yield if block_given? end |
#set(key, value, custom_ttl = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/alephant/broker/cache/client.rb', line 38 def set(key, value, custom_ttl = nil) versioned_key = versioned(key) set_ttl = custom_ttl || ttl logger.info("#{self.class}#set - key: #{versioned_key}, value: #{value}, ttl: #{set_ttl}") @client.set(versioned_key, value, set_ttl) value end |