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

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

Constant Summary collapse

DEFAULT_TTL =
2_592_000

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/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
# 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
  result ? result : set(key, block.call)
rescue StandardError => e
  yield if block_given?
end

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



33
34
35
# File 'lib/alephant/broker/cache/client.rb', line 33

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