Class: PhraseApp::InContextEditor::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/phraseapp-in-context-editor-ruby/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Cache

Returns a new instance of Cache.



10
11
12
13
# File 'lib/phraseapp-in-context-editor-ruby/cache.rb', line 10

def initialize(args={})
  @store = {}
  @lifetime = args.fetch(:lifetime, PhraseApp::InContextEditor.cache_lifetime)
end

Instance Attribute Details

#lifetimeObject

Returns the value of attribute lifetime.



8
9
10
# File 'lib/phraseapp-in-context-editor-ruby/cache.rb', line 8

def lifetime
  @lifetime
end

Instance Method Details

#cached?(cache_key) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/phraseapp-in-context-editor-ruby/cache.rb', line 15

def cached?(cache_key)
  @store.has_key?(cache_key) && !expired?(cache_key)
end

#get(cache_key) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/phraseapp-in-context-editor-ruby/cache.rb', line 19

def get(cache_key)
  begin
    @store.fetch(cache_key)[:payload]
  rescue
    nil
  end
end

#set(cache_key, value) ⇒ Object



27
28
29
# File 'lib/phraseapp-in-context-editor-ruby/cache.rb', line 27

def set(cache_key, value)
  @store[cache_key] = {timestamp: Time.now, payload: value}
end