arid_cache 1.3.2 → 1.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -2
- data/Gemfile +2 -2
- data/Gemfile.lock +58 -0
- data/VERSION +1 -1
- data/arid_cache.gemspec +5 -2
- data/lib/arid_cache/cache_proxy/result_processor.rb +11 -10
- data/lib/arid_cache/cache_proxy.rb +14 -5
- data/spec/arid_cache/cache_proxy/cached_result_spec.rb +12 -7
- data/spec/arid_cache/cache_proxy/options_spec.rb +2 -2
- data/spec/arid_cache/cache_proxy/result_processor_spec.rb +9 -3
- data/spec/arid_cache/cache_proxy/utilities_spec.rb +15 -0
- data/spec/arid_cache/cache_proxy_spec.rb +228 -108
- data/spec/spec_helper.rb +1 -1
- metadata +7 -4
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
arid_cache (1.3.2)
|
5
|
+
will_paginate
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activerecord (2.3.11)
|
11
|
+
activesupport (= 2.3.11)
|
12
|
+
activesupport (2.3.11)
|
13
|
+
columnize (0.3.1)
|
14
|
+
faker (0.3.1)
|
15
|
+
gemcutter (0.6.1)
|
16
|
+
git (1.2.5)
|
17
|
+
hoe (2.6.2)
|
18
|
+
rake (>= 0.8.7)
|
19
|
+
rubyforge (>= 2.0.4)
|
20
|
+
jeweler (1.4.0)
|
21
|
+
gemcutter (>= 0.1.0)
|
22
|
+
git (>= 1.2.5)
|
23
|
+
rubyforge (>= 2.0.0)
|
24
|
+
json_pure (1.4.6)
|
25
|
+
linecache (0.43)
|
26
|
+
machinist (1.0.6)
|
27
|
+
rake (0.8.7)
|
28
|
+
rr (1.0.2)
|
29
|
+
rspec (1.3.1)
|
30
|
+
ruby-debug (0.10.3)
|
31
|
+
columnize (>= 0.1)
|
32
|
+
ruby-debug-base (~> 0.10.3.0)
|
33
|
+
ruby-debug-base (0.10.3)
|
34
|
+
linecache (>= 0.3)
|
35
|
+
rubyforge (2.0.4)
|
36
|
+
json_pure (>= 1.1.7)
|
37
|
+
sqlite3-ruby (1.3.1)
|
38
|
+
test-unit (1.2.3)
|
39
|
+
hoe (>= 1.5.1)
|
40
|
+
will_paginate (2.3.15)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
ruby
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
activerecord (= 2.3.11)
|
47
|
+
activesupport (= 2.3.11)
|
48
|
+
arid_cache!
|
49
|
+
faker (= 0.3.1)
|
50
|
+
jeweler (= 1.4.0)
|
51
|
+
machinist (= 1.0.6)
|
52
|
+
rr (~> 1.0.2)
|
53
|
+
rspec (~> 1.3.1)
|
54
|
+
ruby-debug (= 0.10.3)
|
55
|
+
ruby-debug-base (= 0.10.3)
|
56
|
+
sqlite3-ruby (= 1.3.1)
|
57
|
+
test-unit (= 1.2.3)
|
58
|
+
will_paginate (= 2.3.15)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.3.
|
1
|
+
1.3.3
|
data/arid_cache.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{arid_cache}
|
8
|
-
s.version = "1.3.
|
8
|
+
s.version = "1.3.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Karl Varga"]
|
12
|
-
s.date = %q{2011-04-
|
12
|
+
s.date = %q{2011-04-08}
|
13
13
|
s.description = %q{AridCache makes caching easy and effective. AridCache supports caching on all your model named scopes, class methods and instance methods right out of the box. AridCache prevents caching logic from cluttering your models and clarifies your logic by making explicit calls to cached result sets.
|
14
14
|
AridCache is designed for handling large, expensive ActiveRecord collections but is equally useful for caching anything else as well.
|
15
15
|
}
|
@@ -21,6 +21,7 @@ AridCache is designed for handling large, expensive ActiveRecord collections but
|
|
21
21
|
s.files = [
|
22
22
|
".gitignore",
|
23
23
|
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
24
25
|
"LICENSE",
|
25
26
|
"README.rdoc",
|
26
27
|
"Rakefile",
|
@@ -44,6 +45,7 @@ AridCache is designed for handling large, expensive ActiveRecord collections but
|
|
44
45
|
"spec/arid_cache/cache_proxy/cached_result_spec.rb",
|
45
46
|
"spec/arid_cache/cache_proxy/options_spec.rb",
|
46
47
|
"spec/arid_cache/cache_proxy/result_processor_spec.rb",
|
48
|
+
"spec/arid_cache/cache_proxy/utilities_spec.rb",
|
47
49
|
"spec/arid_cache/cache_proxy_spec.rb",
|
48
50
|
"spec/spec.opts",
|
49
51
|
"spec/spec_helper.rb",
|
@@ -74,6 +76,7 @@ AridCache is designed for handling large, expensive ActiveRecord collections but
|
|
74
76
|
"spec/arid_cache/cache_proxy/cached_result_spec.rb",
|
75
77
|
"spec/arid_cache/cache_proxy/options_spec.rb",
|
76
78
|
"spec/arid_cache/cache_proxy/result_processor_spec.rb",
|
79
|
+
"spec/arid_cache/cache_proxy/utilities_spec.rb",
|
77
80
|
"spec/arid_cache/cache_proxy_spec.rb",
|
78
81
|
"spec/spec_helper.rb",
|
79
82
|
"spec/support/ar_query.rb",
|
@@ -66,19 +66,22 @@ module AridCache
|
|
66
66
|
end
|
67
67
|
Utilities.object_class(@options[:receiver]).send(@options[:proxy], @result)
|
68
68
|
elsif is_activerecord_reflection?
|
69
|
-
lazy_cache.klass = @result.proxy_reflection.klass if @result.respond_to?(:proxy_reflection)
|
70
69
|
if @options.count_only?
|
71
70
|
lazy_cache.count = @result.count
|
72
71
|
else
|
72
|
+
lazy_cache.klass = @result.proxy_reflection.klass if @result.respond_to?(:proxy_reflection)
|
73
73
|
lazy_cache.ids = @result.collect { |r| r[:id] }
|
74
74
|
lazy_cache.count = @result.size
|
75
75
|
end
|
76
76
|
lazy_cache
|
77
|
-
elsif is_activerecord?
|
77
|
+
elsif is_activerecord?
|
78
78
|
lazy_cache.ids = @result.collect { |r| r[:id] }
|
79
79
|
lazy_cache.count = @result.size
|
80
80
|
lazy_cache.klass = @result.first.class
|
81
81
|
lazy_cache
|
82
|
+
elsif @result.nil? # so we can distinguish a cached nil vs an empty cache
|
83
|
+
lazy_cache.klass = NilClass
|
84
|
+
lazy_cache
|
82
85
|
else
|
83
86
|
@result
|
84
87
|
end
|
@@ -109,8 +112,10 @@ module AridCache
|
|
109
112
|
filtered
|
110
113
|
end
|
111
114
|
|
112
|
-
elsif @
|
115
|
+
elsif (@cached || @result).is_a?(AridCache::CacheProxy::CachedResult) && (@cached || @result).klass == NilClass
|
116
|
+
nil
|
113
117
|
|
118
|
+
elsif @options.raw?
|
114
119
|
result =
|
115
120
|
if @cached.is_a?(AridCache::CacheProxy::CachedResult)
|
116
121
|
@cached
|
@@ -144,13 +149,9 @@ module AridCache
|
|
144
149
|
end
|
145
150
|
end
|
146
151
|
|
147
|
-
# Lazy-initialize a new cached result.
|
148
|
-
# that of the receiver.
|
152
|
+
# Lazy-initialize a new cached result.
|
149
153
|
def lazy_cache
|
150
|
-
|
151
|
-
@lazy_cache = AridCache::CacheProxy::CachedResult.new
|
152
|
-
@lazy_cache.klass = @options[:result_klass]
|
153
|
-
@lazy_cache
|
154
|
+
@lazy_cache ||= AridCache::CacheProxy::CachedResult.new
|
154
155
|
end
|
155
156
|
|
156
157
|
# Return the result after processing it to apply limits or pagination in memory.
|
@@ -233,7 +234,7 @@ module AridCache
|
|
233
234
|
|
234
235
|
# Return the klass to use for building results (only applies to ActiveRecord results)
|
235
236
|
def result_klass
|
236
|
-
|
237
|
+
is_cached_result? ? @result.klass : (@cached.is_a?(AridCache::CacheProxy::CachedResult) ? @cached.klass : Utilities.object_class(@options[:receiver]))
|
237
238
|
end
|
238
239
|
end
|
239
240
|
end
|
@@ -23,7 +23,7 @@ module AridCache
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def klass
|
26
|
-
self['klass'].constantize
|
26
|
+
self[:klass].respond_to?(:constantize) ? self['klass'].constantize : self['klass']
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -71,7 +71,6 @@ module AridCache
|
|
71
71
|
@options = Options.new(@blueprint.nil? ? opts : @blueprint.opts.merge(opts))
|
72
72
|
@options[:receiver] = receiver
|
73
73
|
@cache_key = @receiver.arid_cache_key(@method, @options.opts_for_cache_key)
|
74
|
-
@cached = Rails.cache.read(@cache_key, @options.opts_for_cache)
|
75
74
|
end
|
76
75
|
|
77
76
|
#
|
@@ -99,14 +98,14 @@ module AridCache
|
|
99
98
|
# Return a ResultProcessor instance. Seed the cache if we need to, otherwise
|
100
99
|
# use what is in the cache.
|
101
100
|
def result_processor
|
102
|
-
seed_cache? ? seed_cache : ResultProcessor.new(
|
101
|
+
seed_cache? ? seed_cache : ResultProcessor.new(cached, @options)
|
103
102
|
end
|
104
103
|
|
105
104
|
# Return a boolean indicating whether we need to seed the cache. Seed the cache
|
106
105
|
# if :force => true, the cache is empty or records have been requested and there
|
107
106
|
# are none in the cache yet.
|
108
107
|
def seed_cache?
|
109
|
-
|
108
|
+
cached.nil? || @options.force? || (cached.is_a?(CachedResult) && !@options.count_only? && !cached.has_ids? && cached.klass != NilClass)
|
110
109
|
end
|
111
110
|
|
112
111
|
# Seed the cache by executing the stored block (or by calling a method on the object)
|
@@ -120,8 +119,18 @@ module AridCache
|
|
120
119
|
@result
|
121
120
|
end
|
122
121
|
|
122
|
+
# Write +data+ to the cache
|
123
123
|
def write_cache(data)
|
124
124
|
Rails.cache.write(@cache_key, data, @options.opts_for_cache)
|
125
125
|
end
|
126
|
+
|
127
|
+
# Return the contents of the cache. Read from the cache if we have not yet done so.
|
128
|
+
def cached
|
129
|
+
unless @cached_initialized
|
130
|
+
@cached = Rails.cache.read(@cache_key, @options.opts_for_cache)
|
131
|
+
@cached_initialized = true # so we don't read multiple times when the value is nil
|
132
|
+
end
|
133
|
+
@cached
|
134
|
+
end
|
126
135
|
end
|
127
|
-
end
|
136
|
+
end
|
@@ -10,12 +10,12 @@ describe AridCache::CacheProxy::CachedResult do
|
|
10
10
|
@result.klass = X
|
11
11
|
@result.klass.should be(X)
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
it "should set the klass from an object" do
|
15
15
|
@result.klass = X.new
|
16
16
|
@result.klass.should be(X)
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
it "should store the klass as a string" do
|
20
20
|
@result.klass = X
|
21
21
|
@result[:klass].should == X.name
|
@@ -25,12 +25,12 @@ describe AridCache::CacheProxy::CachedResult do
|
|
25
25
|
@result.ids = nil
|
26
26
|
@result.has_ids?.should be_false
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
it "should have ids" do
|
30
30
|
@result.ids = [1,2,3]
|
31
31
|
@result.has_ids?.should be_true
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
it "should have ids even if the array is empty" do
|
35
35
|
@result.ids = []
|
36
36
|
@result.has_ids?.should be_true
|
@@ -40,14 +40,19 @@ describe AridCache::CacheProxy::CachedResult do
|
|
40
40
|
@result.count = nil
|
41
41
|
@result.has_count?.should be_false
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
it "should have a count" do
|
45
45
|
@result.count = 3
|
46
46
|
@result.has_count?.should be_true
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
it "should have a count even if it is zero" do
|
50
50
|
@result.count = 0
|
51
51
|
@result.has_count?.should be_true
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
|
+
it "should handle initializing with a klass" do
|
55
|
+
@result = AridCache::CacheProxy::CachedResult.new([], X, 0)
|
56
|
+
@result.klass.should == X
|
57
|
+
end
|
58
|
+
end
|
@@ -86,12 +86,12 @@ describe AridCache::CacheProxy::Options do
|
|
86
86
|
|
87
87
|
describe "deprecated raw" do
|
88
88
|
it "should be deprecated" do
|
89
|
-
AridCache.
|
89
|
+
mock(AridCache).raw_with_options { false }
|
90
90
|
new_options(:raw => true).deprecated_raw?.should be_true
|
91
91
|
end
|
92
92
|
|
93
93
|
it "should not be deprecated" do
|
94
|
-
AridCache.
|
94
|
+
mock(AridCache).raw_with_options { true }
|
95
95
|
new_options(:raw => true).deprecated_raw?.should be_false
|
96
96
|
end
|
97
97
|
end
|
@@ -6,6 +6,10 @@ describe AridCache::CacheProxy::ResultProcessor do
|
|
6
6
|
AridCache::CacheProxy::ResultProcessor.new(value, opts)
|
7
7
|
end
|
8
8
|
|
9
|
+
before :all do
|
10
|
+
AridCache.store.delete! # so no options get stored and interfere with other tests
|
11
|
+
end
|
12
|
+
|
9
13
|
describe "empty array" do
|
10
14
|
before :each do
|
11
15
|
@result = new_result([])
|
@@ -18,7 +22,10 @@ describe AridCache::CacheProxy::ResultProcessor do
|
|
18
22
|
@result.is_hashes?.should be_false
|
19
23
|
@result.is_cached_result?.should be_false
|
20
24
|
@result.order_in_database?.should be_false
|
21
|
-
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should be cached as an array" do
|
28
|
+
@result.to_cache.should be_a(Array)
|
22
29
|
end
|
23
30
|
end
|
24
31
|
|
@@ -253,7 +260,6 @@ describe AridCache::CacheProxy::ResultProcessor do
|
|
253
260
|
|
254
261
|
# Comparing the hashes directly doesn't work because the updated_at Time are
|
255
262
|
# not considered equal...don't know why, cause the to_s looks the same.
|
256
|
-
# debugger
|
257
263
|
value.should be_a(Array)
|
258
264
|
value.first.should be_a(Hash)
|
259
265
|
value.first.each_pair do |k, v|
|
@@ -261,7 +267,7 @@ describe AridCache::CacheProxy::ResultProcessor do
|
|
261
267
|
end
|
262
268
|
|
263
269
|
# Cache is seeded, it should use the cached result
|
264
|
-
User.
|
270
|
+
dont_allow(User).abc
|
265
271
|
value = @user.cached_companies(:raw => true)
|
266
272
|
value.should be_a(Array)
|
267
273
|
value.first.should be_a(Hash)
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe AridCache::CacheProxy::Utilities do
|
4
|
+
describe 'order_by' do
|
5
|
+
it "id column should be prefixed by the table name" do
|
6
|
+
stub(::ActiveRecord::Base).is_mysql_adapter? { true }
|
7
|
+
AridCache::CacheProxy::Utilities.order_by([1,2,3], Company).should =~ %r[#{Company.table_name}]
|
8
|
+
end
|
9
|
+
|
10
|
+
it "id column should be prefixed by the table name" do
|
11
|
+
stub(::ActiveRecord::Base).is_mysql_adapter? { false }
|
12
|
+
AridCache::CacheProxy::Utilities.order_by([1,2,3], Company).should =~ %r[#{Company.table_name}]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,98 +1,184 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe AridCache::CacheProxy do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
4
|
+
before :all do
|
5
|
+
AridCache.store.delete! # so no options get stored
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "with raw => true" do
|
9
|
+
before :each do
|
10
|
+
@user = User.make
|
11
|
+
@user.companies << Company.make
|
12
|
+
@user.companies << Company.make
|
13
|
+
@user.clear_instance_caches
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "with options" do
|
17
|
+
before :all do
|
18
|
+
AridCache.raw_with_options = true
|
19
|
+
end
|
20
|
+
|
21
|
+
after :all do
|
22
|
+
AridCache.raw_with_options = false
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should use the new raw handling" do
|
26
|
+
AridCache.raw_with_options.should be_true
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return raw results" do
|
30
|
+
@user.cached_companies(:raw => true).should == @user.companies.collect(&:id)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "result should have the same ids as the normal result" do
|
34
|
+
@user.cached_companies(:raw => true).should == @user.cached_companies.collect(&:id)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should ignore :raw => false" do
|
38
|
+
@user.cached_companies(:raw => false).should == @user.cached_companies
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should only query once to seed the cache, ignoring all other options" do
|
42
|
+
lambda { @user.cached_companies(:raw => true, :limit => 0, :order => 'nonexistent_column desc') }.should query(1)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should apply options even if the cache has already been seeded" do
|
46
|
+
lambda {
|
47
|
+
companies = @user.cached_companies
|
48
|
+
@user.cached_companies(:raw => true, :limit => 1).should == companies.collect(&:id)[0,1]
|
49
|
+
}.should query(1)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should not use the raw option when reading from the cache" do
|
53
|
+
mock.proxy(Rails.cache).read(@user.arid_cache_key(:companies), {})
|
54
|
+
@user.cached_companies(:raw => true)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should work for calls to a method that ends with _count" do
|
58
|
+
@user.cached_bogus_count do
|
59
|
+
10
|
60
|
+
end
|
61
|
+
@user.cached_bogus_count(:raw => true).should == 10
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should work for calls to a method that ends with _count" do
|
65
|
+
@user.cached_companies_count(:raw => true).should == @user.cached_companies_count
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "deprecated" do
|
70
|
+
it "should use the deprecated handling" do
|
71
|
+
AridCache.raw_with_options.should be_false
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should return a CacheProxy::CachedResult" do
|
75
|
+
@user.cached_companies(:raw => true).should be_a(AridCache::CacheProxy::CachedResult)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "result should have the same ids as the normal result" do
|
79
|
+
@user.cached_companies(:raw => true).ids.should == @user.cached_companies.collect(&:id)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should ignore :raw => false" do
|
83
|
+
@user.cached_companies(:raw => false).should == @user.cached_companies
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should only query once to seed the cache, ignoring all other options" do
|
87
|
+
lambda { @user.cached_companies(:raw => true, :limit => 0, :order => 'nonexistent_column desc') }.should query(1)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should ignore all other options if the cache has already been seeded" do
|
91
|
+
lambda {
|
92
|
+
companies = @user.cached_companies
|
93
|
+
@user.cached_companies(:raw => true, :limit => 0, :order => 'nonexistent_column').ids.should == companies.collect(&:id)
|
94
|
+
}.should query(1)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should not use the raw option when reading from the cache" do
|
98
|
+
mock.proxy(Rails.cache).read(@user.arid_cache_key(:companies), {})
|
99
|
+
@user.cached_companies(:raw => true)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should work for calls to a method that ends with _count" do
|
103
|
+
@user.cached_bogus_count do
|
104
|
+
10
|
105
|
+
end
|
106
|
+
@user.cached_bogus_count(:raw => true).should == 10
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should work for calls to a method that ends with _count" do
|
110
|
+
@user.cached_companies_count(:raw => true).should == @user.cached_companies_count
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "with clear => true" do
|
116
|
+
before :each do
|
117
|
+
@user = User.make
|
118
|
+
@user.companies << Company.make
|
119
|
+
@user.companies << Company.make
|
120
|
+
@user.clear_instance_caches rescue Rails.cache.clear
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should not fail if there is no cached value" do
|
124
|
+
lambda { @user.cached_companies(:clear => true) }.should_not raise_exception
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should clear the cached entry" do
|
128
|
+
key = @user.arid_cache_key(:companies)
|
129
|
+
@user.cached_companies
|
130
|
+
Rails.cache.read(key).should_not be_nil
|
131
|
+
@user.cached_companies(:clear => true)
|
132
|
+
Rails.cache.read(key).should be_nil
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should not read from the cache or database" do
|
136
|
+
dont_allow(Rails.cache).read
|
137
|
+
lambda {
|
138
|
+
@user.cached_companies(:clear => true)
|
139
|
+
}.should query(0)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
describe "nils" do
|
144
|
+
before :each do
|
145
|
+
@obj = Class.new do
|
146
|
+
include AridCache
|
147
|
+
|
148
|
+
instance_caches do
|
149
|
+
empty { nil }
|
150
|
+
end
|
151
|
+
end.new
|
152
|
+
@obj.cached_empty(:clear => true)
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should cache nils" do
|
156
|
+
@obj.cached_empty.should be_nil
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should only write to the cache once" do
|
160
|
+
mock.proxy(Rails.cache).read.with_any_args.twice
|
161
|
+
mock.proxy(Rails.cache).write.with_any_args.once
|
162
|
+
@obj.cached_empty
|
163
|
+
@obj.cached_empty
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should return nil for the count" do
|
167
|
+
@obj.cached_empty_count.should be_nil
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should return nil for :raw => true" do
|
171
|
+
@obj.cached_empty(:raw => true).should be_nil
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should be cached as a CachedResult" do
|
175
|
+
@obj.cached_empty
|
176
|
+
cached = Rails.cache.read(@obj.arid_cache_key(:empty))
|
177
|
+
cached.should be_a(AridCache::CacheProxy::CachedResult)
|
178
|
+
cached.klass.should == NilClass
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
96
182
|
describe "arrays" do
|
97
183
|
before :each do
|
98
184
|
@o = Class.new do
|
@@ -103,22 +189,56 @@ describe AridCache::CacheProxy do
|
|
103
189
|
end.new
|
104
190
|
end
|
105
191
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
192
|
+
it "should cache the result" do
|
193
|
+
@o.cached_result.should == @o.result
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should respect the :limit option" do
|
197
|
+
@o.cached_result(:limit => 4).should == [1,2,3,4]
|
198
|
+
end
|
113
199
|
|
114
200
|
it "should respect the :offset option" do
|
115
|
-
@o.cached_result(:offset => 2)
|
116
|
-
end
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
201
|
+
@o.cached_result(:offset => 2).should == [3,4,5]
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should apply pagination" do
|
205
|
+
result = @o.cached_result(:page => 2, :per_page => 2)
|
206
|
+
result.should == @o.result.paginate(:page => 2, :per_page => 2)
|
207
|
+
result.total_entries.should == @o.result.size
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
describe "CachedResult" do
|
212
|
+
before :each do
|
213
|
+
class User
|
214
|
+
instance_caches do
|
215
|
+
get_result
|
216
|
+
end
|
217
|
+
|
218
|
+
def get_result
|
219
|
+
AridCache::CacheProxy::CachedResult.new(companies.collect(&:id), Company, companies.count)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
@user = User.make
|
223
|
+
@user.companies << Company.make
|
224
|
+
@user.companies << Company.make
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should store a CachedResult" do
|
228
|
+
@user.cached_get_result
|
229
|
+
Rails.cache.read(@user.arid_cache_key(:get_result)).should == @user.get_result
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should return records" do
|
233
|
+
@user.cached_get_result.should == @user.companies
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should return a count" do
|
237
|
+
@user.cached_get_result_count.should == @user.companies.count
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should return the CachedResult with :raw => true" do
|
241
|
+
@user.cached_get_result(:raw => true).should == @user.get_result
|
242
|
+
end
|
123
243
|
end
|
124
|
-
end
|
244
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -15,7 +15,7 @@ Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].e
|
|
15
15
|
|
16
16
|
Spec::Runner.configure do |config|
|
17
17
|
include ActiveRecordQueryMatchers
|
18
|
-
config.mock_with :
|
18
|
+
config.mock_with :rr
|
19
19
|
|
20
20
|
config.before(:all) do
|
21
21
|
Sham.reset(:before_all)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arid_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 1.3.
|
9
|
+
- 3
|
10
|
+
version: 1.3.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Karl Varga
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-04-
|
18
|
+
date: 2011-04-08 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -89,6 +89,7 @@ extra_rdoc_files:
|
|
89
89
|
files:
|
90
90
|
- .gitignore
|
91
91
|
- Gemfile
|
92
|
+
- Gemfile.lock
|
92
93
|
- LICENSE
|
93
94
|
- README.rdoc
|
94
95
|
- Rakefile
|
@@ -112,6 +113,7 @@ files:
|
|
112
113
|
- spec/arid_cache/cache_proxy/cached_result_spec.rb
|
113
114
|
- spec/arid_cache/cache_proxy/options_spec.rb
|
114
115
|
- spec/arid_cache/cache_proxy/result_processor_spec.rb
|
116
|
+
- spec/arid_cache/cache_proxy/utilities_spec.rb
|
115
117
|
- spec/arid_cache/cache_proxy_spec.rb
|
116
118
|
- spec/spec.opts
|
117
119
|
- spec/spec_helper.rb
|
@@ -170,6 +172,7 @@ test_files:
|
|
170
172
|
- spec/arid_cache/cache_proxy/cached_result_spec.rb
|
171
173
|
- spec/arid_cache/cache_proxy/options_spec.rb
|
172
174
|
- spec/arid_cache/cache_proxy/result_processor_spec.rb
|
175
|
+
- spec/arid_cache/cache_proxy/utilities_spec.rb
|
173
176
|
- spec/arid_cache/cache_proxy_spec.rb
|
174
177
|
- spec/spec_helper.rb
|
175
178
|
- spec/support/ar_query.rb
|