cached_counts 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cached_counts/cache.rb +8 -4
- data/lib/cached_counts/version.rb +1 -1
- data/spec/cached_counts/cache_spec.rb +1 -1
- data/spec/support/scope.rb +4 -4
- metadata +2 -2
data/lib/cached_counts/cache.rb
CHANGED
@@ -15,9 +15,13 @@ module CachedCounts
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
+
# Clear out any count caches which have SQL that includes the scopes table
|
18
19
|
def clear
|
19
|
-
|
20
|
-
|
20
|
+
keys = all_keys
|
21
|
+
invalid_keys = keys.select { |key| key.include?(@scope.table_name.downcase.singularize) }
|
22
|
+
invalid_keys.each { |key| Rails.cache.delete(key) }
|
23
|
+
|
24
|
+
Rails.cache.write(list_key, keys - invalid_keys)
|
21
25
|
end
|
22
26
|
|
23
27
|
private
|
@@ -27,11 +31,11 @@ module CachedCounts
|
|
27
31
|
end
|
28
32
|
|
29
33
|
def list_key
|
30
|
-
"
|
34
|
+
"cached_counts::keys"
|
31
35
|
end
|
32
36
|
|
33
37
|
def current_key
|
34
|
-
"
|
38
|
+
"cached_counts::#{@scope.to_sql.downcase}"
|
35
39
|
end
|
36
40
|
end
|
37
41
|
end
|
@@ -35,7 +35,7 @@ describe CachedCounts::Cache do
|
|
35
35
|
let(:new_scope) { Scope.new('Cat') }
|
36
36
|
let(:same_scope) { Scope.new('Cat') }
|
37
37
|
|
38
|
-
before { new_scope.stub(:to_sql => '
|
38
|
+
before { new_scope.stub(:to_sql => 'select * from cats where name = "bob"') }
|
39
39
|
|
40
40
|
it 'uses a different cache for different to_sql values' do
|
41
41
|
scope.stub(:count_without_caching => 5)
|
data/spec/support/scope.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
class Scope
|
2
|
-
attr_accessor :
|
3
|
-
def initialize(
|
4
|
-
@
|
5
|
-
@to_sql =
|
2
|
+
attr_accessor :table_name, :to_sql
|
3
|
+
def initialize(table_name)
|
4
|
+
@table_name = table_name
|
5
|
+
@to_sql = "select * from #{table_name}"
|
6
6
|
end
|
7
7
|
end
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|