method_cacheable 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ ## 0.1.0
2
+
3
+ - Fix #3 properly use args if tmp_args not passed
data/Gemfile CHANGED
@@ -1,12 +1,13 @@
1
1
  source 'http://rubygems.org'
2
- gem 'activesupport'
3
- gem 'keytar'
2
+
3
+ gem 'activesupport', '>=3.0.0'
4
+ gem 'keytar', '>=1.5.3'
4
5
 
5
6
  group :development, :test do
6
7
  gem 'yard'
7
8
  gem 'rdiscount'
8
9
  gem 'rake', '~>0.8.7'
9
- gem 'jeweler', '~>1.5.2'
10
+ gem 'jeweler', '>=1.5.2'
10
11
  gem "autotest-standalone"
11
12
  gem "autotest-growl"
12
13
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.1.0
@@ -89,6 +89,11 @@ module MethodCacheable
89
89
  class MethodCache
90
90
  attr_accessor :caller, :method, :args, :options, :cache_operation
91
91
 
92
+ def args=(args)
93
+ args = [args] unless args.is_a? Array
94
+ @args = args
95
+ end
96
+
92
97
  def initialize(caller, *method_cache_args)
93
98
  self.caller = caller
94
99
  self.cache_operation = method_cache_args.map {|x| x if x.is_a? Symbol }.compact.first||:fetch
@@ -143,8 +148,8 @@ module MethodCacheable
143
148
  # cache.method = "foo" # => "foo"
144
149
  # cache.key # => "users:foo:263619"
145
150
  def key(tmp_method = nil, *tmp_args)
146
- tmp_method ||= method
147
- tmp_args ||= args
151
+ tmp_method = method if tmp_method.blank?
152
+ tmp_args = args if tmp_args.blank?
148
153
  key_method = "#{tmp_method}_key".to_sym
149
154
  key = caller.send key_method, *tmp_args if caller.respond_to? key_method
150
155
  key ||= caller.build_key(:name => tmp_method, :args => tmp_args)
@@ -5,17 +5,18 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "method_cacheable"
8
- s.version = "0.0.4"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Schneems"]
12
- s.date = "2011-12-29"
12
+ s.date = "2012-12-17"
13
13
  s.description = "\n Cache methods quickly and easily\n "
14
14
  s.email = "richard.schneeman@gmail.com"
