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

cache strategy specific options:

:index => one or more attributes (Symbols) for which the ids are cached for the value of the attribute

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


125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/record_cache/base.rb', line 125

def cache_records(options = {})
  unless @rc_dispatcher
    @rc_dispatcher = RecordCache::Dispatcher.new(self)
    # Callback for Data Store specific initialization
    record_cache_init

    class << self
      alias_method_chain :inherited, :record_cache
    end
  end
  # parse the requested strategies from the given options
  @rc_dispatcher.parse(options)
end

#inherited_with_record_cache(subclass) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/record_cache/base.rb', line 149

def inherited_with_record_cache(subclass)
  class << subclass
    def record_cache
      self.superclass.record_cache
    end
  end
  inherited_without_record_cache(subclass)
end

#record_cacheObject

Returns the RecordCache (class) instance



145
146
147
# File 'lib/record_cache/base.rb', line 145

def record_cache
  @rc_dispatcher
end

#record_cache?Boolean

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

Returns:

  • (Boolean)


140
141
142
# File 'lib/record_cache/base.rb', line 140

def record_cache?
  record_cache && record_cache.instance_variable_get('@base') == self && RecordCache::Base.status == RecordCache::ENABLED
end