Class: Alephant::Sequencer::SequenceCache

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/alephant/sequencer/sequence_cache.rb

Constant Summary collapse

DEFAULT_TTL =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ SequenceCache



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/alephant/sequencer/sequence_cache.rb', line 13

def initialize(config = {})
  @config = config

  if config_endpoint.nil?
    logger.debug 'Alephant::SequenceCache::#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 Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/alephant/sequencer/sequence_cache.rb', line 9

def config
  @config
end

Instance Method Details

#get(key) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/alephant/sequencer/sequence_cache.rb', line 26

def get(key)
  versioned_key = versioned key
  result = @client.get versioned_key
  logger.info "Alephant::SequenceCache#get key: #{versioned_key} - #{result ? 'hit' : 'miss'}"
  logger.metric 'GetKeyMiss' unless result
  result ? result : set(key, yield)
rescue StandardError => e
  yield
end

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



36
37
38
# File 'lib/alephant/sequencer/sequence_cache.rb', line 36

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