cached_counts 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cached_counts/cache.rb +12 -8
- data/lib/cached_counts/version.rb +1 -1
- data/spec/cached_counts/cache_spec.rb +12 -0
- metadata +1 -1
data/lib/cached_counts/cache.rb
CHANGED
@@ -5,14 +5,7 @@ module CachedCounts
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def count(*args)
|
8
|
-
|
9
|
-
Rails.cache.fetch(current_key)
|
10
|
-
else
|
11
|
-
@scope.count_without_caching(*args).tap do |count|
|
12
|
-
Rails.cache.write(current_key, count)
|
13
|
-
Rails.cache.write(list_key, all_keys + [current_key])
|
14
|
-
end
|
15
|
-
end
|
8
|
+
cached_count || uncached_count(*args)
|
16
9
|
end
|
17
10
|
|
18
11
|
# Clear out any count caches which have SQL that includes the scopes table
|
@@ -26,6 +19,17 @@ module CachedCounts
|
|
26
19
|
|
27
20
|
private
|
28
21
|
|
22
|
+
def cached_count
|
23
|
+
Rails.cache.fetch(current_key)
|
24
|
+
end
|
25
|
+
|
26
|
+
def uncached_count(*args)
|
27
|
+
@scope.count_without_caching(*args).tap do |count|
|
28
|
+
Rails.cache.write(current_key, count)
|
29
|
+
Rails.cache.write(list_key, all_keys + [current_key])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
29
33
|
def all_keys
|
30
34
|
Rails.cache.fetch(list_key) || []
|
31
35
|
end
|
@@ -20,6 +20,18 @@ describe CachedCounts::Cache do
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
+
context 'when the cached key is blank' do
|
24
|
+
before do
|
25
|
+
scope.stub(:count_without_caching => 5)
|
26
|
+
cache.count
|
27
|
+
Rails.cache.write(cache.send(:current_key), nil)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'returns the real count' do
|
31
|
+
cache.count.should == 5
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
23
35
|
context 'when there is already a cached count' do
|
24
36
|
before do
|
25
37
|
scope.should_receive(:count_without_caching).once.and_return(8)
|