Class: Conversocial::Resources::QueryEngines::Cache
- Inherits:
-
Object
- Object
- Conversocial::Resources::QueryEngines::Cache
- Defined in:
- lib/conversocial/resources/query_engines/cache.rb
Instance Attribute Summary collapse
-
#expiry ⇒ Object
readonly
Returns the value of attribute expiry.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #get(key) ⇒ Object
- #get_or_set(key, options) ⇒ Object
-
#initialize(expiry = 60*60) ⇒ Cache
constructor
A new instance of Cache.
- #set(key, value) ⇒ Object
Constructor Details
#initialize(expiry = 60*60) ⇒ Cache
Returns a new instance of Cache.
7 8 9 10 |
# File 'lib/conversocial/resources/query_engines/cache.rb', line 7 def initialize expiry=60*60 @cache = {} @expiry = expiry.to_i end |
Instance Attribute Details
#expiry ⇒ Object (readonly)
Returns the value of attribute expiry.
5 6 7 |
# File 'lib/conversocial/resources/query_engines/cache.rb', line 5 def expiry @expiry end |
Instance Method Details
#delete(key) ⇒ Object
27 28 29 |
# File 'lib/conversocial/resources/query_engines/cache.rb', line 27 def delete key @cache.delete key.to_s end |
#get(key) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/conversocial/resources/query_engines/cache.rb', line 31 def get key if @cache[key.to_s] if (Time.now - @cache[key.to_s][:timestamp]) < expiry @cache[key.to_s][:value] else delete key nil end end end |
#get_or_set(key, options) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/conversocial/resources/query_engines/cache.rb', line 12 def get_or_set key, result = get key if result.nil? result = [:get].call if [:if].call result set key, result end end result end |
#set(key, value) ⇒ Object
23 24 25 |
# File 'lib/conversocial/resources/query_engines/cache.rb', line 23 def set key, value @cache[key.to_s] = {:value => value, :timestamp => Time.now} end |