11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/redis_counter_cache.rb', line 11
def redis_counter_cache(association_name, scope: nil, expiration: 1.week, after_cache: nil)
counter_method_name = _counter_method_name(association_name, scope)
recount_method_name = "update_#{counter_method_name}_cache"
redis_counter_name = "#{counter_method_name}_cache"
value redis_counter_name, expiration: expiration
define_method(counter_method_name) do
redis_counter = __send__(redis_counter_name)
if redis_counter.value
redis_counter.value.to_i
else
__send__(recount_method_name, redis_counter)
end
end
define_method(recount_method_name) do |redis_counter = __send__(redis_counter_name)|
relation = __send__(association_name)
if scope
Array(scope).each do |scope|
relation = relation.__send__(scope)
end
end
redis_counter.value = count = relation.count
after_cache.call(self) if after_cache
count
end
end
|