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
22
23
# File 'lib/alephant/broker/cache/client.rb', line 12

def initialize
  if config_endpoint.nil?
    logger.error(
      method:  'Broker::Cache::Client#initialize',
      message: 'No config endpoint, NullClient used'
    )
    logger.metric 'NoConfigEndpoint'
    @client = NullClient.new
  else
    @client ||= Dalli::Client.new(config_endpoint, expires_in: ttl)
  end
end

Instance Method Details

#get(key) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/alephant/broker/cache/client.rb', line 25

def get(key)
  versioned_key = versioned(key)
  result        = @client.get(versioned_key)

  logger.debug(
    method: 'Broker::Cache::Client#get',
    key:    versioned_key,
    result: result ? 'hit' : 'miss'
  )
  logger.metric('GetKeyMiss') unless result

  return result if result

  set(key, yield) if block_given?
end

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/alephant/broker/cache/client.rb', line 41

def set(key, value, custom_ttl = nil)
  versioned_key = versioned(key)
  set_ttl       = custom_ttl || ttl

  logger.debug(
    method: "#{self.class}#set",
    key:    versioned_key,
    ttl:    set_ttl
  )

  @client.set(versioned_key, value, set_ttl)

  value
end