cache_value 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
+ Jeweler::GemcutterTasks.new
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.3.0
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{cache_value}
8
+ s.version = "0.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tobias Crawley"]
12
+ s.date = %q{2010-01-07}
13
+ s.email = %q{tcrawley@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "cache_value.gemspec",
26
+ "lib/cache_value.rb",
27
+ "lib/cache_value/cache_machine.rb",
28
+ "lib/cache_value/cache_value.rb",
29
+ "lib/cache_value/util.rb",
30
+ "test/cache_machine_test.rb",
31
+ "test/cache_value_test.rb",
32
+ "test/test_helper.rb",
33
+ "test/util_test.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/tobias/cache_value}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.5}
39
+ s.summary = %q{Easy value caching}
40
+ s.test_files = [
41
+ "test/cache_machine_test.rb",
42
+ "test/cache_value_test.rb",
43
+ "test/test_helper.rb",
44
+ "test/util_test.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
+ else
53
+ end
54
+ else
55
+ end
56
+ end
57
+
@@ -0,0 +1,80 @@
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
+
7
+ module CacheValue
8
+ class CacheMachine
9
+ extend Util
10
+
11
+ class << self
12
+
13
+ def cache_store=(*store_option)
14
+ @cache_store = store_option ? ActiveSupport::Cache.lookup_store(store_option) : nil
15
+ end
16
+
17
+ def cache_store
18
+ @cache_store ||= ActiveSupport::Cache.lookup_store(:file_store, default_storage_dir)
19
+ end
20
+
21
+ def default_storage_dir
22
+ if !defined?(RAILS_ROOT)
23
+ raise ConfigurationException.new('Not running under rails. Set the cache_store type ' +
24
+ 'and location manually using CacheValue::CacheMachine.cache_store=')
25
+ end
26
+
27
+ File.join(RAILS_ROOT, 'tmp', 'cache_value_caches')
28
+ end
29
+
30
+ def lookup(object, method, options, arguments = nil)
31
+ value, last_cached = fetch_and_parse(object, method, arguments)
32
+ if !last_cached or !cached_value_is_still_valid?(value, last_cached, object, method, options)
33
+ value = call_and_store_value(object, method, arguments)
34
+ end
35
+
36
+ value
37
+ end
38
+
39
+ def cache_key(object, method, arguments = nil)
40
+ if !object.respond_to?(:cache_key)
41
+ raise ConfigurationException.new("object of class #{object.class.name} does not respond to :cache_key")
42
+ end
43
+
44
+ key = object.cache_key.gsub('/', '_') + "_#{method}"
45
+ key << '_' + hex_digest(arguments) if arguments
46
+ key
47
+ end
48
+
49
+ def fetch_and_parse(object, method, arguments = nil)
50
+ data = cache_store.fetch(cache_key(object, method, arguments))
51
+ if data
52
+ YAML::load(data)
53
+ else
54
+ [nil, nil]
55
+ end
56
+ end
57
+
58
+ def call_and_store_value(object, method, arguments = nil)
59
+ without_method = caching_method_names(method).first
60
+ value = arguments ? object.send(without_method, *arguments) : object.send(without_method)
61
+ cache_store.write(cache_key(object, method, arguments), [value, Time.now].to_yaml)
62
+
63
+ value
64
+ end
65
+
66
+ def cached_value_is_still_valid?(value, cached_age, object, method, options)
67
+ options = object.send(:method, options) if options.is_a?(Symbol)
68
+ if options.respond_to?(:arity)
69
+ options.call(*([cached_age, object, method][0,options.arity]))
70
+ else
71
+ cached_age > Time.now - options
72
+ end
73
+ end
74
+
75
+ end
76
+ end
77
+
78
+ class ConfigurationException < StandardError
79
+ end
80
+ 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 |*args|
12
+ CacheValue::CacheMachine.lookup(self, method, option, args)
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,30 @@
1
+ require 'sha1'
2
+
3
+ module CacheValue
4
+ module Util
5
+ def caching_method_names(method)
6
+ washed_method = method.to_s.sub(/([?!=])$/, '')
7
+ punctuation = $1
8
+ ["#{washed_method}_without_value_caching#{punctuation}",
9
+ "#{washed_method}_with_value_caching#{punctuation}"]
10
+ end
11
+
12
+ def hex_digest(values)
13
+ Digest::SHA1.hexdigest(stringify_value(values))
14
+ end
15
+
16
+ protected
17
+ def stringify_value(value)
18
+ if value.respond_to?(:to_str)
19
+ value.to_str
20
+ elsif value.respond_to?(:cache_key)
21
+ value.cache_key.to_s
22
+ elsif value.respond_to?(:collect)
23
+ value.collect { |x| stringify_value(x) }.join
24
+ else
25
+ value.to_s
26
+ end
27
+ end
28
+
29
+ end
30
+ 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_#{@cm.send(:hex_digest, [1, 2, now])}$/, @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
data/test/util_test.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ require 'cache_value/util'
4
+
5
+
6
+ class UtilTest < Test::Unit::TestCase
7
+ include CacheValue::Util
8
+
9
+ should 'be true' do
10
+ assert true
11
+ end
12
+
13
+ should 'have some tests here'
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cache_value
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Tobias Crawley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-07 00:00:00 -05: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
+ - test/util_test.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/tobias/cache_value
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Easy value caching
69
+ test_files:
70
+ - test/cache_machine_test.rb
71
+ - test/cache_value_test.rb
72
+ - test/test_helper.rb
73
+ - test/util_test.rb