class PhraseApp::InContextEditor::Cache

Attributes

lifetime[RW]

Public Class Methods

new(args={}) click to toggle source
# File lib/phraseapp-in-context-editor-ruby/cache.rb, line 9
def initialize(args={})
  @store = {}
  @lifetime = args.fetch(:lifetime, PhraseApp::InContextEditor.cache_lifetime)
end

Public Instance Methods

cached?(cache_key) click to toggle source
# File lib/phraseapp-in-context-editor-ruby/cache.rb, line 14
def cached?(cache_key)
  @store.has_key?(cache_key) && !expired?(cache_key)
end
get(cache_key) click to toggle source
# File lib/phraseapp-in-context-editor-ruby/cache.rb, line 18
def get(cache_key)
  begin
    @store.fetch(cache_key)[:payload]
  rescue
    nil
  end
end
set(cache_key, value) click to toggle source
# File lib/phraseapp-in-context-editor-ruby/cache.rb, line 26
def set(cache_key, value)
  @store[cache_key] = {timestamp: Time.now, payload: value}
end

Private Instance Methods

expired?(cache_key) click to toggle source
# File lib/phraseapp-in-context-editor-ruby/cache.rb, line 31
def expired?(cache_key)
  @store.fetch(cache_key)[:timestamp] < (Time.now - @lifetime)
end