Class: RecordCache::Strategy::UniqueIndexCache
- Defined in:
- lib/record_cache/strategy/unique_index_cache.rb
Class Method Summary collapse
-
.attributes(base) ⇒ Object
All attributes with a unique index for the given model.
-
.parse(base, record_store, options) ⇒ Object
parse the options and return (an array of) instances of this strategy.
Instance Method Summary collapse
-
#cacheable?(query) ⇒ Boolean
Can the cache retrieve the records based on this query?.
-
#initialize(base, attribute, record_store, options, type) ⇒ UniqueIndexCache
constructor
A new instance of UniqueIndexCache.
-
#record_change(record, action) ⇒ Object
Update the version store and the record store.
Methods inherited from Base
#attribute, #fetch, #invalidate
Constructor Details
#initialize(base, attribute, record_store, options, type) ⇒ UniqueIndexCache
Returns a new instance of UniqueIndexCache.
27 28 29 30 31 32 33 34 |
# File 'lib/record_cache/strategy/unique_index_cache.rb', line 27 def initialize(base, attribute, record_store, , type) super(base, attribute, record_store, ) # remember the attributes with a unique index UniqueIndexCache.attributes(base) << attribute # for unique indexes that are not on the :id column, use key: rc/<key or model name>/<attribute>: @cache_key_prefix << "#{attribute}:" unless attribute == :id @type = type end |
Class Method Details
.attributes(base) ⇒ Object
All attributes with a unique index for the given model
6 7 8 |
# File 'lib/record_cache/strategy/unique_index_cache.rb', line 6 def self.attributes(base) (@attributes ||= {})[base.name] ||= [] end |
.parse(base, record_store, options) ⇒ Object
parse the options and return (an array of) instances of this strategy
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/record_cache/strategy/unique_index_cache.rb', line 11 def self.parse(base, record_store, ) return nil unless base.table_exists? attributes = [[:unique_index]].flatten.compact # add unique index for :id by default attributes << :id if base.columns_hash['id'] unless base.record_cache[:id] attributes.uniq! # in development mode, do not keep adding 'id' to the list of unique index attributes return nil if attributes.empty? attributes.map do |attribute| type = base.columns_hash[attribute.to_s].try(:type) raise "No column found for unique index '#{index}' on #{base.name}." unless type raise "Incorrect type (expected string or integer, found #{type}) for unique index '#{attribute}' on #{base.name}." unless type == :string || type == :integer UniqueIndexCache.new(base, attribute, record_store, , type) end end |
Instance Method Details
#cacheable?(query) ⇒ Boolean
Can the cache retrieve the records based on this query?
37 38 39 40 |
# File 'lib/record_cache/strategy/unique_index_cache.rb', line 37 def cacheable?(query) values = query.where_values(@attribute, @type) values && (query.limit.nil? || (query.limit == 1 && values.size == 1)) end |
#record_change(record, action) ⇒ Object
Update the version store and the record store
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/record_cache/strategy/unique_index_cache.rb', line 43 def record_change(record, action) key = cache_key(record.send(@attribute)) if action == :destroy version_store.delete(key) else # update the version store and add the record to the cache new_version = version_store.renew(key, version_opts) record_store.write(versioned_key(key, new_version), Util.serialize(record)) end end |