module RecordCache::Base

Public Class Methods

disable!() click to toggle source

To disable the record cache for all models:

RecordCache::Base.disabled!

Enable again with:

RecordCache::Base.enable
# File lib/record_cache/base.rb, line 60
def disable!
  @status = RecordCache::DISABLED
end
enable() click to toggle source

Enable record cache

# File lib/record_cache/base.rb, line 65
def enable
  @status = RecordCache::ENABLED
end
enabled() { || ... } click to toggle source

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
# 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) click to toggle source
# File lib/record_cache/base.rb, line 11
def included(klass)
  klass.class_eval do
    extend ClassMethods
    include InstanceMethods
  end
end
logger() click to toggle source

The logger instance (Rails.logger if present)

# File lib/record_cache/base.rb, line 19
def logger
  @logger ||= (rails_logger || ::ActiveRecord::Base.logger)
end
logger=(logger) click to toggle source

Provide a different logger for Record Cache related information

# File lib/record_cache/base.rb, line 24
def logger=(logger)
  @logger = logger
end
rails_logger() click to toggle source
# File lib/record_cache/base.rb, line 28
def rails_logger
  defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
end
register_store(id, store) click to toggle source

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))

# File lib/record_cache/base.rb, line 47
def register_store(id, store)
  stores[id] = RecordCache::MultiRead.test(store)
end
status() click to toggle source

Retrieve the current status

# File lib/record_cache/base.rb, line 91
def status
  @status ||= RecordCache::ENABLED
end
stores() click to toggle source

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

# File lib/record_cache/base.rb, line 52
def stores
  @stores ||= {}
end
version_store() click to toggle source

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)

# 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) click to toggle source

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)

# 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() { || ... } click to toggle source

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!

# 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