cache_method 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +6 -0
- data/lib/cache_method/cached_result.rb +22 -6
- data/lib/cache_method/version.rb +1 -1
- data/test/helper.rb +30 -2
- data/test/test_cache_method.rb +34 -0
- metadata +3 -3
data/CHANGELOG
CHANGED
@@ -1,6 +1,26 @@
|
|
1
1
|
require 'digest/sha1'
|
2
2
|
module CacheMethod
|
3
3
|
class CachedResult #:nodoc: all
|
4
|
+
class << self
|
5
|
+
def resolve_cache_key(obj)
|
6
|
+
case obj
|
7
|
+
when ::Array
|
8
|
+
obj.map do |v|
|
9
|
+
resolve_cache_key v
|
10
|
+
end
|
11
|
+
when ::Hash
|
12
|
+
obj.inject({}) do |memo, (k, v)|
|
13
|
+
kk = resolve_cache_key k
|
14
|
+
vv = resolve_cache_key v
|
15
|
+
memo[kk] = vv
|
16
|
+
memo
|
17
|
+
end
|
18
|
+
else
|
19
|
+
obj.respond_to?(:as_cache_key) ? obj.as_cache_key : obj
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
4
24
|
CACHE_KEY_JOINER = ','
|
5
25
|
|
6
26
|
def initialize(obj, method_id, original_method_id, ttl, args)
|
@@ -44,11 +64,11 @@ module CacheMethod
|
|
44
64
|
end
|
45
65
|
|
46
66
|
def obj_digest
|
47
|
-
@obj_digest ||= ::Digest::SHA1.hexdigest(::Marshal.dump(
|
67
|
+
@obj_digest ||= ::Digest::SHA1.hexdigest(::Marshal.dump(CachedResult.resolve_cache_key(obj)))
|
48
68
|
end
|
49
69
|
|
50
70
|
def args_digest
|
51
|
-
@args_digest ||= args.empty? ? 'empty' : ::Digest::SHA1.hexdigest(::Marshal.dump(args))
|
71
|
+
@args_digest ||= args.empty? ? 'empty' : ::Digest::SHA1.hexdigest(::Marshal.dump(CachedResult.resolve_cache_key(args)))
|
52
72
|
end
|
53
73
|
|
54
74
|
def current_generation
|
@@ -56,9 +76,5 @@ module CacheMethod
|
|
56
76
|
@current_generation ||= Generation.new(obj, method_id).current
|
57
77
|
end
|
58
78
|
end
|
59
|
-
|
60
|
-
def arity
|
61
|
-
@arity ||= obj.method(original_method_id).arity
|
62
|
-
end
|
63
79
|
end
|
64
80
|
end
|
data/lib/cache_method/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -2,7 +2,11 @@ require 'rubygems'
|
|
2
2
|
require 'bundler'
|
3
3
|
Bundler.setup
|
4
4
|
require 'test/unit'
|
5
|
-
|
5
|
+
|
6
|
+
if ::Bundler.definition.specs['ruby-debug19'].first or ::Bundler.definition.specs['ruby-debug'].first
|
7
|
+
require 'ruby-debug'
|
8
|
+
end
|
9
|
+
|
6
10
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
11
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
8
12
|
require 'cache_method'
|
@@ -27,6 +31,14 @@ class CopyCat1
|
|
27
31
|
self.echo_count += 1
|
28
32
|
return *args
|
29
33
|
end
|
34
|
+
attr_writer :ack_count
|
35
|
+
def ack_count
|
36
|
+
@ack_count ||= 0
|
37
|
+
end
|
38
|
+
def ack(*args)
|
39
|
+
self.ack_count += 1
|
40
|
+
'ACK'
|
41
|
+
end
|
30
42
|
def marshal_dump
|
31
43
|
raise "Used marshal_dump"
|
32
44
|
end
|
@@ -34,6 +46,7 @@ class CopyCat1
|
|
34
46
|
name
|
35
47
|
end
|
36
48
|
cache_method :echo
|
49
|
+
cache_method :ack
|
37
50
|
end
|
38
51
|
|
39
52
|
class CopyCat1a < CopyCat1
|
@@ -151,6 +164,21 @@ CopyCat1.send :include, Say
|
|
151
164
|
|
152
165
|
class DontStringifyMe < Struct.new(:message)
|
153
166
|
def to_s
|
154
|
-
"Don't stringify me, read my message!"
|
167
|
+
raise "Don't stringify me, read my message!"
|
168
|
+
end
|
169
|
+
alias :inspect :to_s
|
170
|
+
end
|
171
|
+
|
172
|
+
class DontMarshalMe < Struct.new(:message)
|
173
|
+
def marshal_dump
|
174
|
+
raise "Don't marshal dump me!"
|
175
|
+
end
|
176
|
+
|
177
|
+
def marshal_load(src)
|
178
|
+
raise "Don't marshal load me! Maybe I'm a HASH WITH A DEFAULT PROC!"
|
179
|
+
end
|
180
|
+
|
181
|
+
def as_cache_key
|
182
|
+
message
|
155
183
|
end
|
156
184
|
end
|
data/test/test_cache_method.rb
CHANGED
@@ -402,4 +402,38 @@ class TestCacheMethod < Test::Unit::TestCase
|
|
402
402
|
a.echo(world)
|
403
403
|
assert_equal 2, a.echo_count
|
404
404
|
end
|
405
|
+
|
406
|
+
def test_doesnt_marshal_if_as_cache_key_defined
|
407
|
+
hello = DontMarshalMe.new("hello")
|
408
|
+
world = DontMarshalMe.new("world")
|
409
|
+
|
410
|
+
a = CopyCat1.new 'mimo'
|
411
|
+
ack_count = 0
|
412
|
+
|
413
|
+
[
|
414
|
+
hello,
|
415
|
+
world,
|
416
|
+
[hello],
|
417
|
+
[world],
|
418
|
+
[[hello]],
|
419
|
+
[[world]],
|
420
|
+
{ hello => world },
|
421
|
+
{ world => hello },
|
422
|
+
[{hello => world}],
|
423
|
+
[{world => hello}],
|
424
|
+
[[{hello => world}]],
|
425
|
+
[[{world => hello}]],
|
426
|
+
{hello => [{hello => world}]},
|
427
|
+
{world => [{hello => world}]},
|
428
|
+
].each do |nasty|
|
429
|
+
ack_count += 1
|
430
|
+
5.times do
|
431
|
+
a.ack(nasty)
|
432
|
+
assert_equal ack_count, a.ack_count
|
433
|
+
|
434
|
+
a.ack(nasty)
|
435
|
+
assert_equal ack_count, a.ack_count
|
436
|
+
end
|
437
|
+
end
|
438
|
+
end
|
405
439
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_method
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-03-01 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cache
|
16
|
-
requirement: &
|
16
|
+
requirement: &2153251040 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 0.2.1
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2153251040
|
25
25
|
description: Like alias_method, but it's cache_method!
|
26
26
|
email:
|
27
27
|
- seamus@abshere.net
|