15
15
  s.files = [
16
16
  ".yardoc/checksums",
17
17
  ".yardoc/objects/root.dat",
18
18
  ".yardoc/proxy_types",
19
+ "CHANGELOG.md",
19
20
  "Gemfile",
20
21
  "Rakefile",
21
22
  "VERSION",
@@ -39,8 +40,6 @@ Gem::Specification.new do |s|
39
40
  "lib/method_cacheable.rb",
40
41
  "license.txt",
41
42
  "method_cacheable.gemspec",
42
- "pkg/johnny_cache-0.0.1.gem",
43
- "pkg/method_cacheable-0.0.1.gem",
44
43
  "readme.md",
45
44
  "spec/method_cacheable/method_cache_spec.rb",
46
45
  "spec/method_cacheable_spec.rb",
@@ -49,45 +48,40 @@ Gem::Specification.new do |s|
49
48
  s.homepage = "http://github.com/Schnems/method_cacheable"
50
49
  s.licenses = ["MIT"]
51
50
  s.require_paths = ["lib"]
52
- s.rubygems_version = "1.8.10"
51
+ s.rubygems_version = "1.8.24"
53
52
  s.summary = "Cache methods quickly and easily."
54
- s.test_files = [
55
- "spec/method_cacheable/method_cache_spec.rb",
56
- "spec/method_cacheable_spec.rb",
57
- "spec/spec_helper.rb"
58
- ]
59
53
 
60
54
  if s.respond_to? :specification_version then
61
55
  s.specification_version = 3
62
56
 
63
57
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
64
- s.add_runtime_dependency(%q<activesupport>, [">= 0"])
65
- s.add_runtime_dependency(%q<keytar>, [">= 0"])
58
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
59
+ s.add_runtime_dependency(%q<keytar>, [">= 1.5.3"])
66
60
  s.add_development_dependency(%q<yard>, [">= 0"])
67
61
  s.add_development_dependency(%q<rdiscount>, [">= 0"])
68
62
  s.add_development_dependency(%q<rake>, ["~> 0.8.7"])
69
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
63
+ s.add_development_dependency(%q<jeweler>, [">= 1.5.2"])
70
64
  s.add_development_dependency(%q<autotest-standalone>, [">= 0"])
71
65
  s.add_development_dependency(%q<autotest-growl>, [">= 0"])
72
66
  s.add_development_dependency(%q<rspec>, [">= 0"])
73
67
  else
74
- s.add_dependency(%q<activesupport>, [">= 0"])
75
- s.add_dependency(%q<keytar>, [">= 0"])
68
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
69
+ s.add_dependency(%q<keytar>, [">= 1.5.3"])
76
70
  s.add_dependency(%q<yard>, [">= 0"])
77
71
  s.add_dependency(%q<rdiscount>, [">= 0"])
78
72
  s.add_dependency(%q<rake>, ["~> 0.8.7"])
79
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
73
+ s.add_dependency(%q<jeweler>, [">= 1.5.2"])
80
74
  s.add_dependency(%q<autotest-standalone>, [">= 0"])
81
75
  s.add_dependency(%q<autotest-growl>, [">= 0"])
82
76
  s.add_dependency(%q<rspec>, [">= 0"])
83
77
  end
84
78
  else
85
- s.add_dependency(%q<activesupport>, [">= 0"])
86
- s.add_dependency(%q<keytar>, [">= 0"])
79
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
80
+ s.add_dependency(%q<keytar>, [">= 1.5.3"])
87
81
  s.add_dependency(%q<yard>, [">= 0"])
88
82
  s.add_dependency(%q<rdiscount>, [">= 0"])
89
83
  s.add_dependency(%q<rake>, ["~> 0.8.7"])
90
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
84
+ s.add_dependency(%q<jeweler>, [">= 1.5.2"])
91
85
  s.add_dependency(%q<autotest-standalone>, [">= 0"])
92
86
  s.add_dependency(%q<autotest-growl>, [">= 0"])
93
87
  s.add_dependency(%q<rspec>, [">= 0"])
data/readme.md CHANGED
@@ -1,14 +1,20 @@
1
- The Cache in Black
2
- ==================
3
- Cache method calls and speed up your Ruby on Rails application with MethodCacheable.
4
-
5
1
  Method Cacheable
6
2
  ============
7
3
 
4
+ Cache method calls and speed up your Ruby on Rails application with MethodCacheable. It's kindof like [ cache_method](https://github.com/seamusabshere/cache_method?utm_source=rubyweekly&utm_medium=email) but it's more explicit about what's being cached and how.
5
+
6
+
7
+ ## Simplicity Rules
8
+
9
+ This is a very very small library wrapped around `Rails.cache` api. It's goal is to be easy to use, and flexible (can be used without Rails). Currently method_cacheable weights in at about 200 lines with documentation (not counting tests and readme)
10
+
11
+
8
12
  In your Model include `MethodCacheable`
9
13
 
10
14
  app/models/user.rb
11
- ``` ruby
15
+
16
+ ```ruby
17
+
12
18
  class User < ActiveRecord::Base
13
19
  include MethodCacheable
14
20
 
@@ -19,6 +25,7 @@ app/models/user.rb
19
25
  return val
20
26
  end
21
27
  end
28
+
22
29
  ```
23
30
 
24
31
  Then use the `#cache` method to fetch results from cache when available
@@ -26,14 +33,6 @@ Then use the `#cache` method to fetch results from cache when available
26
33
  ```
27
34
  user = User.last
28
35
 
29
- # Call User#expensive_method normally
30
- user.expensive_method(22)
31
- # => 22
32
-
33
- # Fetch User#expensive_method from cache
34
- user.cache.expensive_method(22)
35
- # => 22
36
-
37
36
  # Call User#expensive_method normally
38
37
  Benchmark.measure { user.expensive_method(22) }.real
39
38
  # => 120.00037693977356
@@ -41,8 +40,6 @@ Then use the `#cache` method to fetch results from cache when available
41
40
  # Fetch User#expensive_method from cache
42
41
  Benchmark.measure { user.cache.expensive_method(22) }.real
43
42
  # => 0.000840902328491211
44
-
45
- # SOOOOOOOO FAST!!
46
43
  ```
47
44
 
48
45
 
@@ -52,9 +49,28 @@ in your Gemfile
52
49
 
53
50
  gem 'method_cacheable'
54
51
 
55
- In an initializer tell MethodCacheable to use the Rails.cache backend. You can use any object here that responds to `#write`, `#read`, and `#fetch`
52
+
53
+ You will also want to have a library for caching objects. Such as `dalli` for using memcache
54
+
55
+ gem 'dalli'
56
+
57
+ Then run
58
+
59
+ bundle install
60
+
61
+
62
+ ## Set up Memcache
63
+
64
+ In your `app/config/application.rb` set your Rails cache store to use `dalli`
65
+
66
+ ```ruby
67
+ config.cache_store = :dalli_store
68
+ ```
69
+
70
+ In an initializer tell MethodCacheable to use the Rails.cache backend. You can use any object here that responds to `#write`, `#read`, and `#fetch`.
56
71
 
57
72
  initializers/method_cacheable.rb
73
+
58
74
  ```ruby
59
75
  MethodCacheable.config do |config|
60
76
  config.store = Rails.cache
@@ -65,18 +81,12 @@ then in your models
65
81
 
66
82
  include MethodCacheable
67
83
 
84
+ Now you're good to go, just use the `cache` method in that class and you can write, read and fetch any method from cache.
85
+
68
86
 
69
87
  Usage
70
88
  ========
71
89
 
72
- Explicitly write & read methods.
73
-
74
- ``` ruby
75
- user.cache(:read).pictures # => nil
76
- user.cache(:write).pictures # => [<# Picture ...>, <# Picture ...>] # refreshes the cache
77
- user.cache(:read).pictures # => [<# Picture ...>, <# Picture ...>]
78
- ```
79
-
80
90
  By default the `cache` method will will `:fetch` from the cache store. This means that if the key exists it will be pulled, if not the method will be called, returned, and the key will be set.
81
91
 
82
92
  ``` ruby
@@ -85,12 +95,22 @@ By default the `cache` method will will `:fetch` from the cache store. This mean
85
95
  user.cache.expensive_method("w00t") # => "w00t" # pulls from the cache
86
96
  ```
87
97
 
88
- You can also call `:fetch` explicitly if you prefer (but why, thats more typing)
98
+ You can also call `:fetch` explicitly if you prefer
89
99
 
90
100
  ``` ruby
91
101
  user.cache(:fetch).expensive_method("w00t") # => "w00t" # pulls from the cache
92
102
  ```
93
103
 
104
+
105
+ Explicitly write & read methods.
106
+
107
+ ``` ruby
108
+ user.cache(:read).pictures # => nil
109
+ user.cache(:write).pictures # => [<# Picture ...>, <# Picture ...>] # refreshes the cache
110
+ user.cache(:read).pictures # => [<# Picture ...>, <# Picture ...>]
111
+ ```
112
+
113
+
94
114
  Different method arguments to the method generate different cache objects. I.E. different input => different output, same input => same output
95
115
 
96
116
  ``` ruby
@@ -100,6 +120,17 @@ Different method arguments to the method generate different cache objects. I.E.
100
120
  # => "j/k lol"
101
121
  ```
102
122
 
123
+ ## Delete An Entry
124
+
125
+ You can delete any cached method using `cache.delete` and passing in the name of the method and any arguements, for example:
126
+
127
+ ```ruby
128
+ user.cache.expensive_method("w00t") # => "w00t"
129
+
130
+ user.cache.delete(:expensive_method, "w00t") # => nil
131
+ ```
132
+
133
+
103
134
  Configuration
104
135
  =============
105
136
 
@@ -112,6 +143,14 @@ Any configuration options passed to the cache method will be passed to the cache
112
143
  user.cache(:read).pictures # => nil
113
144
  ```
114
145
 
146
+ ## Generate Keys
147
+
148
+ You don't need to generate keys, we do that for you using a library called [keytar](http://github.com/schneems/keytar). If you want to see a key you can call key and pass in the name of the method and any arguments into it.
149
+
150
+
151
+ ```ruby
152
+ User.find(9).cache.key(:foo) # => "users:foo:9"
153
+ ```
115
154
 
116
155
  Contribution
117
156
  ============
@@ -123,6 +162,3 @@ licensed under MIT License
123
162
  Copyright (c) 2011 Schneems. See LICENSE.txt for
124
163
  further details.
125
164
 
126
-
127
-
128
-
@@ -20,10 +20,25 @@ describe MethodCacheable::MethodCache do
20
20
 
21
21
  end
22
22
 
23
+ describe 'args' do
24
+ it 'converts non array args to array' do
25
+ cache = user.cache
26
+ cache.args = 1
27
+ cache.args.should == [1]
28
+ end
29
+ end
30
+
23
31
  describe 'key' do
24
32
  it 'returns the key' do
25
33
  user.cache.key(:foo).should == "users:foo:#{user.id}"
26
34
  end
35
+
36
+ it 'returns the key after specifying args and method' do
37
+ cache = user.cache
38
+ cache.method = "foo#{@uniq}"
39
+ cache.args = [1]
40
+ cache.key.should == "users:foo#{@uniq}:#{user.id}:1"
41
+ end
27
42
  end
28
43
 
29
44
  describe 'exists?' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: method_cacheable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,33 +9,43 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-29 00:00:00.000000000Z
12
+ date: 2012-12-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70212263196980 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70212263196980
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: keytar
27
- requirement: &70212263195260 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
31
36
  - !ruby/object:Gem::Version
32
- version: '0'
37
+ version: 1.5.3
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70212263195260
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.5.3
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: yard
38
- requirement: &70212263175320 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70212263175320
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: rdiscount
49
- requirement: &70212263173780 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,10 +69,15 @@ dependencies:
54
69
  version: '0'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *70212263173780
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: rake
60
- requirement: &70212263171900 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
83
  - - ~>
@@ -65,21 +85,31 @@ dependencies:
65
85
  version: 0.8.7
66
86
  type: :development
67
87
  prerelease: false
68
- version_requirements: *70212263171900
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 0.8.7
69
94
  - !ruby/object:Gem::Dependency
70
95
  name: jeweler
71
- requirement: &70212263170520 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
72
97
  none: false
73
98
  requirements:
74
- - - ~>
99
+ - - ! '>='
75
100
  - !ruby/object:Gem::Version
76
101
  version: 1.5.2
77
102
  type: :development
78
103
  prerelease: false
79
- version_requirements: *70212263170520
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: 1.5.2
80
110
  - !ruby/object:Gem::Dependency
81
111
  name: autotest-standalone
82
- requirement: &70212263169000 !ruby/object:Gem::Requirement
112
+ requirement: !ruby/object:Gem::Requirement
83
113
  none: false
84
114
  requirements:
85
115
  - - ! '>='
@@ -87,10 +117,15 @@ dependencies:
87
117
  version: '0'
88
118
  type: :development
89
119
  prerelease: false
90
- version_requirements: *70212263169000
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
91
126
  - !ruby/object:Gem::Dependency
92
127
  name: autotest-growl
93
- requirement: &70212263166500 !ruby/object:Gem::Requirement
128
+ requirement: !ruby/object:Gem::Requirement
94
129
  none: false
95
130
  requirements:
96
131
  - - ! '>='
@@ -98,10 +133,15 @@ dependencies:
98
133
  version: '0'
99
134
  type: :development
100
135
  prerelease: false
101
- version_requirements: *70212263166500
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
102
142
  - !ruby/object:Gem::Dependency
103
143
  name: rspec
104
- requirement: &70212263164700 !ruby/object:Gem::Requirement
144
+ requirement: !ruby/object:Gem::Requirement
105
145
  none: false
106
146
  requirements:
107
147
  - - ! '>='
@@ -109,7 +149,12 @@ dependencies:
109
149
  version: '0'
110
150
  type: :development
111
151
  prerelease: false
112
- version_requirements: *70212263164700
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
113
158
  description: ! "\n Cache methods quickly and easily\n "
114
159
  email: richard.schneeman@gmail.com
115
160
  executables: []
@@ -119,6 +164,7 @@ files:
119
164
  - .yardoc/checksums
120
165
  - .yardoc/objects/root.dat
121
166
  - .yardoc/proxy_types
167
+ - CHANGELOG.md
122
168
  - Gemfile
123
169
  - Rakefile
124
170
  - VERSION
@@ -142,8 +188,6 @@ files:
142
188
  - lib/method_cacheable.rb
143
189
  - license.txt
144
190
  - method_cacheable.gemspec
145
- - pkg/johnny_cache-0.0.1.gem
146
- - pkg/method_cacheable-0.0.1.gem
147
191
  - readme.md
148
192
  - spec/method_cacheable/method_cache_spec.rb
149
193
  - spec/method_cacheable_spec.rb
@@ -163,7 +207,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
163
207
  version: '0'
164
208
  segments:
165
209
  - 0
166
- hash: 588910908911295363
210
+ hash: 1837387735007022591
167
211
  required_rubygems_version: !ruby/object:Gem::Requirement
168
212
  none: false
169
213
  requirements:
@@ -172,11 +216,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
216
  version: '0'
173
217
  requirements: []
174
218
  rubyforge_project:
175
- rubygems_version: 1.8.10
219
+ rubygems_version: 1.8.24
176
220
  signing_key:
177
221
  specification_version: 3
178
222
  summary: Cache methods quickly and easily.
179
- test_files:
180
- - spec/method_cacheable/method_cache_spec.rb
181
- - spec/method_cacheable_spec.rb
182
- - spec/spec_helper.rb
223
+ test_files: []
Binary file