Class: Alephant::Broker::Cache::Client

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/alephant/broker/cache.rb

Constant Summary collapse

DEFAULT_TTL =
2592000

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
# File 'lib/alephant/broker/cache.rb', line 12

def initialize
  unless config_endpoint.nil?
    @@elasticache ||= ::Dalli::ElastiCache.new(config_endpoint, { :expires_in => ttl })
    @@client ||= @@elasticache.client
  else
    logger.debug "Broker::Cache::Client#initialize: No config endpoint, NullClient used"
    logger.metric "NoConfigEndpoint"
    @@client = NullClient.new
  end
end

Instance Method Details

#get(key, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/alephant/broker/cache.rb', line 23

def get(key, &block)
  begin
    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
    result ? result : set(key, block.call)
  rescue StandardError => e
    block.call if block_given?
  end
end

#set(key, value, ttl = nil) ⇒ Object



35
36
37
# File 'lib/alephant/broker/cache.rb', line 35

def set(key, value, ttl = nil)
  value.tap { |o| @@client.set(versioned(key), o, ttl) }
end