Module: RecordCache::Base

Defined in:
lib/record_cache/base.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.disable!Object

To disable the record cache for all models:

RecordCache::Base.disabled!

Enable again with:

RecordCache::Base.enable


60
61
62
# File 'lib/record_cache/base.rb', line 60

def disable!
  @status = RecordCache::DISABLED
end

.enableObject

Enable record cache



65
66
67
# File 'lib/record_cache/base.rb', line 65

def enable
  @status = RecordCache::ENABLED
end

.enabled(&block) ⇒ Object

Executes the block with caching enabled. Useful in testing scenarios.

RecordCache::Base.enabled do
  @foo = Article.find(1)
  @foo.update_attributes(:time_spent => 45)
  @foo = Article.find(1)
  @foo.time_spent.should be_nil
  TimeSpent.last.amount.should == 45
end


80
81
82
83
84
85
86
87
88
# File 'lib/record_cache/base.rb', line 80

def enabled(&block)
  previous_status = @status
  begin
    @status = RecordCache::ENABLED
    yield
  ensure
    @status = previous_status
  end
end

.included(klass) ⇒ Object



11
12
13
14
15
16
# File 'lib/record_cache/base.rb', line 11

def included(klass)
  klass.class_eval do
    extend ClassMethods
    include InstanceMethods
  end
end

.loggerObject

The logger instance (Rails.logger if present)



19
20
21
# File 'lib/record_cache/base.rb', line 19

def logger
  @logger ||= (rails_logger || ::ActiveRecord::Base.logger)
end

.logger=(logger) ⇒ Object

Provide a different logger for Record Cache related information



24
25
26
# File 'lib/record_cache/base.rb', line 24

def logger=(logger)
  @logger = logger
end

.rails_loggerObject



28
29
30
# File 'lib/record_cache/base.rb', line 28

def rails_logger
  defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
end

.register_store(id, store) ⇒ Object

Register a cache store by id for future reference with the :store option for cache_records e.g. RecordCache::Base.register_store(:server, ActiveSupport::Cache.lookup_store(:memory_store))



47
48
49
# File 'lib/record_cache/base.rb', line 47

def register_store(id, store)
  stores[id] = RecordCache::MultiRead.test(store)
end

.statusObject

Retrieve the current status



91
92
93
# File 'lib/record_cache/base.rb', line 91

def status
  @status ||= RecordCache::ENABLED
end

.storesObject

The hash of registered record stores (store_id => store)



52
53
54
# File 'lib/record_cache/base.rb', line 52

def stores
  @stores ||= {}
end

.version_storeObject

The ActiveSupport::Cache::Store instance that contains the current record(group) versions. Note that it must point to a single Store shared by all webservers (defaults to Rails.cache)



40
41
42
43
# File 'lib/record_cache/base.rb', line 40

def version_store
  self.version_store = Rails.cache unless @version_store
  @version_store
end

.version_store=(store) ⇒ Object

Set the ActiveSupport::Cache::Store instance that contains the current record(group) versions. Note that it must point to a single Store shared by all webservers (defaults to Rails.cache)



34
35
36
# File 'lib/record_cache/base.rb', line 34

def version_store=(store)
  @version_store = RecordCache::VersionStore.new(RecordCache::MultiRead.test(store))
end

.without_record_cache(&block) ⇒ Object

execute block of code without using the records cache to fetch records note that updates are still written to the cache, as otherwise other workers may receive stale results. To fully disable caching use disable!



99
100
101
102
103
104
105
106
107
# File 'lib/record_cache/base.rb', line 99

def without_record_cache(&block)
  old_status = status
  begin
    @status = RecordCache::NO_FETCH
    yield
  ensure
    @status = old_status
  end
end