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

#initialize(no_cache = false) ⇒ QueryCache

Returns a new instance of QueryCache.



6
7
8
9
# File 'lib/montage_rails/query_cache.rb', line 6

def initialize(no_cache = false)
  @cache = {}
  @no_cache = no_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

#no_cacheObject

Returns the value of attribute no_cache.



4
5
6
# File 'lib/montage_rails/query_cache.rb', line 4

def no_cache
  @no_cache
end

Instance Method Details

#clearObject

Clear the entire query cache



26
27
28
# File 'lib/montage_rails/query_cache.rb', line 26

def clear
  @cache = {}
end

#get_or_set_query(klass, query) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/montage_rails/query_cache.rb', line 11

def get_or_set_query(klass, query)
  cached = cache.keys.include?("#{klass}/#{query}") && !no_cache
  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



33
34
35
# File 'lib/montage_rails/query_cache.rb', line 33

def remove(key)
  cache.delete(key)
end