cache_method 0.0.2 → 0.0.3
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/lib/cache_method/cached_result.rb +2 -2
- data/lib/cache_method/epoch.rb +15 -4
- data/lib/cache_method/version.rb +1 -1
- data/lib/cache_method.rb +8 -0
- data/test/helper.rb +33 -0
- data/test/test_cache_method.rb +43 -0
- metadata +4 -4
@@ -34,7 +34,7 @@ module CacheMethod
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def cache_key
|
37
|
-
[ method_signature, current_epoch, obj_hash, args_digest ].join ','
|
37
|
+
[ 'CacheMethod', 'CachedResult', method_signature, current_epoch, obj_hash, args_digest ].join ','
|
38
38
|
end
|
39
39
|
|
40
40
|
def method_signature
|
@@ -42,7 +42,7 @@ module CacheMethod
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def obj_hash
|
45
|
-
@obj_hash ||= obj
|
45
|
+
@obj_hash ||= ::CacheMethod.hashcode(obj)
|
46
46
|
end
|
47
47
|
|
48
48
|
def args_digest
|
data/lib/cache_method/epoch.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'digest/md5'
|
1
2
|
module CacheMethod
|
2
3
|
class Epoch #:nodoc: all
|
3
4
|
class << self
|
@@ -10,6 +11,10 @@ module CacheMethod
|
|
10
11
|
epoch = new options
|
11
12
|
epoch.mark_passing
|
12
13
|
end
|
14
|
+
|
15
|
+
def random_name
|
16
|
+
::Digest::MD5.hexdigest rand.to_s
|
17
|
+
end
|
13
18
|
end
|
14
19
|
|
15
20
|
def initialize(options = {})
|
@@ -26,19 +31,25 @@ module CacheMethod
|
|
26
31
|
end
|
27
32
|
|
28
33
|
def obj_hash
|
29
|
-
@obj_hash ||= obj
|
34
|
+
@obj_hash ||= ::CacheMethod.hashcode(obj)
|
30
35
|
end
|
31
36
|
|
32
37
|
def cache_key
|
33
|
-
[ 'CacheMethod', method_signature, obj_hash ].join ','
|
38
|
+
[ 'CacheMethod', 'Epoch', method_signature, obj_hash ].join ','
|
34
39
|
end
|
35
40
|
|
36
41
|
def current
|
37
|
-
Config.instance.storage.get(cache_key)
|
42
|
+
if cached_v = Config.instance.storage.get(cache_key)
|
43
|
+
cached_v
|
44
|
+
else
|
45
|
+
v = Epoch.random_name
|
46
|
+
Config.instance.storage.set cache_key, v
|
47
|
+
v
|
48
|
+
end
|
38
49
|
end
|
39
50
|
|
40
51
|
def mark_passing
|
41
|
-
Config.instance.storage.
|
52
|
+
Config.instance.storage.delete cache_key
|
42
53
|
end
|
43
54
|
end
|
44
55
|
end
|
data/lib/cache_method/version.rb
CHANGED
data/lib/cache_method.rb
CHANGED
@@ -21,6 +21,14 @@ module CacheMethod
|
|
21
21
|
[ klass_name(obj), method_id ].join method_delimiter(obj)
|
22
22
|
end
|
23
23
|
|
24
|
+
# What gets called to determine the hashcode of an object.
|
25
|
+
#
|
26
|
+
# * If the object is a Class, then it just does Class.to_s (otherwise Class hash codes change too often)
|
27
|
+
# * Otherwise, call #hash
|
28
|
+
def self.hashcode(obj)
|
29
|
+
obj.is_a?(::Class) ? obj.to_s : obj.hash
|
30
|
+
end
|
31
|
+
|
24
32
|
# All Objects, including instances and Classes, get the <tt>#clear_method_cache</tt> method.
|
25
33
|
module InstanceMethods
|
26
34
|
# Clear the cache for a particular method.
|
data/test/helper.rb
CHANGED
@@ -10,6 +10,39 @@ require 'cache_method'
|
|
10
10
|
class Test::Unit::TestCase
|
11
11
|
end
|
12
12
|
|
13
|
+
class CopyCat1
|
14
|
+
attr_reader :name
|
15
|
+
def initialize(name)
|
16
|
+
@name = name
|
17
|
+
end
|
18
|
+
attr_writer :echo_count
|
19
|
+
def echo_count
|
20
|
+
@echo_count ||= 0
|
21
|
+
end
|
22
|
+
def echo(*args)
|
23
|
+
self.echo_count += 1
|
24
|
+
return *args
|
25
|
+
end
|
26
|
+
def hash
|
27
|
+
name.hash
|
28
|
+
end
|
29
|
+
cache_method :echo
|
30
|
+
end
|
31
|
+
|
32
|
+
class CopyCat2
|
33
|
+
class << self
|
34
|
+
attr_writer :echo_count
|
35
|
+
def echo_count
|
36
|
+
@echo_count ||= 0
|
37
|
+
end
|
38
|
+
def echo(*args)
|
39
|
+
self.echo_count += 1
|
40
|
+
return *args
|
41
|
+
end
|
42
|
+
cache_method :echo
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
13
46
|
class Blog1
|
14
47
|
attr_reader :name
|
15
48
|
attr_reader :url
|
data/test/test_cache_method.rb
CHANGED
@@ -5,11 +5,54 @@ require 'memcached'
|
|
5
5
|
class TestCacheMethod < Test::Unit::TestCase
|
6
6
|
def setup
|
7
7
|
Blog2.request_count = 0
|
8
|
+
CopyCat2.echo_count = 0
|
8
9
|
my_cache = Memcached.new '127.0.0.1:11211'
|
9
10
|
CacheMethod.config.storage = my_cache
|
10
11
|
my_cache.flush
|
11
12
|
end
|
12
13
|
|
14
|
+
def test_cache_instance_method_with_args
|
15
|
+
a = CopyCat1.new 'mimo'
|
16
|
+
|
17
|
+
assert_equal ['hi'], a.echo(['hi'])
|
18
|
+
assert_equal 1, a.echo_count
|
19
|
+
|
20
|
+
assert_equal ['hi'], a.echo(['hi'])
|
21
|
+
assert_equal 1, a.echo_count
|
22
|
+
|
23
|
+
assert_equal ['bye'], a.echo(['bye'])
|
24
|
+
assert_equal 2, a.echo_count
|
25
|
+
|
26
|
+
assert_equal ['bye'], a.echo(['bye'])
|
27
|
+
assert_equal 2, a.echo_count
|
28
|
+
|
29
|
+
assert_equal nil, a.echo
|
30
|
+
assert_equal 3, a.echo_count
|
31
|
+
|
32
|
+
assert_equal nil, a.echo
|
33
|
+
assert_equal 3, a.echo_count
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_cache_class_method_with_args
|
37
|
+
assert_equal ['hi'], CopyCat2.echo(['hi'])
|
38
|
+
assert_equal 1, CopyCat2.echo_count
|
39
|
+
|
40
|
+
assert_equal ['hi'], CopyCat2.echo(['hi'])
|
41
|
+
assert_equal 1, CopyCat2.echo_count
|
42
|
+
|
43
|
+
assert_equal ['bye'], CopyCat2.echo(['bye'])
|
44
|
+
assert_equal 2, CopyCat2.echo_count
|
45
|
+
|
46
|
+
assert_equal ['bye'], CopyCat2.echo(['bye'])
|
47
|
+
assert_equal 2, CopyCat2.echo_count
|
48
|
+
|
49
|
+
assert_equal nil, CopyCat2.echo
|
50
|
+
assert_equal 3, CopyCat2.echo_count
|
51
|
+
|
52
|
+
assert_equal nil, CopyCat2.echo
|
53
|
+
assert_equal 3, CopyCat2.echo_count
|
54
|
+
end
|
55
|
+
|
13
56
|
def test_cache_instance_method
|
14
57
|
a = new_instance_of_my_blog
|
15
58
|
assert_equal ["hello from #{a.name}"], a.get_latest_entries
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_method
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Seamus Abshere
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-21 00:00:00 -06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|