Class: PhraseApp::InContextEditor::Cache
- Inherits:
-
Object
- Object
- PhraseApp::InContextEditor::Cache
- Defined in:
- lib/phraseapp-in-context-editor-ruby/cache.rb
Instance Attribute Summary collapse
-
#lifetime ⇒ Object
Returns the value of attribute lifetime.
Instance Method Summary collapse
- #cached?(cache_key) ⇒ Boolean
- #get(cache_key) ⇒ Object
-
#initialize(args = {}) ⇒ Cache
constructor
A new instance of Cache.
- #set(cache_key, value) ⇒ Object
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
#lifetime ⇒ Object
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
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 |