Module: RecordCache::Base::ClassMethods

Defined in:
lib/record_cache/base.rb

Instance Method Summary collapse

Instance Method Details

#cache_records(options) ⇒ Object

Cache the instances of this model options:

:store => the cache store for the instances, e.g. :memory_store, :dalli_store* (default: Rails.cache)
          or one of the store ids defined using +RecordCache::Base.register_store+
:key   => provide a unique shorter key to limit the cache key length (default: model.name)
:index => one or more attributes (Symbols) for which the ids are cached for the value of the attribute
:request_cache => Set to true in case the exact same query is executed more than once during a single request
                  If set to true somewhere, make sure to add the following to your application controller:
                  before_filter { |c| RecordCache::Strategy::RequestCache.clear }

Hints:

- Dalli is a high performance pure Ruby client for accessing memcached servers, see https://github.com/mperham/dalli
- use :store => :memory_store in case all records can easily fit in server memory
- use :index => :account_id in case the records are (almost) always queried as a full set per account
- use :index => :person_id for aggregated has_many associations


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

def cache_records(options)
  @rc_dispatcher = RecordCache::Dispatcher.new(self) unless defined?(@rc_dispatcher)
  store = RecordCache::MultiRead.test(options[:store] ? RecordCache::Base.stores[options[:store]] || ActiveSupport::Cache.lookup_store(options[:store]) : (defined?(::Rails) ? Rails.cache : ActiveSupport::Cache.lookup_store(:memory_store)))
  # always register an ID Cache
  record_cache.register(:id, ::RecordCache::Strategy::IdCache, store, options)
  # parse :index option
  [options[:index]].flatten.compact.map(&:to_sym).each do |index|
    record_cache.register(index, ::RecordCache::Strategy::IndexCache, store, options.merge({:index => index}))
  end
  # parse :request_cache option
  record_cache.register(:request_cache, ::RecordCache::Strategy::RequestCache, store, options) if options[:request_cache]
  # Callback for Data Store specific initialization
  record_cache_init
end

#record_cacheObject

Returns the RecordCache (class) instance



116
117
118
# File 'lib/record_cache/base.rb', line 116

def record_cache
  @rc_dispatcher
end

#record_cache?Boolean

Returns true if record cache is defined and active for this class

Returns:

  • (Boolean)


111
112
113
# File 'lib/record_cache/base.rb', line 111

def record_cache?
  record_cache && RecordCache::Base.status == RecordCache::ENABLED
end