Class: IdentityCache::Cached::Attribute Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/identity_cache/cached/attribute.rb

Overview

This class is abstract.

Direct Known Subclasses

AttributeByMulti, AttributeByOne

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, attribute_or_proc, alias_name, key_fields, unique) ⇒ Attribute

Returns a new instance of Attribute.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/identity_cache/cached/attribute.rb', line 9

def initialize(model, attribute_or_proc, alias_name, key_fields, unique)
  @model = model
  if attribute_or_proc.is_a?(Proc)
    @attribute_proc = attribute_or_proc
  else
    @attribute = attribute_or_proc.to_sym
  end
  @alias_name = alias_name.to_sym
  @key_fields = key_fields.map(&:to_sym)
  @unique = !!unique
end

Instance Attribute Details

#alias_nameObject (readonly)

Returns the value of attribute alias_name.



7
8
9
# File 'lib/identity_cache/cached/attribute.rb', line 7

def alias_name
  @alias_name
end

#key_fieldsObject (readonly)

Returns the value of attribute key_fields.



7
8
9
# File 'lib/identity_cache/cached/attribute.rb', line 7

def key_fields
  @key_fields
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/identity_cache/cached/attribute.rb', line 7

def model
  @model
end

#uniqueObject (readonly)

Returns the value of attribute unique.



7
8
9
# File 'lib/identity_cache/cached/attribute.rb', line 7

def unique
  @unique
end

Instance Method Details

#attributeObject



21
22
23
# File 'lib/identity_cache/cached/attribute.rb', line 21

def attribute
  @attribute ||= @attribute_proc.call.to_sym
end

#cache_key(index_key) ⇒ Object



50
51
52
53
# File 'lib/identity_cache/cached/attribute.rb', line 50

def cache_key(index_key)
  values_hash = IdentityCache.memcache_hash(unhashed_values_cache_key_string(index_key))
  "#{model.rails_cache_key_namespace}#{cache_key_prefix}#{values_hash}"
end

#expire(record) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/identity_cache/cached/attribute.rb', line 37

def expire(record)
  unless record.send(:was_new_record?)
    old_key = old_cache_key(record)
    IdentityCache.cache.delete(old_key)
  end
  unless record.destroyed?
    new_key = new_cache_key(record)
    if new_key != old_key
      IdentityCache.cache.delete(new_key)
    end
  end
end

#fetch(db_key) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/identity_cache/cached/attribute.rb', line 25

def fetch(db_key)
  db_key = cast_db_key(db_key)

  if model.should_use_cache?
    IdentityCache.fetch(cache_key(db_key)) do
      load_one_from_db(db_key)
    end
  else
    load_one_from_db(db_key)
  end
end

#load_one_from_db(key) ⇒ Object



55
56
57
58
59
60
# File 'lib/identity_cache/cached/attribute.rb', line 55

def load_one_from_db(key)
  query = model.reorder(nil).where(load_from_db_where_conditions(key))
  query = query.limit(1) if unique
  results = query.pluck(attribute)
  unique ? results.first : results
end