Class: RecordCache::Strategy::RequestCache

Inherits:
Base
  • Object
show all
Defined in:
lib/record_cache/strategy/request_cache.rb

Constant Summary collapse

@@request_store =
{}

Constants inherited from Base

Base::ATTRIBUTES_KEY, Base::CLASS_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#cacheable?, #initialize

Constructor Details

This class inherits a constructor from RecordCache::Strategy::Base

Class Method Details

.clearObject

call before each request: in application_controller.rb before_filter { |c| RecordCache::Strategy::RequestCache.clear }



13
14
15
# File 'lib/record_cache/strategy/request_cache.rb', line 13

def self.clear
  @@request_store.clear
end

Instance Method Details

#fetch(query, &block) ⇒ Object

return the records from the request cache, execute block in case this is the first time this query is performed during this request



29
30
31
32
33
34
35
36
# File 'lib/record_cache/strategy/request_cache.rb', line 29

def fetch(query, &block)
  klass_store = (@@request_store[@base.name] ||= {})
  key = query.cache_key
  # logging (only in debug mode!) and statistics
  log_cache_hit(key, klass_store.key?(key)) if RecordCache::Base.logger.debug?
  statistics.add(1, klass_store.key?(key) ? 1 : 0) if statistics.active?
  klass_store[key] ||= yield
end

#invalidate(value) ⇒ Object

Handle invalidation call



23
24
25
# File 'lib/record_cache/strategy/request_cache.rb', line 23

def invalidate(value)
  @@request_store.delete(@base.name)
end

#record_change(record, action) ⇒ Object

Handle record change



18
19
20
# File 'lib/record_cache/strategy/request_cache.rb', line 18

def record_change(record, action)
  @@request_store.delete(@base.name)
end