Class: MontageRails::QueryCache
- Inherits:
-
Object
- Object
- MontageRails::QueryCache
- Defined in:
- lib/montage_rails/query_cache.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear the entire query cache.
- #get_or_set_query(klass, query) ⇒ Object
-
#initialize ⇒ QueryCache
constructor
A new instance of QueryCache.
-
#remove(key) ⇒ Object
Remove a certain key from the cache Returns the removed value, or nil if nothin was found.
Constructor Details
#initialize ⇒ QueryCache
Returns a new instance of QueryCache.
5 6 7 |
# File 'lib/montage_rails/query_cache.rb', line 5 def initialize @cache = {} end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
3 4 5 |
# File 'lib/montage_rails/query_cache.rb', line 3 def cache @cache end |
Instance Method Details
#clear ⇒ Object
Clear the entire query cache
24 25 26 |
# File 'lib/montage_rails/query_cache.rb', line 24 def clear @cache = {} end |
#get_or_set_query(klass, query) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/montage_rails/query_cache.rb', line 9 def get_or_set_query(klass, query) cached = cache.keys.include?("#{klass}/#{query}") ActiveSupport::Notifications.instrument("reql.montage_rails", notification_payload(query, klass, cached)) do if cached cache["#{klass}/#{query}"] else response = yield cache["#{klass}/#{query}"] = response response end end end |
#remove(key) ⇒ Object
Remove a certain key from the cache Returns the removed value, or nil if nothin was found
31 32 33 |
# File 'lib/montage_rails/query_cache.rb', line 31 def remove(key) cache.delete(key) end |