cache_method 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,6 +19,7 @@ module CacheMethod
19
19
  attr_reader :original_method_id
20
20
  attr_reader :args
21
21
 
22
+ # Store things wrapped in an Array so that nil is accepted
22
23
  def fetch
23
24
  if v = Config.instance.storage.get(cache_key) and v.is_a?(::Array)
24
25
  v[0]
@@ -34,7 +35,11 @@ module CacheMethod
34
35
  end
35
36
 
36
37
  def cache_key
37
- [ 'CacheMethod', 'CachedResult', method_signature, current_epoch, obj_hash, args_digest ].join ','
38
+ if obj.is_a? ::Class
39
+ [ 'CacheMethod', 'CachedResult', method_signature, current_epoch, args_digest ].join ','
40
+ else
41
+ [ 'CacheMethod', 'CachedResult', method_signature, obj_hash, current_epoch, args_digest ].join ','
42
+ end
38
43
  end
39
44
 
40
45
  def method_signature
@@ -42,11 +47,11 @@ module CacheMethod
42
47
  end
43
48
 
44
49
  def obj_hash
45
- @obj_hash ||= ::CacheMethod.hashcode(obj)
50
+ @obj_hash ||= obj.hash
46
51
  end
47
52
 
48
53
  def args_digest
49
- @args_digest ||= ::Digest::MD5.hexdigest(args.flatten.join)
54
+ @args_digest ||= args.empty? ? 'empty' : ::Digest::MD5.hexdigest(args.join)
50
55
  end
51
56
 
52
57
  def current_epoch
@@ -1,4 +1,3 @@
1
- require 'digest/md5'
2
1
  module CacheMethod
3
2
  class Epoch #:nodoc: all
4
3
  class << self
@@ -13,7 +12,7 @@ module CacheMethod
13
12
  end
14
13
 
15
14
  def random_name
16
- ::Digest::MD5.hexdigest rand.to_s
15
+ rand(1_000_000).to_s
17
16
  end
18
17
  end
19
18
 
@@ -31,11 +30,15 @@ module CacheMethod
31
30
  end
32
31
 
33
32
  def obj_hash
34
- @obj_hash ||= ::CacheMethod.hashcode(obj)
33
+ @obj_hash ||= obj.hash
35
34
  end
36
35
 
37
36
  def cache_key
38
- [ 'CacheMethod', 'Epoch', method_signature, obj_hash ].join ','
37
+ if obj.is_a? ::Class
38
+ [ 'CacheMethod', 'Epoch', method_signature ].join ','
39
+ else
40
+ [ 'CacheMethod', 'Epoch', method_signature, obj_hash ].join ','
41
+ end
39
42
  end
40
43
 
41
44
  def current
@@ -1,3 +1,3 @@
1
1
  module CacheMethod
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/cache_method.rb CHANGED
@@ -20,15 +20,7 @@ module CacheMethod
20
20
  def self.method_signature(obj, method_id) #:nodoc:
21
21
  [ klass_name(obj), method_id ].join method_delimiter(obj)
22
22
  end
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
-
23
+
32
24
  # All Objects, including instances and Classes, get the <tt>#clear_method_cache</tt> method.
33
25
  module InstanceMethods
34
26
  # Clear the cache for a particular method.
@@ -65,7 +57,7 @@ module CacheMethod
65
57
  original_method_id = "_uncached_#{method_id}"
66
58
  alias_method original_method_id, method_id
67
59
  define_method method_id do |*args|
68
- ::CacheMethod::CachedResult.fetch :obj => self, :method_id => method_id, :original_method_id => original_method_id, :args => args, :ttl => ttl
60
+ ::CacheMethod::CachedResult.fetch :obj => self, :method_id => method_id, :original_method_id => original_method_id, :ttl => ttl, :args => args
69
61
  end
70
62
  end
71
63
  end
data/test/helper.rb CHANGED
@@ -19,9 +19,23 @@ class CopyCat1
19
19
  def echo_count
20
20
  @echo_count ||= 0
21
21
  end
22
+ # http://www.ruby-forum.com/topic/98106
23
+ # matz: "In 1.9, values (i.e. result of splat) are always represented by array,
24
+ # so that we won't confuse array as an value with array as values
25
+ # representation."
22
26
  def echo(*args)
23
27
  self.echo_count += 1
24
- return *args
28
+ if RUBY_VERSION >= '1.9'
29
+ if args.empty?
30
+ return nil
31
+ elsif args.length == 1
32
+ return args[0]
33
+ else
34
+ return args
35
+ end
36
+ else
37
+ return *args
38
+ end
25
39
  end
26
40
  def hash
27
41
  name.hash
@@ -37,7 +51,17 @@ class CopyCat2
37
51
  end
38
52
  def echo(*args)
39
53
  self.echo_count += 1
