Class: IdentityCache::Cached::Attribute
Abstract
- Inherits:
-
Object
- Object
- IdentityCache::Cached::Attribute
show all
- Defined in:
- lib/identity_cache/cached/attribute.rb
Overview
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_name ⇒ Object
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_fields ⇒ Object
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
|
#model ⇒ Object
Returns the value of attribute model.
7
8
9
|
# File 'lib/identity_cache/cached/attribute.rb', line 7
def model
@model
end
|
#unique ⇒ Object
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
#attribute ⇒ Object
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
115
116
117
|
# File 'lib/identity_cache/cached/attribute.rb', line 115
def cache_encode(db_value)
db_value
end
|
#cache_key(index_key) ⇒ Object
66
67
68
69
|
# File 'lib/identity_cache/cached/attribute.rb', line 66
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
53
54
55
56
57
58
59
60
61
62
63
64
|
# 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)
if Thread.current[:idc_deferred_expiration]
Thread.current[:idc_attributes_to_expire] << old_key
all_deleted = true
else
all_deleted = IdentityCache.cache.delete(old_key)
end
end
unless record.destroyed?
new_key = new_cache_key(record)
if new_key != old_key
if Thread.current[:idc_deferred_expiration]
Thread.current[:idc_attributes_to_expire] << new_key
all_deleted = true
else
all_deleted = IdentityCache.cache.delete(new_key) && all_deleted
end
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/identity_cache/cached/attribute.rb', line 78
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)
keys.each_with_object({}) do |key, ordered_hash|
ordered_hash[key] = unordered_hash.fetch(key)
end
end
|
#load_multi_from_db(keys) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/identity_cache/cached/attribute.rb', line 94
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
71
72
73
74
75
76
|
# File 'lib/identity_cache/cached/attribute.rb', line 71
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
|