cache_method 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YjczZDg5NDE3MTY2MGMxMTMwNGI3OWFhOTZhMGM1NzE5YTQzZDkzYQ==
5
+ data.tar.gz: !binary |-
6
+ OGZhNzRiM2JhMTQ3NjA3MzllOTc0NDEyOGZlZGI5OTg4MWNkYTM4MA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NmQ1MTIxOTJkZTZjZjFjODRlMDM5MjNmOGU0N2YwN2M0NzIwYWEwYzAyMTAw
10
+ YTM2MDAwMGI0OWZhZmVlOWE5ODY2ZjhhZjlkNGE5MTliYmFjNTU1YzA2MjA3
11
+ YWM3YWFhODY1MTBiMGFmNWI5YmIxOTc3MWQ2MmM2YmRlNjYxOTI=
12
+ data.tar.gz: !binary |-
13
+ NTQ5ZDBkYjViNGIxYjk0MDE4MTY0ZGRmNTFlNTI0NTcwMDFiN2NmZmZmNThk
14
+ ODdlYWVkNDcyZjlmODVjNTNjYzI1OGUzNjJiNDg2YjhjZDIxODc2ODhlYzg5
15
+ YzUzZDMzNmIzNmNkNWQyNGEyMTA5OWFjNzU4MGU4ZjhhODViOTE=
data/CHANGELOG CHANGED
@@ -1,3 +1,10 @@
1
+ 0.2.6 / 2013-05-13
2
+
3
+ * Enhancements
4
+
5
+ * cache_method_clear_on - for when a cache should be cleared whenever another method is called - thanks @rubemz ! https://github.com/seamusabshere/cache_method/pull/4
6
+ * hypothetically more useful output when you require 'cache_method/debug'
7
+
1
8
  0.2.5 / 2012-04-24
2
9
 
3
10
  * Enhancements
data/Gemfile CHANGED
@@ -1,11 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in cache_method.gemspec
4
4
  gemspec
