Class: Alephant::Sequencer::SequenceCache
- Inherits:
-
Object
- Object
- Alephant::Sequencer::SequenceCache
- Includes:
- Logger
- Defined in:
- lib/alephant/sequencer/sequence_cache.rb
Constant Summary collapse
- DEFAULT_TTL =
2
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #get(key, &block) ⇒ Object
-
#initialize(config = {}) ⇒ SequenceCache
constructor
A new instance of SequenceCache.
- #set(key, value, ttl = nil) ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ SequenceCache
Returns a new instance of 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 unless config_endpoint.nil? @elasticache ||= ::Dalli::ElastiCache.new(config_endpoint, { :expires_in => ttl }) @client ||= @elasticache.client else logger.debug "Alephant::SequenceCache::#initialize: No config endpoint, NullClient used" logger.metric "NoConfigEndpoint" @client = NullClient.new end end |
Instance Attribute Details
#config ⇒ Object (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, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/alephant/sequencer/sequence_cache.rb', line 26 def get(key, &block) begin 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, block.call) rescue StandardError => e block.call end end |
#set(key, value, ttl = nil) ⇒ Object
38 39 40 |
# File 'lib/alephant/sequencer/sequence_cache.rb', line 38 def set(key, value, ttl = nil) value.tap { |o| @client.set(versioned(key), o, ttl) } end |