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.
@@ -5,14 +5,7 @@ module CachedCounts
5
5
  end
6
6
 
7
7
  def count(*args)
8
- if all_keys.include?(current_key)
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
@@ -1,3 +1,3 @@
1
1
  module CachedCounts
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cached_counts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: