method_cacheable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -0,0 +1,97 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "method_cacheable"
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Schneems"]
12
+ s.date = "2011-12-12"
13
+ s.description = "\n Cache methods quickly and easily\n "
14
+ s.email = "richard.schneeman@gmail.com"
15
+ s.files = [
16
+ ".rvmrc",
17
+ ".yardoc/checksums",
18
+ ".yardoc/objects/root.dat",
19
+ ".yardoc/proxy_types",
20
+ "Gemfile",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "autotest/discover.rb",
24
+ "doc/JohnnyCache.html",
25
+ "doc/JohnnyCache/ClassMethods.html",
26
+ "doc/JohnnyCache/MethodCache.html",
27
+ "doc/_index.html",
28
+ "doc/class_list.html",
29
+ "doc/css/common.css",
30
+ "doc/css/full_list.css",
31
+ "doc/css/style.css",
32
+ "doc/file_list.html",
33
+ "doc/frames.html",
34
+ "doc/index.html",
35
+ "doc/js/app.js",
36
+ "doc/js/full_list.js",
37
+ "doc/js/jquery.js",
38
+ "doc/method_list.html",
39
+ "doc/top-level-namespace.html",
40
+ "johnny_cache.gemspec",
41
+ "lib/method_cachable.rb",
42
+ "license.txt",
43
+ "method_cacheable.gemspec",
44
+ "pkg/johnny_cache-0.0.1.gem",
45
+ "readme.md",
46
+ "spec/method_cachable/method_cache_spec.rb",
47
+ "spec/method_cachable_spec.rb",
48
+ "spec/spec_helper.rb"
49
+ ]
50
+ s.homepage = "http://github.com/Schnems/method_cacheable"
51
+ s.licenses = ["MIT"]
52
+ s.require_paths = ["lib"]
53
+ s.rubygems_version = "1.8.10"
54
+ s.summary = "Cache methods quickly and easily."
55
+ s.test_files = [
56
+ "spec/method_cachable/method_cache_spec.rb",
57
+ "spec/method_cachable_spec.rb",
58
+ "spec/spec_helper.rb"
59
+ ]
60
+
61
+ if s.respond_to? :specification_version then
62
+ s.specification_version = 3
63
+
64
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
65
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
66
+ s.add_runtime_dependency(%q<keytar>, [">= 0"])
67
+ s.add_development_dependency(%q<yard>, [">= 0"])
68
+ s.add_development_dependency(%q<rdiscount>, [">= 0"])
69
+ s.add_development_dependency(%q<rake>, ["~> 0.8.7"])
70
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
71
+ s.add_development_dependency(%q<autotest-standalone>, [">= 0"])
72
+ s.add_development_dependency(%q<autotest-growl>, [">= 0"])
73
+ s.add_development_dependency(%q<rspec>, [">= 0"])
74
+ else
75
+ s.add_dependency(%q<activesupport>, [">= 0"])
76
+ s.add_dependency(%q<keytar>, [">= 0"])
77
+ s.add_dependency(%q<yard>, [">= 0"])
78
+ s.add_dependency(%q<rdiscount>, [">= 0"])
79
+ s.add_dependency(%q<rake>, ["~> 0.8.7"])
80
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
81
+ s.add_dependency(%q<autotest-standalone>, [">= 0"])
82
+ s.add_dependency(%q<autotest-growl>, [">= 0"])
83
+ s.add_dependency(%q<rspec>, [">= 0"])
84
+ end
85
+ else
86
+ s.add_dependency(%q<activesupport>, [">= 0"])
87
+ s.add_dependency(%q<keytar>, [">= 0"])
88
+ s.add_dependency(%q<yard>, [">= 0"])
89
+ s.add_dependency(%q<rdiscount>, [">= 0"])
90
+ s.add_dependency(%q<rake>, ["~> 0.8.7"])
91
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
92
+ s.add_dependency(%q<autotest-standalone>, [">= 0"])
93
+ s.add_dependency(%q<autotest-growl>, [">= 0"])
94
+ s.add_dependency(%q<rspec>, [">= 0"])
95
+ end
96
+ end
97
+
@@ -0,0 +1,108 @@
1
+ The Cache in Black
2
+ ==================
3
+ Cache method calls and speed up your Ruby on Rails application with MethodCachable.
4
+
5
+ Method Cacheable
6
+ ============
7
+
8
+ ``` ruby
9
+ class User < ActiveRecord::Base
10
+ include MethodCachable
11
+
12
+ has_many :pictures
13
+
14
+ def expensive_method(val)
15
+ sleep 120
16
+ return val
17
+ end
18
+ end
19
+
20
+ user = User.last
21
+
22
+ # Call User#expensive_method normally
23
+ user.expensive_method(22)
24
+ # => 22
25
+
26
+ # Fetch User#expensive_method from cache
27
+ user.cache.expensive_method(22)
28
+ # => 22
29
+
30
+ # Call User#expensive_method normally
31
+ Benchmark.measure { user.expensive_method(22) }.real
32
+ # => 120.00037693977356
33
+
34
+ # Fetch User#expensive_method from cache
35
+ Benchmark.measure { user.cache.expensive_method(22) }.real
36
+ # => 0.000840902328491211
37
+
38
+ # SOOOOOOOO FAST!!
39
+ ```
40
+
41
+
42
+ Install
43
+ =======
44
+ in your Gemfile
45
+
46
+ gem 'method_cachable'
47
+
48
+ then in your models
49
+
50
+ include MethodCachable
51
+
52
+
53
+ Usage
54
+ ========
55
+
56
+ Explicitly write & read methods.
57
+
58
+ ``` ruby
59
+ user.cache(:read).pictures # => nil
60
+ user.cache(:write).pictures # => [<# Picture ...>, <# Picture ...>] # refreshes the cache
61
+ user.cache(:read).pictures # => [<# Picture ...>, <# Picture ...>]
62
+ ```
63
+
64
+ 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.
65
+
66
+ ``` ruby
67
+ user.cache(:read).expensive_method("w00t") # => nil
68
+ user.cache.expensive_method("w00t") # => "w00t" # sets the cache via :fetch
69
+ user.cache.expensive_method("w00t") # => "w00t" # pulls from the cache
70
+ ```
71
+
72
+ You can also call `:fetch` explicitly if you prefer (but why, thats more typing)
73
+
74
+ ``` ruby
75
+ user.cache(:fetch).expensive_method("w00t") # => "w00t" # pulls from the cache
76
+ ```
77
+
78
+ Different method arguments to the method generate different cache objects. I.E. different input => different output, same input => same output
79
+
80
+ ``` ruby
81
+ user.cache.expensive_method(:schneems => :is_awesome).inspect
82
+ # => {:schneems => :is_awesome}
83
+ user.cache.expensive_method("j/k lol").inspect
84
+ # => "j/k lol"
85
+ ```
86
+
87
+ Configuration
88
+ =============
89
+
90
+ Any configuration options passed to the cache method will be passed to the cache store (default is [Rails.cache](http://api.rubyonrails.org/classes/ActionController/Caching.html#method-i-cache Rails.cache))
91
+
92
+ ``` ruby
93
+ user.cache(:write, :expires_in => 5.seconds).pictures # => [<# Picture ...>, #... ]
94
+ user.cache(:read).pictures # => [<# Picture ...>, #... ]
95
+ sleep 10 # => 2
96
+ user.cache(:read).pictures # => nil
97
+ ```
98
+
99
+
100
+ Contribution
101
+ ============
102
+
103
+ Fork away. If you want to chat about a feature idea, or a question you can find me on the twitters [@schneems](http://twitter.com/schneems). Put any major changes into feature branches. Make sure all tests stay green, and make sure your changes are covered.
104
+
105
+
106
+ licensed under MIT License
107
+ Copyright (c) 2011 Schneems. See LICENSE.txt for
108
+ further details.
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe MethodCachable::MethodCache do
5
+ before(:all) do
6
+ @user = User.new
7
+ end
8
+
9
+ before(:each) do
10
+ @uniq ||= 0
11
+ @uniq += 1
12
+ end
13
+
14
+ describe 'initialize' do
15
+ it 'saves caller' do
16
+ @user.cache.caller_object.should eq(@user)
17
+ end
18
+
19
+ it 'saves cache_method' do
20
+ cache_method = :fetch
21
+ @user.cache(cache_method).cache_operation.should eq(cache_method)
22
+ end
23
+
24
+ it 'saves options' do
25
+ options = {:foo => "bar"}
26
+ @user.cache(options).options.should eq(options)
27
+ end
28
+
29
+ it 'saves options and cache_method' do
30
+ cache_method = :write
31
+ options = {:foo => "bar"}
32
+ cache = @user.cache(cache_method, options)
33
+ cache.options.should eq(options)
34
+ cache.cache_operation.should eq(cache_method)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe MethodCachable do
5
+ before(:each) do
6
+ @user = User.new
7
+ @uniq ||= 0
8
+ @uniq += 1
9
+ end
10
+
11
+ describe '' do
12
+ describe 'calling a cached method' do
13
+ describe 'fetch' do
14
+ it 'should return the result of the normal method' do
15
+ @user.cache.foo(@uniq).should == @user.foo(@uniq)
16
+ end
17
+
18
+ it 'should bypass the normal method if the cache is written' do
19
+ @user.cache(:write).foo(@uniq)
20
+ @user.should_not_receive(:foo)
21
+ @user.cache(:fetch).foo(@uniq)
22
+ end
23
+
24
+ it 'should bypass the normal method if the cache has been fetched before' do
25
+ @user.cache(:fetch).foo(@uniq)
26
+ @user.should_not_receive(:foo)
27
+ @user.cache(:fetch).foo(@uniq)
28
+ end
29
+
30
+ it 'should call the normal method if the cache has been not fetched before' do
31
+ @user.should_receive(:foo)
32
+ @user.cache(:fetch).foo(@uniq)
33
+ end
34
+
35
+ it 'should call the normal method if the cache has been not fetched before' do
36
+ @user.should_receive(:foo)
37
+ @user.cache.foo(@uniq)
38
+ end
39
+
40
+ end
41
+
42
+ describe 'read' do
43
+ it 'read should return nil if cache has not been set yet' do
44
+ @user.cache(:read).foo(@uniq).should eq(nil)
45
+ end
46
+
47
+ it 'read should return value if cache has been set' do
48
+ result = @user.cache.foo(@uniq)
49
+ @user.cache(:read).foo(@uniq).should eq(result)
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '../..', 'lib'))
6
+
7
+ ## Fake rails for testing Rails.cache
8
+ class Rails
9
+ def self.cache
10
+ self
11
+ end
12
+
13
+ def self.fetch(key, options, &block)
14
+ eval("@#{key.gsub(':', '_')} ||= block.call")
15
+ end
16
+
17
+ def self.write(key, val, options, &block)
18
+ eval("@#{key.gsub(':', '_')} = val")
19
+ end
20
+
21
+ def self.read(key, options)
22
+ eval("@#{key.gsub(':', '_')}")
23
+ end
24
+ end
25
+
26
+
27
+ require 'method_cachable'
28
+ class User
29
+ include MethodCachable
30
+ define_keys :foo
31
+
32
+ def foo(var=nil)
33
+ "bar#{var}"
34
+ end
35
+
36
+ def id
37
+ @id ||= rand(100)
38
+ end
39
+ end
40
+
41
+
42
+ require 'rspec'
43
+ require 'rspec/autorun'
44
+
metadata ADDED
@@ -0,0 +1,228 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: method_cacheable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Schneems
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-12 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ hash: 3
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ name: activesupport
31
+ prerelease: false
32
+ type: :runtime
33
+ requirement: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ version_requirements: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ hash: 3
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ name: keytar
45
+ prerelease: false
46
+ type: :runtime
47
+ requirement: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ version_requirements: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ name: yard
59
+ prerelease: false
60
+ type: :development
61
+ requirement: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ version_requirements: &id004 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ name: rdiscount
73
+ prerelease: false
74
+ type: :development
75
+ requirement: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ version_requirements: &id005 !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ hash: 49
83
+ segments:
84
+ - 0
85
+ - 8
86
+ - 7
87
+ version: 0.8.7
88
+ name: rake
89
+ prerelease: false
90
+ type: :development
91
+ requirement: *id005
92
+ - !ruby/object:Gem::Dependency
93
+ version_requirements: &id006 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ hash: 7
99
+ segments:
100
+ - 1
101
+ - 5
102
+ - 2
103
+ version: 1.5.2
104
+ name: jeweler
105
+ prerelease: false
106
+ type: :development
107
+ requirement: *id006
108
+ - !ruby/object:Gem::Dependency
109
+ version_requirements: &id007 !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ name: autotest-standalone
119
+ prerelease: false
120
+ type: :development
121
+ requirement: *id007
122
+ - !ruby/object:Gem::Dependency
123
+ version_requirements: &id008 !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ hash: 3
129
+ segments:
130
+ - 0
131
+ version: "0"
132
+ name: autotest-growl
133
+ prerelease: false
134
+ type: :development
135
+ requirement: *id008
136
+ - !ruby/object:Gem::Dependency
137
+ version_requirements: &id009 !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ hash: 3
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ name: rspec
147
+ prerelease: false
148
+ type: :development
149
+ requirement: *id009
150
+ description: "\n Cache methods quickly and easily\n "
151
+ email: richard.schneeman@gmail.com
152
+ executables: []
153
+
154
+ extensions: []
155
+
156
+ extra_rdoc_files: []
157
+
158
+ files:
159
+ - .rvmrc
160
+ - .yardoc/checksums
161
+ - .yardoc/objects/root.dat
162
+ - .yardoc/proxy_types
163
+ - Gemfile
164
+ - Rakefile
165
+ - VERSION
166
+ - autotest/discover.rb
167
+ - doc/JohnnyCache.html
168
+ - doc/JohnnyCache/ClassMethods.html
169
+ - doc/JohnnyCache/MethodCache.html
170
+ - doc/_index.html
171
+ - doc/class_list.html
172
+ - doc/css/common.css
173
+ - doc/css/full_list.css
174
+ - doc/css/style.css
175
+ - doc/file_list.html
176
+ - doc/frames.html
177
+ - doc/index.html
178
+ - doc/js/app.js
179
+ - doc/js/full_list.js
180
+ - doc/js/jquery.js
181
+ - doc/method_list.html
182
+ - doc/top-level-namespace.html
183
+ - johnny_cache.gemspec
184
+ - lib/method_cachable.rb
185
+ - license.txt
186
+ - method_cacheable.gemspec
187
+ - pkg/johnny_cache-0.0.1.gem
188
+ - readme.md
189
+ - spec/method_cachable/method_cache_spec.rb
190
+ - spec/method_cachable_spec.rb
191
+ - spec/spec_helper.rb
192
+ homepage: http://github.com/Schnems/method_cacheable
193
+ licenses:
194
+ - MIT
195
+ post_install_message:
196
+ rdoc_options: []
197
+
198
+ require_paths:
199
+ - lib
200
+ required_ruby_version: !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - ">="
204
+ - !ruby/object:Gem::Version
205
+ hash: 3
206
+ segments:
207
+ - 0
208
+ version: "0"
209
+ required_rubygems_version: !ruby/object:Gem::Requirement
210
+ none: false
211
+ requirements:
212
+ - - ">="
213
+ - !ruby/object:Gem::Version
214
+ hash: 3
215
+ segments:
216
+ - 0
217
+ version: "0"
218
+ requirements: []
219
+
220
+ rubyforge_project:
221
+ rubygems_version: 1.8.10
222
+ signing_key:
223
+ specification_version: 3
224
+ summary: Cache methods quickly and easily.
225
+ test_files:
226
+ - spec/method_cachable/method_cache_spec.rb
227
+ - spec/method_cachable_spec.rb
228
+ - spec/spec_helper.rb