cache_method 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ 0.2.2 / 2012-03-02
2
+
3
+ * Enhancements
4
+
5
+ * 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.
6
+
1
7
  0.2.1 / 2012-03-01
2
8
 
3
9
  * Enhancements
data/README.markdown CHANGED
@@ -44,6 +44,27 @@ And clear them too
44
44
 
45
45
  (which doesn't delete the rest of your cache)
46
46
 
47
+ ## ActiveRecord
48
+
49
+ If you're caching methods ActiveRecord objects (aka instances of `ActiveRecord::Base`), then you should probably define something like:
50
+
51
+ class ActiveRecord::Base
52
+ def as_cache_key
53
+ attributes
54
+ end
55
+ end
56
+
57
+ Otherwise the full object will be marshal dumped **just to get a cache key**.
58
+
59
+ ## Debug
60
+
61
+ CacheMethod can warn you if your obj or args cache keys are suspiciously long.
62
+
63
+ require 'cache_method'
64
+ require 'cache_method/debug'
65
+
66
+ Then watch your logs.
67
+
47
68
  ## Configuration (and supported cache clients)
48
69
 
49
70
  You need to set where the cache will be stored:
@@ -16,7 +16,7 @@ module CacheMethod
16
16
  memo
17
17
  end
18
18
  else
19
- obj.respond_to?(:as_cache_key) ? obj.as_cache_key : obj
19
+ obj.respond_to?(:as_cache_key) ? [obj.class.name, obj.as_cache_key] : obj
20
20
  end
21
21
  end
22
22
  end
@@ -46,6 +46,10 @@ module CacheMethod
46
46
  v
47
47
  end
48
48
  end
49
+
50
+ def exist?
51
+ Config.instance.storage.exist?(cache_key)
52
+ end
49
53
 
50
54
  def ttl
51
55
  @ttl ||= Config.instance.default_ttl
@@ -0,0 +1,17 @@
1
+ module CacheMethod
2
+ class CachedResult #:nodoc: all
3
+ def obj_digest
4
+ if (l = ::Marshal.dump(CachedResult.resolve_cache_key(obj)).length) > 500
5
+ $stderr.puts "Warn: #{l} long obj digest. #{obj.class} -> #{CachedResult.resolve_cache_key(obj).inspect}"
6
+ end
7
+ @obj_digest ||= ::Digest::SHA1.hexdigest(::Marshal.dump(CachedResult.resolve_cache_key(obj)))
8
+ end
9
+
10
+ def args_digest
11
+ if (l = ::Marshal.dump(CachedResult.resolve_cache_key(args)).length) > 500
12
+ $stderr.puts "Warn: #{l} long args digest. #{method_signature}(#{CachedResult.resolve_cache_key(args).inspect})"
13
+ end
14
+ @args_digest ||= args.empty? ? 'empty' : ::Digest::SHA1.hexdigest(::Marshal.dump(CachedResult.resolve_cache_key(args)))
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module CacheMethod
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/lib/cache_method.rb CHANGED
@@ -35,6 +35,10 @@ module CacheMethod
35
35
  raise ::RuntimeError, "[cache_method] cache_method_clear called, but you have disabled generational caching. Check your setting for CacheMethod.config.generational"
36
36
  end
37
37
  end
38
+
39
+ def cache_method_cached?(method_id, *args)
40
+ ::CacheMethod::CachedResult.new(self, method_id, nil, nil, args).exist?
41
+ end
38
42
  end
39
43
 
40
44
  # All Classes (but not instances), get the <tt>.cache_method</tt> method.
data/test/helper.rb CHANGED
@@ -182,3 +182,15 @@ class DontMarshalMe < Struct.new(:message)
182
182
  message
183
183
  end
184
184
  end
185
+
186
+ class DrJekyll
187
+ def as_cache_key
188
+ 'just a guy'
189
+ end
190
+ end
191
+
192
+ class MrHyde
193
+ def as_cache_key
194
+ 'just a guy'
195
+ end
196
+ end
@@ -436,4 +436,40 @@ class TestCacheMethod < Test::Unit::TestCase
436
436
  end
437
437
  end
438
438
  end
439
+
440
+ def test_cached_query
441
+ a = CopyCat1.new 'mimo'
442
+ assert !a.cache_method_cached?(:echo, 'hi')
443
+ assert !a.cache_method_cached?(:echo, 'there')
444
+ a.echo('hi')
445
+ assert a.cache_method_cached?(:echo, 'hi')
446
+ assert !a.cache_method_cached?(:echo, 'there')
447
+
448
+ assert !BlogM.cache_method_cached?(:get_latest_entries)
449
+ assert !BlogM.cache_method_cached?(:get_latest_entries, 'there')
450
+ BlogM.get_latest_entries
451
+ assert BlogM.cache_method_cached?(:get_latest_entries)
452
+ assert !BlogM.cache_method_cached?(:get_latest_entries, 'there')
453
+ end
454
+
455
+ def test_class_name_automatically_appended_to_cache_key
456
+ jek = DrJekyll.new
457
+ hyde = MrHyde.new
458
+
459
+ assert_equal jek.as_cache_key, hyde.as_cache_key
460
+
461
+ a = CopyCat1.new 'mimo'
462
+
463
+ a.ack(jek)
464
+ assert_equal 1, a.ack_count
465
+
466
+ a.ack(hyde)
467
+ assert_equal 2, a.ack_count
468
+
469
+ a.ack(jek)
470
+ assert_equal 2, a.ack_count
471
+
472
+ a.ack(hyde)
473
+ assert_equal 2, a.ack_count
474
+ end
439
475
  end
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.1
4
+ version: 0.2.2
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-01 00:00:00.000000000 Z
12
+ date: 2012-03-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cache
16
- requirement: &2153251040 !ruby/object:Gem::Requirement
16
+ requirement: &2152318920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 0.2.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153251040
24
+ version_requirements: *2152318920
25
25
  description: Like alias_method, but it's cache_method!
26
26
  email:
27
27
  - seamus@abshere.net
@@ -38,6 +38,7 @@ files:
38
38
  - lib/cache_method.rb
39
39
  - lib/cache_method/cached_result.rb
40
40
  - lib/cache_method/config.rb
41
+ - lib/cache_method/debug.rb
41
42
  - lib/cache_method/generation.rb
42
43
  - lib/cache_method/version.rb
43
44
  - test/helper.rb