Class: MontageRails::QueryCache

Inherits:
Object
  • Object
show all
Defined in:
lib/montage_rails/query_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQueryCache

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

#cacheObject (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

#clearObject

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