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.



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

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

Instance Method Details

#config_endpointObject



23
24
25
# File 'lib/alephant/broker/cache.rb', line 23

def config_endpoint
  Broker.config['elasticache_config_endpoint']
end

#get(key, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/alephant/broker/cache.rb', line 31

def get(key, &block)
  begin
    result = @client.get(key)
    logger.info("Broker::Cache::Client#get key: #{key} - #{result ? 'hit' : 'miss'}")
    result ? result : set(key, block.call)
  rescue StandardError => e
    block.call if block_given?
  end
end

#set(key, value) ⇒ Object



41
42
43
# File 'lib/alephant/broker/cache.rb', line 41

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

#ttlObject



27
28
29
# File 'lib/alephant/broker/cache.rb', line 27

def ttl
   Broker.config['elasticache_ttl'] || DEFAULT_TTL
end