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


50
51
52
# File 'lib/record_cache/base.rb', line 50

def disable!
  @status = RecordCache::DISABLED
end

.enableObject

Enable record cache



55
56
57
# File 'lib/record_cache/base.rb', line 55

def enable
  @status = RecordCache::ENABLED
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 ||= defined?(::Rails) ? ::Rails.logger : ::ActiveRecord::Base.logger
end

.register_store(id, store) ⇒ Object

Register a store with a specific id for reference with :store in cache_records e.g. RecordCache::Base.register_store(:server, ActiveSupport::Cache.lookup_store(:memory_store))



37
38
39
# File 'lib/record_cache/base.rb', line 37

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

.statusObject

Retrieve the current status



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

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

.storesObject

The hash of stores (store_id => store)



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

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)



31
32
33
# File 'lib/record_cache/base.rb', line 31

def version_store
  @version_store ||= RecordCache::VersionStore.new(RecordCache::MultiRead.test(Rails.cache))
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)



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

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!



68
69
70
71
72
73
74
75
76
# File 'lib/record_cache/base.rb', line 68

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