40
- return *args
54
+ if RUBY_VERSION >= '1.9'
55
+ if args.empty?
56
+ return nil
57
+ elsif args.length == 1
58
+ return args[0]
59
+ else
60
+ return args
61
+ end
62
+ else
63
+ return *args
64
+ end
41
65
  end
42
66
  cache_method :echo
43
67
  end
@@ -6,14 +6,39 @@ class TestCacheMethod < Test::Unit::TestCase
6
6
  def setup
7
7
  Blog2.request_count = 0
8
8
  CopyCat2.echo_count = 0
9
- my_cache = Memcached.new '127.0.0.1:11211'
10
- CacheMethod.config.storage = my_cache
9
+ my_cache = Memcached.new '127.0.0.1:11211', :binary => false
11
10
  my_cache.flush
11
+ CacheMethod.config.storage = my_cache
12
12
  end
13
13
 
14
14
  def test_cache_instance_method_with_args
15
15
  a = CopyCat1.new 'mimo'
16
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
+ end
23
+
24
+ def test_cache_instance_method_with_nil_args
25
+ a = CopyCat1.new 'mimo'
26
+ assert_equal nil, a.echo
27
+ assert_equal 1, a.echo_count
28
+
29
+ assert_equal nil, a.echo
30
+ assert_equal 1, a.echo_count
31
+
32
+ assert_equal nil, a.echo(nil)
33
+ assert_equal 2, a.echo_count
34
+
35
+ assert_equal nil, a.echo(nil)
36
+ assert_equal 2, a.echo_count
37
+ end
38
+
39
+ def test_cache_instance_method_with_array_args
40
+ a = CopyCat1.new 'mimo'
41
+
17
42
  assert_equal ['hi'], a.echo(['hi'])
18
43
  assert_equal 1, a.echo_count
19
44
 
@@ -26,14 +51,43 @@ class TestCacheMethod < Test::Unit::TestCase
26
51
  assert_equal ['bye'], a.echo(['bye'])
27
52
  assert_equal 2, a.echo_count
28
53
 
29
- assert_equal nil, a.echo
54
+ assert_equal ['hi', 'there'], a.echo(['hi', 'there'])
30
55
  assert_equal 3, a.echo_count
31
56
 
32
- assert_equal nil, a.echo
57
+ # same as previous
58
+ assert_equal ['hi', 'there'], a.echo('hi', 'there')
33
59
  assert_equal 3, a.echo_count
60
+
61
+ assert_equal [], a.echo([])
62
+ assert_equal 4, a.echo_count
63
+
64
+ assert_equal [], a.echo([])
65
+ assert_equal 4, a.echo_count
34
66
  end
35
-
67
+
36
68
  def test_cache_class_method_with_args
69
+ assert_equal 'hi', CopyCat2.echo('hi')
70
+ assert_equal 1, CopyCat2.echo_count
71
+
72
+ assert_equal 'hi', CopyCat2.echo('hi')
73
+ assert_equal 1, CopyCat2.echo_count
74
+ end
75
+
76
+ def test_cache_class_method_with_nil_args
77
+ assert_equal nil, CopyCat2.echo
78
+ assert_equal 1, CopyCat2.echo_count
79
+
80
+ assert_equal nil, CopyCat2.echo
81
+ assert_equal 1, CopyCat2.echo_count
82
+
83
+ assert_equal nil, CopyCat2.echo(nil)
84
+ assert_equal 2, CopyCat2.echo_count
85
+
86
+ assert_equal nil, CopyCat2.echo(nil)
87
+ assert_equal 2, CopyCat2.echo_count
88
+ end
89
+
90
+ def test_cache_class_method_with_array_args
37
91
  assert_equal ['hi'], CopyCat2.echo(['hi'])
38
92
  assert_equal 1, CopyCat2.echo_count
39
93
 
@@ -46,13 +100,19 @@ class TestCacheMethod < Test::Unit::TestCase
46
100
  assert_equal ['bye'], CopyCat2.echo(['bye'])
47
101
  assert_equal 2, CopyCat2.echo_count
48
102
 
49
- assert_equal nil, CopyCat2.echo
103
+ assert_equal ['hi', 'there'], CopyCat2.echo(['hi', 'there'])
50
104
  assert_equal 3, CopyCat2.echo_count
51
105
 
52
- assert_equal nil, CopyCat2.echo
106
+ assert_equal ['hi', 'there'], CopyCat2.echo('hi', 'there')
53
107
  assert_equal 3, CopyCat2.echo_count
108
+
109
+ assert_equal [], CopyCat2.echo([])
110
+ assert_equal 4, CopyCat2.echo_count
111
+
112
+ assert_equal [], CopyCat2.echo([])
113
+ assert_equal 4, CopyCat2.echo_count
54
114
  end
55
-
115
+
56
116
  def test_cache_instance_method
57
117
  a = new_instance_of_my_blog
58
118
  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: 25
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 3
10
- version: 0.0.3
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Seamus Abshere