module RecordCache::Base
Public Class Methods
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 record cache
# File lib/record_cache/base.rb, line 65 def enable @status = RecordCache::ENABLED end
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
# File lib/record_cache/base.rb, line 11 def included(klass) klass.class_eval do extend ClassMethods include InstanceMethods end end
The logger instance (Rails.logger if present)
# File lib/record_cache/base.rb, line 19 def logger @logger ||= (rails_logger || ::ActiveRecord::Base.logger) end
Provide a different logger for Record Cache related information
# File lib/record_cache/base.rb, line 24 def logger=(logger) @logger = logger end
# File lib/record_cache/base.rb, line 28 def rails_logger defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger end
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
Retrieve the current status
# File lib/record_cache/base.rb, line 91 def status @status ||= RecordCache::ENABLED end
The hash of registered record stores (store_id => store)
# File lib/record_cache/base.rb, line 52 def stores @stores ||= {} end
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
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
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