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_encode(db_value) ⇒ Object Also known as: cache_decode



103
104
105
# File 'lib/identity_cache/cached/attribute.rb', line 103

def cache_encode(db_value)
  db_value
end

#cache_key(index_key) ⇒ Object



54
55
56
57
# File 'lib/identity_cache/cached/attribute.rb', line 54

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
49
50
51
52
# File 'lib/identity_cache/cached/attribute.rb', line 37

def expire(record)
  all_deleted = true

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

  all_deleted
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

#fetch_multi(keys) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/identity_cache/cached/attribute.rb', line 66

def fetch_multi(keys)
  keys = keys.map { |key| cast_db_key(key) }

  unless model.should_use_cache?
    return load_multi_from_db(keys)
  end

  unordered_hash = CacheKeyLoader.load_multi(self, keys)

  # Calling `values` on the result is expected to return the values in the same order as their
  # corresponding keys. The fetch_multi_by_#{field_list} generated methods depend on this.
  keys.each_with_object({}) do |key, ordered_hash|
    ordered_hash[key] = unordered_hash.fetch(key)
  end
end

#load_multi_from_db(keys) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/identity_cache/cached/attribute.rb', line 82

def load_multi_from_db(keys)
  result = {}
  return result if keys.empty?

  rows = load_multi_rows(keys)
  default = unique ? nil : []
  keys.each do |index_value|
    result[index_value] = default.try!(:dup)
  end
  if unique
    rows.each do |index_value, attribute_value|
      result[index_value] = attribute_value
    end
  else
    rows.each do |index_value, attribute_value|
      result[index_value] << attribute_value
    end
  end
  result
end

#load_one_from_db(key) ⇒ Object



59
60
61
62
63
64
# File 'lib/identity_cache/cached/attribute.rb', line 59

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