5
-
6
- # development dependencies
7
- gem 'rake'
8
- gem 'dalli'
9
- gem 'yard'
10
- gem 'minitest'
11
- gem 'minitest-reporters'
data/README.markdown CHANGED
@@ -24,17 +24,29 @@ We use `cache_method` for [data science at Brighter Planet](http://brighterplane
24
24
 
25
25
  require 'cache_method'
26
26
  class Blog
27
+
27
28
  attr_reader :name, :url
29
+
28
30
  def initialize(name, url)
29
31
  @name = name
30
32
  @url = url
31
33
  end
32
- # The slow method that you want to speed up
34
+
33
35
  def entries(date)
34
36
  # ...
35
37
  end
38
+
39
+ # cache that slow method!
36
40
  cache_method :entries
37
- # Not always required
41
+
42
+ def update(stuff)
43
+ # ...
44
+ end
45
+
46
+ # automatically clear cache for #entries when #update is called...
47
+ cache_method_clear_on :update, :entries
48
+
49
+ # custom cache key - not always required!
38
50
  def as_cache_key
39
51
  { :name => name, :url => url }
40
52
  end
@@ -221,6 +233,11 @@ Rest assured that `Tiger.my_module_method` and `Lion.my_module_method` will be c
221
233
  # cache_method :my_module_method
222
234
  end
223
235
 
236
+ ## Contributors
237
+
238
+ * [Seamus Abshere](https://github.com/seamusabshere)
239
+ * [Rubem Nakamura](https://github.com/rubemz)
240
+
224
241
  ## Copyright
225
242
 
226
243
  Copyright 2012 Seamus Abshere
data/cache_method.gemspec CHANGED
@@ -1,6 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "cache_method/version"
2
+ require File.expand_path("../lib/cache_method/version", __FILE__)
4
3
 
5
4
  Gem::Specification.new do |s|
6
5
  s.name = "cache_method"
@@ -17,5 +16,13 @@ Gem::Specification.new do |s|
17
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
17
  s.require_paths = ["lib"]
19
18
 
20
- s.add_dependency 'cache', '>=0.2.1'
19
+ s.add_runtime_dependency 'cache', '>=0.2.1'
20
+
21
+ s.add_development_dependency 'activesupport'
22
+ s.add_development_dependency 'dalli'
23
+ s.add_development_dependency 'yard'
24
+ s.add_development_dependency 'minitest'
25
+ if RUBY_VERSION >= '1.9'
26
+ s.add_development_dependency 'minitest-reporters'
27
+ end
21
28
  end
data/lib/cache_method.rb CHANGED
@@ -14,15 +14,15 @@ module CacheMethod
14
14
  @config ||= Config.new
15
15
  end
16
16
  end
17
-
17
+
18
18
  def CacheMethod.klass_name(obj) #:nodoc:
19
19
  (obj.is_a?(::Class) or obj.is_a?(::Module)) ? obj.to_s : obj.class.to_s
20
20
  end
21
-
21
+
22
22
  def CacheMethod.method_delimiter(obj) #:nodoc:
23
23
  (obj.is_a?(::Class) or obj.is_a?(::Module)) ? '.' : '#'
24
24
  end
25
-
25
+
26
26
  def CacheMethod.method_signature(obj, method_id) #:nodoc:
27
27
  [ klass_name(obj), method_id ].join method_delimiter(obj)
28
28
  end
@@ -55,7 +55,7 @@ module CacheMethod
55
55
  def CacheMethod.digest(obj)
56
56
  ::Digest::SHA1.hexdigest ::Marshal.dump(resolve_cache_key(obj))
57
57
  end
58
-
58
+
59
59
  # All Objects, including instances and Classes, get the <tt>#cache_method_clear</tt> method.
60
60
  module InstanceMethods
61
61
  # Clear the cache for a particular method.
@@ -99,6 +99,30 @@ module CacheMethod
99
99
  ::CacheMethod::CachedResult.new(self, method_id, original_method_id, ttl, args, &blk).fetch
100
100
  end
101
101
  end
102
+
103
+ # Clear a cache method once another method is called. Useful in situations where
104
+ # you want to clear a cache whenever another method is callled, commonly
105
+ # an update.
106
+ #
107
+ # Example:
108
+ # class Blog
109
+ # def get_latest_entries
110
+ # # [...]
111
+ # end
112
+ # def update_entries
113
+ # # update happens
114
+ # end
115
+ # cache_method_clear_on :update_entries, :get_latest_entries
116
+ # end
117
+ def cache_method_clear_on(method_id, cache_method_clear_id)
118
+ original_method_id = "_original_#{method_id}"
119
+ alias_method original_method_id, method_id
120
+
121
+ define_method method_id do |*args, &blk|
122
+ cache_method_clear cache_method_clear_id
123
+ send(original_method_id, *args, &blk)
124
+ end
125
+ end
102
126
  end
103
127
  end
104
128
 
@@ -1,18 +1,25 @@
1
1
  module CacheMethod
2
2
  def CacheMethod.digest(obj)
3
- if (l = ::Marshal.dump(resolve_cache_key(obj)).length) > 500
4
- $stderr.puts "[cache_method] DIGEST (long?): #{resolve_cache_key(obj).inspect}"
3
+ cache_key = resolve_cache_key obj
4
+ m = Marshal.dump cache_key
5
+ if m.length > 1000
6
+ $stderr.puts
7
+ $stderr.puts "[cache_method] DIGEST (#{'X' * (m.length / 1024)}): #{cache_key.inspect}"
5
8
  end
6
- ::Digest::SHA1.hexdigest ::Marshal.dump(resolve_cache_key(obj))
9
+ ::Digest::SHA1.hexdigest m
7
10
  end
8
11
 
9
12
  class CachedResult
10
13
  def debug_get_wrapped
11
14
  retval = original_get_wrapped
12
15
  if retval
16
+ # $stderr.puts
13
17
  # $stderr.puts "[cache_method] GET: #{method_signature}(#{args.inspect})"
14
18
  else
15
- $stderr.puts "[cache_method] GET (miss!): #{method_signature}(#{args.inspect})"
19
+ cache_key = CacheMethod.resolve_cache_key obj
20
+ m = Marshal.dump cache_key
21
+ $stderr.puts
22
+ $stderr.puts "[cache_method] GET (miss!): #{method_signature}(#{args.inspect}) - #{::Digest::SHA1.hexdigest(m)} - #{cache_key.inspect}"
16
23
  end
17
24
  retval
18
25
  end
@@ -21,9 +28,12 @@ module CacheMethod
21
28
 
22
29
  def debug_set_wrapped
23
30
  retval = original_set_wrapped
24
- if (l = ::Marshal.dump(retval).length > 1000)
25
- $stderr.puts "[cache_method] SET (long?): #{method_signature}(#{args.inspect}) -> #{retval.inspect}"
31
+ m = Marshal.dump retval
32
+ if m.length > 1000
33
+ $stderr.puts
34
+ $stderr.puts "[cache_method] SET (#{'X' * (m.length / 1024)}): #{method_signature}(#{args.inspect}) -> #{retval.inspect}"
26
35
  else
36
+ # $stderr.puts
27
37
  # $stderr.puts "[cache_method] SET: #{method_signature}(#{args.inspect})"
28
38
  end
29
39
  retval
@@ -1,3 +1,3 @@
1
1
  module CacheMethod
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
data/test/helper.rb CHANGED
@@ -2,12 +2,9 @@ require 'rubygems'
2
2
  require 'bundler/setup'
3
3
  require 'minitest/spec'
4
4
  require 'minitest/autorun'
5
- require 'minitest/reporters'
6
- MiniTest::Unit.runner = MiniTest::SuiteRunner.new
7
- MiniTest::Unit.runner.reporters << MiniTest::Reporters::SpecReporter.new
8
-
9
- if ::Bundler.definition.specs['ruby-debug19'].first or ::Bundler.definition.specs['ruby-debug'].first
10
- require 'ruby-debug'
5
+ if RUBY_VERSION >= '1.9'
6
+ require 'minitest/reporters'
7
+ MiniTest::Reporters.use! MiniTest::Reporters::SpecReporter.new
11
8
  end
12
9
 
13
10
  require 'cache_method'
@@ -66,11 +63,11 @@ module Say
66
63
  def say_count
67
64
  @say_count ||= 0
68
65
  end
69
-
66
+
70
67
  def say_count=(x)
71
68
  @say_count = x
72
69
  end
73
-
70
+
74
71
  def say(msg)
75
72
  self.say_count += 1
76
73
  msg
@@ -123,6 +120,14 @@ class Blog1
123
120
  ["voo vaa #{name}"]
124
121
  end
125
122
  cache_method :get_latest_entries2, 1 # second
123
+ def update_entries param
124
+ if block_given?
125
+ yield param
126
+ else
127
+ param
128
+ end
129
+ end
130
+ cache_method_clear_on :update_entries, :get_latest_entries
126
131
  def as_cache_key
127
132
  { :name => name, :url => url }
128
133
  end
@@ -197,11 +197,25 @@ describe CacheMethod do
197
197
  a = new_instance_of_my_blog
198
198
  a.get_latest_entries.must_equal ["hello from #{a.name}"]
199
199
  a.request_count.must_equal 1
200
+ a.get_latest_entries.must_equal ["hello from #{a.name}"]
201
+ a.request_count.must_equal 1
200
202
  a.cache_method_clear :get_latest_entries
201
203
  a.get_latest_entries.must_equal ["hello from #{a.name}"]
202
204
  a.request_count.must_equal 2
203
205
  end
204
206
 
207
+ it %{cache_method_clear_on} do
208
+ a = new_instance_of_my_blog
209
+ a.get_latest_entries.must_equal ["hello from #{a.name}"]
210
+ a.request_count.must_equal 1
211
+ a.update_entries("param").must_equal("param")
212
+ a.update_entries(1) {|param| param + 1}.must_equal(2)
213
+ a.get_latest_entries.must_equal ["hello from #{a.name}"]
214
+ a.request_count.must_equal 2
215
+ a.get_latest_entries.must_equal ["hello from #{a.name}"]
216
+ a.request_count.must_equal 2
217
+ end
218
+
205
219
  it %{clear_correct_instance_method} do
206
220
  a = new_instance_of_my_blog
207
221
  a.get_latest_entries.must_equal ["hello from #{a.name}"]
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_method
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
5
- prerelease:
4
+ version: 0.2.6
6
5
  platform: ruby
7
6
  authors:
8
7
  - Seamus Abshere
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-04-24 00:00:00.000000000 Z
11
+ date: 2013-05-13 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: cache
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,11 +20,80 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: 0.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dalli
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-reporters
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
30
97
  description: Like alias_method, but it's cache_method!
31
98
  email:
32
99
  - seamus@abshere.net
@@ -50,27 +117,26 @@ files:
50
117
  - test/test_cache_method.rb
51
118
  homepage: https://github.com/seamusabshere/cache_method
52
119
  licenses: []
120
+ metadata: {}
53
121
  post_install_message:
54
122
  rdoc_options: []
55
123
  require_paths:
56
124
  - lib
57
125
  required_ruby_version: !ruby/object:Gem::Requirement
58
- none: false
59
126
  requirements:
60
127
  - - ! '>='
61
128
  - !ruby/object:Gem::Version
62
129
  version: '0'
63
130
  required_rubygems_version: !ruby/object:Gem::Requirement
64
- none: false
65
131
  requirements:
66
132
  - - ! '>='
67
133
  - !ruby/object:Gem::Version
68
134
  version: '0'
69
135
  requirements: []
70
136
  rubyforge_project: cache_method
71
- rubygems_version: 1.8.21
137
+ rubygems_version: 2.0.3
72
138
  signing_key:
73
- specification_version: 3
139
+ specification_version: 4
74
140
  summary: Lets you cache methods (to memcached, redis, etc.) sort of like you can memoize
75
141
  them
76
142
  test_files: