cache_method 0.1.4 → 0.1.5
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/README.rdoc +9 -6
- data/cache_method.gemspec +1 -0
- data/lib/cache_method/cached_result.rb +2 -2
- data/lib/cache_method/epoch.rb +2 -2
- data/lib/cache_method/version.rb +1 -1
- data/lib/cache_method.rb +3 -2
- data/test/helper.rb +29 -0
- data/test/test_cache_method.rb +20 -5
- metadata +20 -6
data/README.rdoc
CHANGED
@@ -40,6 +40,10 @@ And clear them too
|
|
40
40
|
|
41
41
|
(which doesn't delete the rest of your cache)
|
42
42
|
|
43
|
+
== Real-world usage
|
44
|
+
|
45
|
+
In production use at {carbon.brighterplanet.com}[http://carbon.brighterplanet.com], the Brighter Planet emission estimate web service.
|
46
|
+
|
43
47
|
== Configuration (and supported cache clients)
|
44
48
|
|
45
49
|
You need to set where the cache will be stored:
|
@@ -78,14 +82,13 @@ Ideally, you should try to make a <tt>String</tt> or a <tt>Hash</tt> and call th
|
|
78
82
|
|
79
83
|
Note: this is NOT the same thing as <tt>#to_hash</tt>! That returns a <b><tt>Hash</tt></b>. What we want is an integer "hash code."
|
80
84
|
|
85
|
+
== Using #method_cache_hash instead of #hash
|
86
|
+
|
87
|
+
If you don't want to modify #hash, use #method_cache_hash instead.
|
88
|
+
|
81
89
|
== Rationale
|
82
90
|
|
83
|
-
* It should be easy to cache a method using memcached.
|
84
|
-
* The main clients should be supported
|
85
|
-
* memcache-client (for people who use the Rails default)
|
86
|
-
* dalli (for people on heroku)
|
87
|
-
* memcached (for people using Evan Weaver's ultra-fast gem)
|
88
|
-
* redis (for people who like that sort of thing, but you won't get expiration)
|
91
|
+
* It should be easy to cache a method using memcached, dalli (if you're on heroku), redis, etc. (that's why I made the {cache gem}[https://rubygems.org/gems/cache])
|
89
92
|
* It should be easy to uncache a method without clearing the whole cache
|
90
93
|
* It should be easy to cache instance methods
|
91
94
|
* It should be easy to cache methods that depend on object state
|
data/cache_method.gemspec
CHANGED
@@ -35,7 +35,7 @@ module CacheMethod
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def cache_key
|
38
|
-
if obj.is_a? ::Class
|
38
|
+
if obj.is_a? ::Class or obj.is_a? ::Module
|
39
39
|
[ 'CacheMethod', 'CachedResult', method_signature, current_epoch, args_digest ].join ','
|
40
40
|
else
|
41
41
|
[ 'CacheMethod', 'CachedResult', method_signature, obj_hash, current_epoch, args_digest ].join ','
|
@@ -47,7 +47,7 @@ module CacheMethod
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def obj_hash
|
50
|
-
@obj_hash ||= obj.hash
|
50
|
+
@obj_hash ||= obj.respond_to?(:method_cache_hash) ? obj.method_cache_hash : obj.hash
|
51
51
|
end
|
52
52
|
|
53
53
|
def args_digest
|
data/lib/cache_method/epoch.rb
CHANGED
@@ -30,11 +30,11 @@ module CacheMethod
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def obj_hash
|
33
|
-
@obj_hash ||= obj.hash
|
33
|
+
@obj_hash ||= obj.respond_to?(:method_cache_hash) ? obj.method_cache_hash : obj.hash
|
34
34
|
end
|
35
35
|
|
36
36
|
def cache_key
|
37
|
-
if obj.is_a? ::Class
|
37
|
+
if obj.is_a? ::Class or obj.is_a? ::Module
|
38
38
|
[ 'CacheMethod', 'Epoch', method_signature ].join ','
|
39
39
|
else
|
40
40
|
[ 'CacheMethod', 'Epoch', method_signature, obj_hash ].join ','
|
data/lib/cache_method/version.rb
CHANGED
data/lib/cache_method.rb
CHANGED
@@ -10,11 +10,11 @@ module CacheMethod
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def self.klass_name(obj) #:nodoc:
|
13
|
-
obj.is_a?(::Class) ? obj.to_s : obj.class.to_s
|
13
|
+
(obj.is_a?(::Class) or obj.is_a?(::Module)) ? obj.to_s : obj.class.to_s
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.method_delimiter(obj) #:nodoc:
|
17
|
-
obj.is_a?(::Class) ? '.' : '#'
|
17
|
+
(obj.is_a?(::Class) or obj.is_a?(::Module)) ? '.' : '#'
|
18
18
|
end
|
19
19
|
|
20
20
|
def self.method_signature(obj, method_id) #:nodoc:
|
@@ -66,4 +66,5 @@ end
|
|
66
66
|
unless ::Object.method_defined? :cache_method
|
67
67
|
::Object.send :include, ::CacheMethod::InstanceMethods
|
68
68
|
::Class.send :include, ::CacheMethod::ClassMethods
|
69
|
+
::Module.send :include, ::CacheMethod::ClassMethods
|
69
70
|
end
|
data/test/helper.rb
CHANGED
@@ -38,11 +38,20 @@ class CopyCat1
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
def hash
|
41
|
+
raise "Used hash"
|
42
|
+
end
|
43
|
+
def method_cache_hash
|
41
44
|
name.hash
|
42
45
|
end
|
43
46
|
cache_method :echo
|
44
47
|
end
|
45
48
|
|
49
|
+
class CopyCat1a < CopyCat1
|
50
|
+
def method_cache_hash
|
51
|
+
raise "Used method_cache_hash"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
46
55
|
class CopyCat2
|
47
56
|
class << self
|
48
57
|
attr_writer :echo_count
|
@@ -112,3 +121,23 @@ class Blog2
|
|
112
121
|
cache_method :get_latest_entries
|
113
122
|
end
|
114
123
|
end
|
124
|
+
|
125
|
+
require 'active_support'
|
126
|
+
require 'active_support/core_ext/module'
|
127
|
+
module BlogM
|
128
|
+
# mattr_accessor :request_count
|
129
|
+
# self.request_count = 0
|
130
|
+
def self.request_count
|
131
|
+
@request_count ||= 0
|
132
|
+
end
|
133
|
+
def self.request_count=(x)
|
134
|
+
@request_count = x
|
135
|
+
end
|
136
|
+
def self.get_latest_entries
|
137
|
+
self.request_count += 1
|
138
|
+
'danke schoen'
|
139
|
+
end
|
140
|
+
class << self
|
141
|
+
cache_method :get_latest_entries
|
142
|
+
end
|
143
|
+
end
|
data/test/test_cache_method.rb
CHANGED
@@ -20,7 +20,7 @@ class TestCacheMethod < Test::Unit::TestCase
|
|
20
20
|
assert_equal 'hi', a.echo('hi')
|
21
21
|
assert_equal 1, a.echo_count
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
def test_cache_instance_method_with_nil_args
|
25
25
|
a = CopyCat1.new 'mimo'
|
26
26
|
assert_equal nil, a.echo
|
@@ -31,11 +31,11 @@ class TestCacheMethod < Test::Unit::TestCase
|
|
31
31
|
|
32
32
|
assert_equal nil, a.echo(nil)
|
33
33
|
assert_equal 2, a.echo_count
|
34
|
-
|
34
|
+
|
35
35
|
assert_equal nil, a.echo(nil)
|
36
36
|
assert_equal 2, a.echo_count
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
def test_cache_instance_method_with_array_args
|
40
40
|
a = CopyCat1.new 'mimo'
|
41
41
|
|
@@ -64,7 +64,7 @@ class TestCacheMethod < Test::Unit::TestCase
|
|
64
64
|
assert_equal [], a.echo([])
|
65
65
|
assert_equal 4, a.echo_count
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
def test_cache_class_method_with_args
|
69
69
|
assert_equal 'hi', CopyCat2.echo('hi')
|
70
70
|
assert_equal 1, CopyCat2.echo_count
|
@@ -82,7 +82,7 @@ class TestCacheMethod < Test::Unit::TestCase
|
|
82
82
|
|
83
83
|
assert_equal nil, CopyCat2.echo(nil)
|
84
84
|
assert_equal 2, CopyCat2.echo_count
|
85
|
-
|
85
|
+
|
86
86
|
assert_equal nil, CopyCat2.echo(nil)
|
87
87
|
assert_equal 2, CopyCat2.echo_count
|
88
88
|
end
|
@@ -218,4 +218,19 @@ class TestCacheMethod < Test::Unit::TestCase
|
|
218
218
|
assert_equal 'hi', a.echo('hi')
|
219
219
|
assert_equal 1, a.echo_count
|
220
220
|
end
|
221
|
+
|
222
|
+
def test_cache_module_method
|
223
|
+
assert_equal 0, BlogM.request_count
|
224
|
+
assert_equal 'danke schoen', BlogM.get_latest_entries
|
225
|
+
assert_equal 1, BlogM.request_count
|
226
|
+
assert_equal 'danke schoen', BlogM.get_latest_entries
|
227
|
+
assert_equal 1, BlogM.request_count
|
228
|
+
end
|
229
|
+
|
230
|
+
def test_method_cache_hash
|
231
|
+
assert_raises(RuntimeError, /Used method_cache_hash/) do
|
232
|
+
a = CopyCat1a.new 'mimo'
|
233
|
+
a.echo 'hi'
|
234
|
+
end
|
235
|
+
end
|
221
236
|
end
|
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:
|
5
|
-
prerelease:
|
4
|
+
hash: 17
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 5
|
10
|
+
version: 0.1.5
|
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-03-
|
18
|
+
date: 2011-03-12 23:00:00 -06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -48,6 +48,20 @@ dependencies:
|
|
48
48
|
version: "0"
|
49
49
|
type: :development
|
50
50
|
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: activesupport
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
51
65
|
description: Like alias_method, but it's cache_method!
|
52
66
|
email:
|
53
67
|
- seamus@abshere.net
|
@@ -100,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
114
|
requirements: []
|
101
115
|
|
102
116
|
rubyforge_project: cache_method
|
103
|
-
rubygems_version: 1.
|
117
|
+
rubygems_version: 1.6.2
|
104
118
|
signing_key:
|
105
119
|
specification_version: 3
|
106
120
|
summary: Lets you cache methods (to memcached, redis, etc.) sort of like you can memoize them
|