cache_method 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,8 +1,15 @@
1
+ 0.2.3 / 2012-04-06
2
+
3
+ * Enhancements
4
+
5
+ * Make it possible to work with proxies by defining #to_cache_key which---unlike #as_cache_key---does not cause #class to be run
6
+
1
7
  0.2.2 / 2012-03-02
2
8
 
3
9
  * Enhancements
4
10
 
5
11
  * Class name is now force-prepended to the result of #as_cache_key. This prevents objects of different classes that have the same #as_cache_key from being confused with each other. In any case, this sort of duck typing (?) doesn't seem appropriate for cache keys.
12
+ * Enable "debug mode" by requiring "cache_method/debug"... it helps you find cache keys that are suspiciously long (and therefore slow to digest).
6
13
 
7
14
  0.2.1 / 2012-03-01
8
15
 
data/README.markdown CHANGED
@@ -54,8 +54,6 @@ If you're caching methods ActiveRecord objects (aka instances of `ActiveRecord::
54
54
  end
55
55
  end
56
56
 
57
- Otherwise the full object will be marshal dumped **just to get a cache key**.
58
-
59
57
  ## Debug
60
58
 
61
59
  CacheMethod can warn you if your obj or args cache keys are suspiciously long.
@@ -81,7 +79,7 @@ or this might even work...
81
79
 
82
80
  See `Config` for the full list of supported caches.
83
81
 
84
- == Defining a #as_cache_key method
82
+ ## Defining a #as_cache_key method
85
83
 
86
84
  Since we're not pure functional programmers, sometimes cache hits depend on object state in addition to method arguments. To illustrate:
87
85
 
@@ -99,6 +97,25 @@ get_latest_entries doesn't take any arguments, so it must depend on my_blog.url
99
97
 
100
98
  If you don't define `#as_cache_key`, then `cache_method` will `Marshal.dump` an instance.
101
99
 
100
+ ## Danger: #to_cache_key
101
+
102
+ Let's say you want need cache keys from classes that undefine the `#class` method (how annoying). You can define `#to_cache_key`, but **make sure it identifies the class too!** (in addition to the instance.)
103
+
104
+ For example, if you find yourself passing association proxies as arguments to cached methods, this might be helpful:
105
+
106
+ user = User.first
107
+ Foo.bar(user.groups) # you're passing an association as an argument
108
+
109
+ class ActiveRecord::Associations::AssociationCollection
110
+ # danger! this is a special case... try to use #as_cache_key instead
111
+ # also note that this example is based on ActiveRecord 3.0.10 ... YMMV
112
+ def to_cache_key
113
+ [ proxy_owner.class.name, proxy_owner.id, proxy_reflection.name, conditions ].join('/')
114
+ end
115
+ end
116
+
117
+ Otherwise, `cache_method` will try to run `user.groups.class` which causes the proxy to be loaded. You probably don't want to load 1000 AR objects just to generate a cache key.
118
+
102
119
  ## Module methods
103
120
 
104
121
  You can put `#cache_method` right into your module declarations:
@@ -16,7 +16,14 @@ module CacheMethod
16
16
  memo
17
17
  end
18
18
  else
19
- obj.respond_to?(:as_cache_key) ? [obj.class.name, obj.as_cache_key] : obj
19
+ if obj.respond_to?(:to_cache_key)
20
+ # this is meant to be used sparingly, usually when a proxy class is involved
21
+ obj.to_cache_key
22
+ elsif obj.respond_to?(:as_cache_key)
23
+ [obj.class.name, obj.as_cache_key]
24
+ else
25
+ obj
26
+ end
20
27
  end
21
28
  end
22
29
  end
@@ -1,3 +1,3 @@
1
1
  module CacheMethod
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
data/test/helper.rb CHANGED
@@ -55,6 +55,15 @@ class CopyCat1a < CopyCat1
55
55
  end
56
56
  end
57
57
 
58
+ class CopyCat1b < CopyCat1
59
+ def as_cache_key
60
+ raise "Used as_cache_key"
61
+ end
62
+ def to_cache_key
63
+ raise "Used to_cache_key"
64
+ end
65
+ end
66
+
58
67
  module Say
59
68
  def say_count
60
69
  @say_count ||= 0
@@ -317,6 +317,13 @@ class TestCacheMethod < Test::Unit::TestCase
317
317
  end
318
318
  end
319
319
 
320
+ def test_to_cache_key
321
+ assert_raises(RuntimeError, /Used to_cache_key/) do
322
+ a = CopyCat1b.new 'mimo'
323
+ a.echo 'hi'
324
+ end
325
+ end
326
+
320
327
  def test_method_added_by_extension
321
328
  assert_equal 'hi', CopyCat2.say('hi')
322
329
  assert_equal 1, CopyCat2.say_count
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.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-02 00:00:00.000000000 Z
12
+ date: 2012-04-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cache
16
- requirement: &2152318920 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,12 @@ dependencies:
21
21
  version: 0.2.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152318920
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.2.1
25
30
  description: Like alias_method, but it's cache_method!
26
31
  email:
27
32
  - seamus@abshere.net
@@ -63,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
68
  version: '0'
64
69
  requirements: []
65
70
  rubyforge_project: cache_method
66
- rubygems_version: 1.8.15
71
+ rubygems_version: 1.8.21
67
72
  signing_key:
68
73
  specification_version: 3
69
74
  summary: Lets you cache methods (to memcached, redis, etc.) sort of like you can memoize