tobias-cache_value 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Tobias Crawley
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = cache_value
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Tobias Crawley. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "cache_value"
8
+ gem.summary = %Q{Easy value caching}
9
+ gem.email = "tcrawley@gmail.com"
10
+ gem.homepage = "http://github.com/tobias/cache_value"
11
+ gem.authors = ["Tobias Crawley"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "cache_value #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -0,0 +1,52 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{cache_value}
5
+ s.version = "0.2.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Tobias Crawley"]
9
+ s.date = %q{2009-07-13}
10
+ s.email = %q{tcrawley@gmail.com}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "cache_value.gemspec",
23
+ "lib/cache_value.rb",
24
+ "lib/cache_value/cache_machine.rb",
25
+ "lib/cache_value/cache_value.rb",
26
+ "lib/cache_value/util.rb",
27
+ "test/cache_machine_test.rb",
28
+ "test/cache_value_test.rb",
29
+ "test/test_helper.rb"
30
+ ]
31
+ s.has_rdoc = true
32
+ s.homepage = %q{http://github.com/tobias/cache_value}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.1}
36
+ s.summary = %q{Easy value caching}
37
+ s.test_files = [
38
+ "test/cache_machine_test.rb",
39
+ "test/cache_value_test.rb",
40
+ "test/test_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 2
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ else
49
+ end
50
+ else
51
+ end
52
+ end
@@ -0,0 +1,73 @@
1
+ require 'active_support/core_ext/array'
2
+ require 'active_support/core_ext/class'
3
+ require 'active_support/inflector'
4
+ require 'active_support/cache'
5
+ require 'cache_value/util'
6
+ require 'sha1'
7
+
8
+ module CacheValue
9
+ class CacheMachine
10
+ extend Util
11
+
12
+ class << self
13
+
14
+ def cache_store=(*store_option)
15
+ @cache_store = store_option ? ActiveSupport::Cache.lookup_store(store_option) : nil
16
+ end
17
+
18
+ def cache_store
19
+ @cache_store ||= ActiveSupport::Cache.lookup_store(:file_store, default_storage_dir)
20
+ end
21
+
22
+ def default_storage_dir
23
+ raise ConfigurationException.new('Not running under rails. Set the cache_store type and location manually using CacheValue::CacheMachine.store_option=') unless defined?(RAILS_ROOT)
24
+ File.join(RAILS_ROOT, 'tmp', 'cache_value_caches')
25
+ end
26
+
27
+ def lookup(object, method, options, arguments = nil)
28
+ value, last_cached = fetch_and_parse(object, method, arguments)
29
+ if !last_cached or !cached_value_is_still_valid?(value, last_cached, object, method, options)
30
+ value = call_and_store_value(object, method, arguments)
31
+ end
32
+
33
+ value
34
+ end
35
+
36
+ def cache_key(object, method, arguments = nil)
37
+ raise ConfigurationException.new("object of class #{object.class.name} does not respond to :cache_key") unless object.respond_to?(:cache_key)
38
+ key = object.cache_key.gsub('/', '_') + "_#{method}"
39
+ key << '_' + Digest::SHA1.hexdigest(arguments.to_yaml) if arguments
40
+ key
41
+ end
42
+
43
+ def fetch_and_parse(object, method, arguments = nil)
44
+ data = cache_store.fetch(cache_key(object, method, arguments))
45
+ if data
46
+ YAML::load(data)
47
+ else
48
+ [nil, nil]
49
+ end
50
+ end
51
+
52
+ def call_and_store_value(object, method, arguments = nil)
53
+ without_method = caching_method_names(method).first
54
+ value = arguments ? object.send(without_method, arguments) : object.send(without_method)
55
+ cache_store.write(cache_key(object, method, arguments), [value, Time.now].to_yaml)
56
+
57
+ value
58
+ end
59
+
60
+ def cached_value_is_still_valid?(value, cached_age, object, method, options)
61
+ options = object.send(:method, options) if options.is_a?(Symbol)
62
+ if options.respond_to?(:arity)
63
+ options.call(*([cached_age, object, method][0,options.arity]))
64
+ else
65
+ cached_age > Time.now - options
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ class ConfigurationException < StandardError
72
+ end
73
+ end
@@ -0,0 +1,21 @@
1
+ require 'cache_value/util'
2
+ require 'cache_value/cache_machine'
3
+
4
+ module CacheValue
5
+ module ClassMethods
6
+ include Util
7
+
8
+ def cache_value(method, option)
9
+ without_method, with_method = caching_method_names(method)
10
+ class_eval do
11
+ define_method with_method do
12
+ CacheValue::CacheMachine.lookup(self, method, option)
13
+ end
14
+
15
+ alias_method without_method, method
16
+ alias_method method, with_method
17
+ end
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,10 @@
1
+ module CacheValue
2
+ module Util
3
+ def caching_method_names(method)
4
+ washed_method = method.to_s.sub(/([?!=])$/, '')
5
+ punctuation = $1
6
+ ["#{washed_method}_without_value_caching#{punctuation}",
7
+ "#{washed_method}_with_value_caching#{punctuation}"]
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ require 'cache_value/cache_value'
2
+
3
+ module CacheValue
4
+ ActiveRecord::Base.send(:extend, CacheValue::ClassMethods) if defined?(ActiveRecord::Base)
5
+ end
@@ -0,0 +1,189 @@
1
+ require 'test_helper'
2
+
3
+ require 'cache_value/cache_machine'
4
+
5
+
6
+ class CacheMachineTest < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @cm = CacheValue::CacheMachine
10
+ @obj = mock
11
+ @method = :some_method
12
+ @value = 2
13
+ end
14
+
15
+ context 'cache store' do
16
+ should 'provide a cache store' do
17
+ ::RAILS_ROOT = '/tmp'
18
+ @cm.cache_store.should_not == nil
19
+ end
20
+
21
+ context 'default storage dir' do
22
+ should 'raise exception if RAILS_ENV is not set' do
23
+ assert_raise CacheValue::ConfigurationException do
24
+ @cm.default_storage_dir
25
+ end
26
+ end
27
+
28
+ should 'point to the tmp in RAILS_ROOT if available' do
29
+ ::RAILS_ROOT = '/tmp'
30
+ @cm.default_storage_dir.should == '/tmp/tmp/cache_value_caches'
31
+ end
32
+
33
+
34
+ end
35
+
36
+ teardown do
37
+ Object.send(:remove_const, :RAILS_ROOT) if defined?(RAILS_ROOT)
38
+ end
39
+
40
+ end
41
+
42
+
43
+ context 'cache key' do
44
+ should 'raise exception if object does not respond_to cache_key' do
45
+ assert_raise CacheValue::ConfigurationException do
46
+ @cm.cache_key(@obj, @method)
47
+ end
48
+ end
49
+
50
+ context 'with an object that responds to cache_key' do
51
+ setup do
52
+ @obj.expects(:cache_key).returns('a/cache/key')
53
+ end
54
+
55
+ should 'include the method name at the end' do
56
+ assert_match /_method$/, @cm.cache_key(@obj, :method)
57
+ end
58
+
59
+ should 'not have any slashes' do
60
+ assert_no_match %r{/}, @cm.cache_key(@obj, :method)
61
+ end
62
+ end
63
+
64
+ context 'with arguments' do
65
+ setup do
66
+ @obj.expects(:cache_key).returns('a/cache/key')
67
+ end
68
+
69
+ should 'should include the hash in the key' do
70
+ now = Time.now
71
+ assert_match /_method_#{Digest::SHA1.hexdigest([1, 2, now].to_yaml)}$/, @cm.cache_key(@obj, :method, [1, 2, now])
72
+ end
73
+
74
+ end
75
+ end
76
+
77
+ context 'fetch_and_parse' do
78
+ setup do
79
+ @cache_data = ['data', Time.now]
80
+ @cm.expects(:cache_key).returns('')
81
+ end
82
+
83
+ should 'return the data and timestamp when found' do
84
+ @cm.cache_store.expects(:fetch).returns(@cache_data.to_yaml)
85
+ @cm.fetch_and_parse(@obj, @method).should == @cache_data
86
+ end
87
+
88
+ should 'return nils when data not found' do
89
+ @cm.cache_store.expects(:fetch).returns(nil)
90
+ @cm.fetch_and_parse(@obj, @method).should == [nil, nil]
91
+ end
92
+ end
93
+
94
+ context 'call_and_store_value' do
95
+ setup do
96
+ @obj.expects(:some_method_without_value_caching).returns(2)
97
+ key = 'key'
98
+ @cm.expects(:cache_key).with(@obj, :some_method, nil).returns(key)
99
+ now = Time.now
100
+ Time.stubs(:now).returns(now)
101
+ @cm.cache_store.expects(:write).with(key, [2, Time.now].to_yaml)
102
+ end
103
+
104
+ should 'return the data' do
105
+ @cm.call_and_store_value(@obj, :some_method).should == 2
106
+ end
107
+
108
+ end
109
+
110
+ context 'validity of stored value' do
111
+
112
+ context 'checking by age in seconds' do
113
+ should 'be valid if the value is younger than the age limit' do
114
+ @cm.cached_value_is_still_valid?(@value, Time.now - 30, @obj, @method, 35).should == true
115
+ end
116
+
117
+ should 'not be valid if the value is older than the age limit' do
118
+ @cm.cached_value_is_still_valid?(@value, Time.now - 30, @obj, @method, 5).should == false
119
+ end
120
+ end
121
+
122
+ context 'checking with block' do
123
+ should 'pass the proper fields to the proc' do
124
+ now = Time.now
125
+ proc = lambda { |a,b,c|}
126
+ proc.expects(:call).with(now, @obj, @method)
127
+
128
+ @cm.cached_value_is_still_valid?(@value, now, @obj, @method, proc)
129
+ end
130
+
131
+ should 'only pass time if the proc only takes two args' do
132
+ now = Time.now
133
+ proc = lambda { |a|}
134
+ proc.expects(:call).with(now)
135
+
136
+ @cm.cached_value_is_still_valid?(@value, now, @obj, @method, proc)
137
+ end
138
+ end
139
+
140
+ context 'checking with symbol' do
141
+ setup do
142
+ @object = Object.new
143
+ def @object.the_method(a,b)
144
+ 'blah'
145
+ end
146
+
147
+ end
148
+
149
+ should 'call the method for the symbol on the object' do
150
+ @cm.cached_value_is_still_valid?(@value, Time.now, @object, @method, :the_method).should == 'blah'
151
+ end
152
+
153
+ end
154
+
155
+ should 'raise exceptions if options is incorrect'
156
+
157
+ end
158
+
159
+ context 'cache lookup' do
160
+ should 'call for the value (and return it) if nothing was cached' do
161
+ @cm.expects(:fetch_and_parse).with(@obj, @method, nil).returns(nil, nil)
162
+ @cm.expects(:call_and_store_value).with(@obj, @method, nil).returns(@value)
163
+ @cm.lookup(@obj, @method, nil).should == @value
164
+ end
165
+
166
+ should 'call for the value (and return it) if the value is no longer valid' do
167
+ now = Time.now
168
+ @cm.expects(:fetch_and_parse).with(@obj, @method, nil).returns([2, now])
169
+ @cm.expects(:cached_value_is_still_valid?).with(@value, now, @obj, @method, 2).returns(false)
170
+ @cm.expects(:call_and_store_value).with(@obj, @method, nil).returns(@value)
171
+ @cm.lookup(@obj, @method, 2).should == @value
172
+ end
173
+
174
+ should 'not call and store if the cached value is valid' do
175
+ @cm.expects(:fetch_and_parse).with(@obj, @method, nil).returns([2, Time.now])
176
+ @cm.expects(:cached_value_is_still_valid?).returns(true)
177
+ @cm.expects(:call_and_store_value).never
178
+ @cm.lookup(@obj, @method, 2).should == @value
179
+ end
180
+
181
+ should 'be able to cache a nil value' do
182
+ @cm.expects(:fetch_and_parse).with(@obj, @method, nil).returns([nil, Time.now])
183
+ @cm.expects(:cached_value_is_still_valid?).returns(true)
184
+ @cm.expects(:call_and_store_value).never
185
+ @cm.lookup(@obj, @method, 2).should == nil
186
+ end
187
+
188
+ end
189
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ require 'cache_value/cache_value'
4
+
5
+ class CacheTestClass
6
+ extend CacheValue::ClassMethods
7
+
8
+ def do_something
9
+ 'blech'
10
+ end
11
+
12
+ cache_value :do_something, 'yo'
13
+
14
+ end
15
+
16
+ class CacheValueTest < Test::Unit::TestCase
17
+
18
+ def setup
19
+ @cacher = CacheTestClass.new
20
+ end
21
+
22
+ should 'delegate the cache lookup to CacheMachine' do
23
+ CacheValue::CacheMachine.expects(:lookup).with(@cacher, :do_something, 'yo')
24
+ @cacher.do_something
25
+ end
26
+
27
+ context 'aliased methods' do
28
+
29
+ context 'generate caching method names' do
30
+ should 'generate vanilla names' do
31
+ @cacher.class.caching_method_names(:vanilla).should == %w{ vanilla_without_value_caching vanilla_with_value_caching }
32
+ end
33
+
34
+ should 'generate chocolate! names' do
35
+ @cacher.class.caching_method_names(:chocolate!).should == %w{ chocolate_without_value_caching! chocolate_with_value_caching! }
36
+ end
37
+
38
+ should 'generate strawberry? names' do
39
+ @cacher.class.caching_method_names(:strawberry?).should == %w{ strawberry_without_value_caching? strawberry_with_value_caching? }
40
+ end
41
+
42
+ end
43
+
44
+ should 'have aliased a with method' do
45
+ @cacher.respond_to?(:do_something_with_value_caching).should == true
46
+ end
47
+
48
+ should 'have aliased a without method' do
49
+ @cacher.respond_to?(:do_something_without_value_caching).should == true
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'matchy'
5
+ require 'mocha'
6
+
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+ require 'cache_value'
10
+
11
+ class Test::Unit::TestCase
12
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tobias-cache_value
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Tobias Crawley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: tcrawley@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - cache_value.gemspec
33
+ - lib/cache_value.rb
34
+ - lib/cache_value/cache_machine.rb
35
+ - lib/cache_value/cache_value.rb
36
+ - lib/cache_value/util.rb
37
+ - test/cache_machine_test.rb
38
+ - test/cache_value_test.rb
39
+ - test/test_helper.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/tobias/cache_value
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: Easy value caching
66
+ test_files:
67
+ - test/cache_machine_test.rb
68
+ - test/cache_value_test.rb
69
+ - test/test_helper.rb