arid_cache 1.3.3 → 1.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.rdoc +4 -1
- data/VERSION +1 -1
- data/arid_cache.gemspec +6 -2
- data/lib/arid_cache/helpers.rb +8 -2
- data/lib/arid_cache/store.rb +35 -14
- data/spec/arid_cache/cache_proxy_spec.rb +81 -0
- data/spec/arid_cache/helpers_spec.rb +16 -0
- data/spec/arid_cache/store_spec.rb +5 -0
- metadata +8 -4
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -8,6 +8,7 @@ AridCache simplifies caching by supporting auto-expiring cache keys - as well as
|
|
8
8
|
|
9
9
|
== Changes
|
10
10
|
|
11
|
+
* v1.3.4: Inherited caches: Caches, cache options and cache blocks are inherited by all subclasses
|
11
12
|
* v1.3.2: <tt>AridCache.raw_with_options</tt> configuration for better <tt>:raw</tt> handling on cached ActiveRecord collections
|
12
13
|
* v1.3.1: Proxy support which allow you to control how your objects get serialized and unserialized
|
13
14
|
* v1.3.0: Support limits, ordering and pagination on cached Enumerables
|
@@ -40,7 +41,8 @@ Then
|
|
40
41
|
|
41
42
|
* Include the AridCache module in any Class
|
42
43
|
* <b>Rails 2 & 3 compatible</b> with automatic ActiveRecord::Base integration
|
43
|
-
*
|
44
|
+
* Auto-generated namespaced cache keys
|
45
|
+
* <b>auto-expiring cache keys</b> - your cache expires when the updated_at timestamp changes
|
44
46
|
* Supports *limits*, *ordering* & *pagination* of cached Enumerables and ActiveRecord collections
|
45
47
|
* Define caches and their options on your class using +instance_caches+ and +class_caches+
|
46
48
|
* <b>Counts for free</b> - if you have already cached the result, you get the count for free
|
@@ -50,6 +52,7 @@ Then
|
|
50
52
|
* <b>Preserves ordering</b> of your cached ActiveRecord collections
|
51
53
|
* <b>Optimized</b> to make as few cache and database accesses as absolutely neccessary
|
52
54
|
* Define your own <b>cache proxy</b> to serialize your objects as they go to and from the cache
|
55
|
+
* <b>Inherited caches</b> - subclasses inherit all caches, cache options and blocks from superclasses.
|
53
56
|
|
54
57
|
== Introduction
|
55
58
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.3.
|
1
|
+
1.3.4
|
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.4"
|
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-11}
|
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
|
}
|
@@ -47,6 +47,8 @@ AridCache is designed for handling large, expensive ActiveRecord collections but
|
|
47
47
|
"spec/arid_cache/cache_proxy/result_processor_spec.rb",
|
48
48
|
"spec/arid_cache/cache_proxy/utilities_spec.rb",
|
49
49
|
"spec/arid_cache/cache_proxy_spec.rb",
|
50
|
+
"spec/arid_cache/helpers_spec.rb",
|
51
|
+
"spec/arid_cache/store_spec.rb",
|
50
52
|
"spec/spec.opts",
|
51
53
|
"spec/spec_helper.rb",
|
52
54
|
"spec/support/ar_query.rb",
|
@@ -78,6 +80,8 @@ AridCache is designed for handling large, expensive ActiveRecord collections but
|
|
78
80
|
"spec/arid_cache/cache_proxy/result_processor_spec.rb",
|
79
81
|
"spec/arid_cache/cache_proxy/utilities_spec.rb",
|
80
82
|
"spec/arid_cache/cache_proxy_spec.rb",
|
83
|
+
"spec/arid_cache/helpers_spec.rb",
|
84
|
+
"spec/arid_cache/store_spec.rb",
|
81
85
|
"spec/spec_helper.rb",
|
82
86
|
"spec/support/ar_query.rb",
|
83
87
|
"spec/support/custom_methods.rb",
|
data/lib/arid_cache/helpers.rb
CHANGED
@@ -40,7 +40,7 @@ module AridCache
|
|
40
40
|
#
|
41
41
|
# @return an AridCache::Store::Blueprint.
|
42
42
|
def define(object, key, opts, fetch_method=:fetch, method_name=nil, &block)
|
43
|
-
|
43
|
+
|
44
44
|
# FIXME: Pass default options to store.add
|
45
45
|
# Pass nil in for now until we get the cache_ calls working.
|
46
46
|
# This means that the first time you define a dynamic cache
|
@@ -61,6 +61,12 @@ module AridCache
|
|
61
61
|
blueprint
|
62
62
|
end
|
63
63
|
|
64
|
+
def subclasses_of(parent)
|
65
|
+
result = []
|
66
|
+
ObjectSpace.each_object(Class) { |klass| result << klass if klass < parent }
|
67
|
+
result
|
68
|
+
end
|
69
|
+
|
64
70
|
private
|
65
71
|
|
66
72
|
# Dynamically define a method on the object's class to return cached results
|
@@ -82,5 +88,5 @@ module AridCache
|
|
82
88
|
object = (class << object; self; end) if object.is_a?(Class)
|
83
89
|
object.class_eval(method_body, __FILE__, __LINE__)
|
84
90
|
end
|
85
|
-
end
|
91
|
+
end
|
86
92
|
end
|
data/lib/arid_cache/store.rb
CHANGED
@@ -69,7 +69,15 @@ module AridCache
|
|
69
69
|
class ClassCacheConfiguration < Configuration; end #:nodoc:
|
70
70
|
|
71
71
|
def has?(object, key)
|
72
|
-
self.include?(object_store_key(object, key))
|
72
|
+
return true if self.include?(object_store_key(object, key))
|
73
|
+
|
74
|
+
store_key = object.is_a?(Class) ? :class_store_key : :instance_store_key
|
75
|
+
klass = object.is_a?(Class) ? object : object.class
|
76
|
+
while klass.superclass
|
77
|
+
return true if self.include?(send(store_key, klass.superclass, key))
|
78
|
+
klass = klass.superclass
|
79
|
+
end
|
80
|
+
false
|
73
81
|
end
|
74
82
|
|
75
83
|
# Empty the proc store
|
@@ -82,7 +90,7 @@ module AridCache
|
|
82
90
|
end
|
83
91
|
|
84
92
|
def find(object, key)
|
85
|
-
|
93
|
+
inherited_find(object, key)
|
86
94
|
end
|
87
95
|
|
88
96
|
def add_instance_cache_configuration(klass, key, opts, proc)
|
@@ -97,19 +105,10 @@ module AridCache
|
|
97
105
|
add_generic_cache_configuration(object_store_key(object, key), object, key, opts, proc)
|
98
106
|
end
|
99
107
|
|
100
|
-
def find_or_create(object, key)
|
101
|
-
store_key = object_store_key(object, key)
|
102
|
-
if self.include?(store_key)
|
103
|
-
self[store_key]
|
104
|
-
else
|
105
|
-
self[store_key] = AridCache::Store::Blueprint.new(object, key)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
108
|
protected
|
110
109
|
|
111
|
-
def add_generic_cache_configuration(store_key,
|
112
|
-
self[store_key] = AridCache::Store::Blueprint.new(
|
110
|
+
def add_generic_cache_configuration(store_key, object, key, opts, proc)
|
111
|
+
self[store_key] = AridCache::Store::Blueprint.new(object, key, opts, proc)
|
113
112
|
end
|
114
113
|
|
115
114
|
def initialize
|
@@ -120,5 +119,27 @@ module AridCache
|
|
120
119
|
def object_store_key(object, key)
|
121
120
|
case object; when Class; class_store_key(object, key); else; instance_store_key(object.class, key); end
|
122
121
|
end
|
122
|
+
|
123
|
+
def inherited_find(object, key)
|
124
|
+
blueprint = self[object_store_key(object, key)] || AridCache::Store::Blueprint.new(object, key)
|
125
|
+
inherit_options(blueprint, object, key)
|
126
|
+
if blueprint.opts.empty? && blueprint['proc'].nil?
|
127
|
+
nil
|
128
|
+
else
|
129
|
+
blueprint
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def inherit_options(blueprint, object, key)
|
134
|
+
klass = object.is_a?(Class) ? object : object.class
|
135
|
+
store_key = object.is_a?(Class) ? :class_store_key : :instance_store_key
|
136
|
+
while klass.superclass
|
137
|
+
if super_blueprint = self[send(store_key, klass.superclass, key)]
|
138
|
+
blueprint.opts = super_blueprint.opts.merge(blueprint.opts)
|
139
|
+
blueprint['proc'] ||= super_blueprint['proc']
|
140
|
+
end
|
141
|
+
klass = klass.superclass
|
142
|
+
end
|
143
|
+
end
|
123
144
|
end
|
124
|
-
end
|
145
|
+
end
|
@@ -241,4 +241,85 @@ describe AridCache::CacheProxy do
|
|
241
241
|
@user.cached_get_result(:raw => true).should == @user.get_result
|
242
242
|
end
|
243
243
|
end
|
244
|
+
|
245
|
+
describe "inheritance" do
|
246
|
+
before :each do
|
247
|
+
class Abc
|
248
|
+
include AridCache
|
249
|
+
instance_caches do
|
250
|
+
name(:limit => 2, :expires_in => 1) { 'abc' }
|
251
|
+
end
|
252
|
+
|
253
|
+
def inherited_method
|
254
|
+
'inherited'
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
class Def < Abc
|
259
|
+
instance_caches do
|
260
|
+
name(:limit => 1, :expires_in => 2) { 'def' }
|
261
|
+
call_name { name }
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
class Xyz < Def
|
266
|
+
instance_caches do
|
267
|
+
name(:limit => 3) # should use the block from the superclass
|
268
|
+
end
|
269
|
+
|
270
|
+
def name
|
271
|
+
'xyz'
|
272
|
+
end
|
273
|
+
end
|
274
|
+
@abc = Abc.new
|
275
|
+
@def = Def.new
|
276
|
+
@xyz = Xyz.new
|
277
|
+
end
|
278
|
+
|
279
|
+
it "should inherit procs and options from subclasses" do
|
280
|
+
@abc.cached_name.should == 'ab'
|
281
|
+
@def.cached_name.should == 'd'
|
282
|
+
@xyz.cached_name.should == 'def'
|
283
|
+
end
|
284
|
+
|
285
|
+
it "should inherit options" do
|
286
|
+
AridCache.store.find(@xyz, 'name').opts[:expires_in].should == 2
|
287
|
+
end
|
288
|
+
|
289
|
+
it "should inherit caches" do
|
290
|
+
@xyz.should respond_to(:cached_call_name)
|
291
|
+
end
|
292
|
+
|
293
|
+
it "should evaluate inherited caches in the right instance" do
|
294
|
+
@xyz.cached_call_name.should == 'xyz'
|
295
|
+
lambda {
|
296
|
+
@def.cached_call_name
|
297
|
+
}.should raise_error(NameError)
|
298
|
+
end
|
299
|
+
|
300
|
+
it "should inherit methods" do
|
301
|
+
@abc.cached_inherited_method.should == 'inherited'
|
302
|
+
@def.cached_inherited_method.should == 'inherited'
|
303
|
+
@xyz.cached_inherited_method.should == 'inherited'
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
# describe "reserved names" do
|
308
|
+
# before :each do
|
309
|
+
# @obj = Class.new do
|
310
|
+
# include AridCache
|
311
|
+
# instance_caches do
|
312
|
+
# debugger
|
313
|
+
# test(:expires_in => 1)
|
314
|
+
# end
|
315
|
+
# def test
|
316
|
+
# true
|
317
|
+
# end
|
318
|
+
# end.new
|
319
|
+
# end
|
320
|
+
#
|
321
|
+
# it "should support a cache named test" do
|
322
|
+
# @obj.cached_test
|
323
|
+
# end
|
324
|
+
# end
|
244
325
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AridCache::Helpers do
|
4
|
+
describe "subclasses_of" do
|
5
|
+
before :each do
|
6
|
+
class Aa; end
|
7
|
+
class Bb < Aa; end
|
8
|
+
class Cc < Bb; end
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should description" do
|
12
|
+
AridCache.subclasses_of(Aa).should == [Cc, Bb]
|
13
|
+
AridCache.subclasses_of(Bb).should == [Cc]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
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: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 1.3.
|
9
|
+
- 4
|
10
|
+
version: 1.3.4
|
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-11 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -115,6 +115,8 @@ files:
|
|
115
115
|
- spec/arid_cache/cache_proxy/result_processor_spec.rb
|
116
116
|
- spec/arid_cache/cache_proxy/utilities_spec.rb
|
117
117
|
- spec/arid_cache/cache_proxy_spec.rb
|
118
|
+
- spec/arid_cache/helpers_spec.rb
|
119
|
+
- spec/arid_cache/store_spec.rb
|
118
120
|
- spec/spec.opts
|
119
121
|
- spec/spec_helper.rb
|
120
122
|
- spec/support/ar_query.rb
|
@@ -174,6 +176,8 @@ test_files:
|
|
174
176
|
- spec/arid_cache/cache_proxy/result_processor_spec.rb
|
175
177
|
- spec/arid_cache/cache_proxy/utilities_spec.rb
|
176
178
|
- spec/arid_cache/cache_proxy_spec.rb
|
179
|
+
- spec/arid_cache/helpers_spec.rb
|
180
|
+
- spec/arid_cache/store_spec.rb
|
177
181
|
- spec/spec_helper.rb
|
178
182
|
- spec/support/ar_query.rb
|
179
183
|
- spec/support/custom_methods.rb
|