jashmenn-method_cache 0.7.1.0 → 0.8.0.0
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 +17 -2
- data/VERSION.yml +3 -3
- data/lib/method_cache.rb +34 -8
- data/lib/method_cache/proxy.rb +8 -3
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -6,7 +6,7 @@ Ruby.
|
|
6
6
|
== Usage:
|
7
7
|
|
8
8
|
class Foo
|
9
|
-
extend MethodCache
|
9
|
+
extend Ifttt::MethodCache
|
10
10
|
|
11
11
|
cache_method :bar
|
12
12
|
def bar
|
@@ -35,6 +35,21 @@ Ruby.
|
|
35
35
|
|
36
36
|
gem install method_cache
|
37
37
|
|
38
|
+
== About this fork:
|
39
|
+
|
40
|
+
Forked for ifttt
|
41
|
+
|
42
|
+
=== About instances
|
43
|
+
|
44
|
+
*This is important, please make sure you understand it*
|
45
|
+
|
46
|
+
By default this method will qualify the key based on an instance hash.
|
47
|
+
|
48
|
+
That is, by default, every instance will cache to a *different* key, even given the same arguments. (This makes sense, technically all the instance variables of an object are arguments and thus the output could be different.)
|
49
|
+
|
50
|
+
If you want all instance to share a cache for a given set of arguments, first, consider making this a class method and calling that. Second, if you absolutely have to have it be an instance method, define #string_hash on your method and, if that method is consistent, all the instances will share a hash key for a given set of arguments.
|
51
|
+
|
52
|
+
|
38
53
|
== License:
|
39
54
|
|
40
|
-
Copyright (c) 2010 Justin Balthrop, Geni.com; Published under The MIT License, see LICENSE
|
55
|
+
Copyright (c) 2010 Justin Balthrop, Geni.com; Published under The MIT License, see LICENSE
|
data/VERSION.yml
CHANGED
data/lib/method_cache.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
$:.unshift(File.dirname(__FILE__))
|
2
|
+
require 'pp'
|
2
3
|
require 'method_cache/proxy'
|
3
4
|
|
5
|
+
module Eigenjoy
|
4
6
|
module MethodCache
|
5
7
|
def cache_method(method_name, opts = {})
|
6
8
|
method_name = method_name.to_sym
|
9
|
+
(opts[:version] ||= self.to_s) if self.class == Module # maybe in other cases too?
|
7
10
|
proxy = opts.kind_of?(Proxy) ? opts : Proxy.new(method_name, opts)
|
8
11
|
|
9
12
|
if self.class == Class
|
@@ -29,6 +32,9 @@ module MethodCache
|
|
29
32
|
|
30
33
|
elsif self.class == Module
|
31
34
|
# We will alias all methods when the module is mixed-in.
|
35
|
+
# include(InvalidationMethods)
|
36
|
+
# pp [:module, self, cached_module_methods]
|
37
|
+
|
32
38
|
extend(ModuleAdded) if cached_module_methods.empty?
|
33
39
|
cached_module_methods[method_name.to_sym] = proxy
|
34
40
|
end
|
@@ -73,11 +79,21 @@ module MethodCache
|
|
73
79
|
@default_cache ||= {}
|
74
80
|
end
|
75
81
|
|
82
|
+
def get_ancestors
|
83
|
+
ancestors = if self.respond_to?(:ancestors)
|
84
|
+
self.ancestors
|
85
|
+
else
|
86
|
+
self.class.ancestors
|
87
|
+
end
|
88
|
+
ancestors + extended_modules
|
89
|
+
end
|
90
|
+
|
76
91
|
def cached_instance_methods(method_name = nil)
|
77
92
|
if method_name
|
78
93
|
method_name = method_name.to_sym
|
79
|
-
|
80
|
-
next unless klass.kind_of?(MethodCache)
|
94
|
+
get_ancestors.each do |klass|
|
95
|
+
next unless klass.kind_of?(Eigenjoy::MethodCache)
|
96
|
+
# pp [:found, method_name, klass, klass.cached_instance_methods]
|
81
97
|
proxy = klass.cached_instance_methods[method_name]
|
82
98
|
return proxy if proxy
|
83
99
|
end
|
@@ -90,8 +106,8 @@ module MethodCache
|
|
90
106
|
def cached_class_methods(method_name = nil)
|
91
107
|
if method_name
|
92
108
|
method_name = method_name.to_sym
|
93
|
-
|
94
|
-
next unless klass.kind_of?(MethodCache)
|
109
|
+
get_ancestors.each do |klass|
|
110
|
+
next unless klass.kind_of?(Eigenjoy::MethodCache)
|
95
111
|
proxy = klass.cached_class_methods[method_name]
|
96
112
|
return proxy if proxy
|
97
113
|
end
|
@@ -134,16 +150,25 @@ module MethodCache
|
|
134
150
|
end
|
135
151
|
|
136
152
|
def without_method_cache(&block)
|
137
|
-
MethodCache.disable(&block)
|
153
|
+
Eigenjoy::MethodCache.disable(&block)
|
138
154
|
end
|
139
155
|
|
140
156
|
private
|
141
157
|
|
158
|
+
def extended_modules
|
159
|
+
(class << self; self end).included_modules
|
160
|
+
end
|
161
|
+
|
142
162
|
def cached_method(method_name, args)
|
163
|
+
# pp [:cached_method, method_name, self, self.get_ancestors, self.class.respond_to?(:cached_instance_methods), extended_modules]
|
143
164
|
if self.kind_of?(Class) or self.kind_of?(Module)
|
144
165
|
proxy = cached_class_methods(method_name)
|
145
166
|
else
|
146
|
-
|
167
|
+
if self.class.respond_to?(:cached_instance_methods)
|
168
|
+
proxy = self.class.send(:cached_instance_methods, method_name)
|
169
|
+
else
|
170
|
+
proxy = cached_instance_methods(method_name)
|
171
|
+
end
|
147
172
|
end
|
148
173
|
raise "method '#{method_name}' not cached" unless proxy
|
149
174
|
proxy.bind(self, args)
|
@@ -170,17 +195,18 @@ module MethodCache
|
|
170
195
|
|
171
196
|
module ModuleAdded
|
172
197
|
def extended(mod)
|
173
|
-
mod.extend(MethodCache)
|
198
|
+
mod.extend(Eigenjoy::MethodCache)
|
174
199
|
cached_module_methods.each do |method_name, proxy|
|
175
200
|
mod.cache_class_method(method_name, proxy)
|
176
201
|
end
|
177
202
|
end
|
178
203
|
|
179
204
|
def included(mod)
|
180
|
-
mod.extend(MethodCache)
|
205
|
+
mod.extend(Eigenjoy::MethodCache)
|
181
206
|
cached_module_methods.each do |method_name, proxy|
|
182
207
|
mod.cache_method(method_name, proxy)
|
183
208
|
end
|
184
209
|
end
|
185
210
|
end
|
186
211
|
end
|
212
|
+
end
|
data/lib/method_cache/proxy.rb
CHANGED
@@ -4,6 +4,7 @@ class Object
|
|
4
4
|
def metaclass; class << self; self; end; end
|
5
5
|
end
|
6
6
|
|
7
|
+
module Eigenjoy
|
7
8
|
module MethodCache
|
8
9
|
class Proxy
|
9
10
|
attr_reader :method_name, :opts, :args, :target
|
@@ -54,7 +55,7 @@ module MethodCache
|
|
54
55
|
end
|
55
56
|
|
56
57
|
def value
|
57
|
-
value = opts[:counter] ? cache.count(key) : cache[key] unless MethodCache.disabled?
|
58
|
+
value = opts[:counter] ? cache.count(key) : cache[key] unless Eigenjoy::MethodCache.disabled?
|
58
59
|
value = nil unless valid?(:load, value)
|
59
60
|
|
60
61
|
if value.nil?
|
@@ -100,7 +101,7 @@ module MethodCache
|
|
100
101
|
|
101
102
|
def cache
|
102
103
|
if @cache.nil?
|
103
|
-
@cache = opts[:cache] || MethodCache.default_cache
|
104
|
+
@cache = opts[:cache] || Eigenjoy::MethodCache.default_cache
|
104
105
|
@cache = Memcache.pool[@cache] if @cache.kind_of?(Symbol)
|
105
106
|
if not @cache.respond_to?(:[]) and @cache.respond_to?(:get)
|
106
107
|
@cache.metaclass.module_eval do
|
@@ -129,6 +130,7 @@ module MethodCache
|
|
129
130
|
@key = ['m', version, arg_string].compact.join('|')
|
130
131
|
@key = "m|#{Digest::SHA1.hexdigest(@key)}" if @key.length > 250
|
131
132
|
end
|
133
|
+
pp @key
|
132
134
|
@key
|
133
135
|
end
|
134
136
|
|
@@ -191,7 +193,7 @@ module MethodCache
|
|
191
193
|
end
|
192
194
|
|
193
195
|
def object_key(arg)
|
194
|
-
return "#{class_key(arg.class)}-#{arg.
|
196
|
+
return "#{class_key(arg.class)}-#{arg.cache_key_qualifier}" if arg.respond_to?(:cache_key_qualifier)
|
195
197
|
|
196
198
|
case arg
|
197
199
|
when NilClass then 'nil'
|
@@ -208,6 +210,8 @@ module MethodCache
|
|
208
210
|
when defined?(ActiveRecord::Base) && ActiveRecord::Base
|
209
211
|
"#{class_key(arg.class)}-#{arg.id}"
|
210
212
|
else
|
213
|
+
# such a tricky case. if you want all instances to share the same value then implement #cache_key_qualifier
|
214
|
+
# otherwise this cache is instance specific
|
211
215
|
hash = local? ? arg.hash : Marshal.dump(arg).hash
|
212
216
|
"#{class_key(arg.class)}-#{hash}"
|
213
217
|
end
|
@@ -218,3 +222,4 @@ module MethodCache
|
|
218
222
|
end
|
219
223
|
end
|
220
224
|
end
|
225
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jashmenn-method_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-22 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: Simple memcache-based memoization library for Ruby
|
15
15
|
email: code@justinbalthrop.com
|