itrigga-file_cache 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +13 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +41 -0
- data/Rakefile +76 -0
- data/VERSION +1 -0
- data/lib/itrigga/cache/itrigga-file_cache.rb +74 -0
- data/lib/itrigga/file_cache.rb +1 -0
- data/spec/file_cache_spec.rb +250 -0
- data/spec/spec_helper.rb +22 -0
- data/test/helper.rb +18 -0
- data/test/test_itrigga-file_cache.rb +7 -0
- metadata +162 -0
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "bundler", "~> 1.0.0"
|
10
|
+
gem "jeweler", "~> 1.6.4"
|
11
|
+
gem "rcov", ">= 0"
|
12
|
+
gem 'rspec'
|
13
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Al Davidson
|
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,41 @@
|
|
1
|
+
= itrigga-file_cache
|
2
|
+
|
3
|
+
A simple file-system-based cache wrapper.
|
4
|
+
|
5
|
+
The main method is 'with_cache( :cache_key=>'something_unique', :timeout_seconds=>(an integer) ){ (...) }'
|
6
|
+
If the given cache key exists and has not timed out, it will return the cached value
|
7
|
+
If not, it will
|
8
|
+
* yield to the given block
|
9
|
+
* store the result of the given block in the cache with the given key
|
10
|
+
* return the result of the given block
|
11
|
+
|
12
|
+
Required params:
|
13
|
+
* :cache_key=>'some unique string that is valid in a filename'
|
14
|
+
|
15
|
+
Optional params:
|
16
|
+
* :timeout_seconds => (an integer - default 3600)
|
17
|
+
* :cache_dir => (an absolute path - defaults to RAILS_ROOT/tmp/cache if RAILS_ROOT is defined, otherwise /tmp/cache )
|
18
|
+
|
19
|
+
|
20
|
+
Example usage:
|
21
|
+
|
22
|
+
|
23
|
+
@stats_json = Trigga::Cache::FileCache.with_cache(:cache_key=>'admin_stats.json', :timeout_seconds=>600){
|
24
|
+
/* some expensive remote API / slow IO call here /*
|
25
|
+
}
|
26
|
+
|
27
|
+
== Contributing to itrigga-file_cache
|
28
|
+
|
29
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
30
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
31
|
+
* Fork the project
|
32
|
+
* Start a feature/bugfix branch
|
33
|
+
* Commit and push until you are happy with your contribution
|
34
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
35
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
36
|
+
|
37
|
+
== Copyright
|
38
|
+
|
39
|
+
Copyright (c) 2011 Al Davidson. See LICENSE.txt for
|
40
|
+
further details.
|
41
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "itrigga-file_cache"
|
18
|
+
gem.homepage = "http://github.com/itrigga/itrigga-file_cache"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{A simple file-system-based cache wrapper.}
|
21
|
+
gem.description = %Q{A simple file-system-based cache wrapper.
|
22
|
+
|
23
|
+
The main method is 'with_cache( :cache_key=>'something_unique', :timeout_seconds=>(an integer) ){ (...) }'
|
24
|
+
If the given cache key exists and has not timed out, it will return the cached value
|
25
|
+
If not, it will
|
26
|
+
* yield to the given block
|
27
|
+
* store the result of the given block in the cache with the given key
|
28
|
+
* return the result of the given block
|
29
|
+
|
30
|
+
Required params:
|
31
|
+
* :cache_key=>'some unique string that is valid in a filename'
|
32
|
+
|
33
|
+
Optional params:
|
34
|
+
* :timeout_seconds => (an integer - default 3600)
|
35
|
+
* :cache_dir => (an absolute path - defaults to RAILS_ROOT/tmp/cache if RAILS_ROOT is defined, otherwise /tmp/cache )
|
36
|
+
|
37
|
+
|
38
|
+
Example usage:
|
39
|
+
|
40
|
+
|
41
|
+
@stats_json = Trigga::Cache::FileCache.with_cache(:cache_key=>'admin_stats.json', :timeout_seconds=>600){
|
42
|
+
/* some expensive remote API / slow IO call here /*
|
43
|
+
}}
|
44
|
+
gem.email = "aldavidson@trigga.com"
|
45
|
+
gem.authors = ["Al Davidson"]
|
46
|
+
# dependencies defined in Gemfile
|
47
|
+
gem.require_paths << "lib/itrigga"
|
48
|
+
end
|
49
|
+
Jeweler::RubygemsDotOrgTasks.new
|
50
|
+
|
51
|
+
require 'rake/testtask'
|
52
|
+
Rake::TestTask.new(:test) do |test|
|
53
|
+
test.libs << 'lib' << 'test'
|
54
|
+
test.pattern = 'test/**/test_*.rb'
|
55
|
+
test.verbose = true
|
56
|
+
end
|
57
|
+
|
58
|
+
require 'rcov/rcovtask'
|
59
|
+
Rcov::RcovTask.new do |test|
|
60
|
+
test.libs << 'test'
|
61
|
+
test.pattern = 'test/**/test_*.rb'
|
62
|
+
test.verbose = true
|
63
|
+
test.rcov_opts << '--exclude "gems/*"'
|
64
|
+
end
|
65
|
+
|
66
|
+
task :default => :test
|
67
|
+
|
68
|
+
require 'rake/rdoctask'
|
69
|
+
Rake::RDocTask.new do |rdoc|
|
70
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
71
|
+
|
72
|
+
rdoc.rdoc_dir = 'rdoc'
|
73
|
+
rdoc.title = "itrigga-file_cache #{version}"
|
74
|
+
rdoc.rdoc_files.include('README*')
|
75
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
76
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,74 @@
|
|
1
|
+
#require 'active_support'
|
2
|
+
|
3
|
+
module Trigga
|
4
|
+
module Cache
|
5
|
+
module FileCache
|
6
|
+
|
7
|
+
module_function
|
8
|
+
|
9
|
+
|
10
|
+
@cache_dir = "#{defined?(RAILS_ROOT) ? RAILS_ROOT : ''}/tmp/cache"
|
11
|
+
@timeout_seconds = 3600 # <- default timeout is 1hr
|
12
|
+
|
13
|
+
def cache_dir
|
14
|
+
@cache_dir
|
15
|
+
end
|
16
|
+
|
17
|
+
def cache_dir=(dir)
|
18
|
+
@cache_dir=dir
|
19
|
+
end
|
20
|
+
|
21
|
+
def timeout_seconds
|
22
|
+
@timeout_seconds
|
23
|
+
end
|
24
|
+
|
25
|
+
def timeout_seconds=(secs)
|
26
|
+
@timeout_seconds = secs
|
27
|
+
end
|
28
|
+
|
29
|
+
def with_cache( opts={}, &block )
|
30
|
+
begin
|
31
|
+
if is_in_cache?(opts) && !expired?(opts)
|
32
|
+
get(opts)
|
33
|
+
else
|
34
|
+
delete(opts)
|
35
|
+
result = block.call
|
36
|
+
put( opts, result )
|
37
|
+
result
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def get(opts)
|
43
|
+
File.open( file_path(opts), 'r' ){ |f| f.read }
|
44
|
+
end
|
45
|
+
|
46
|
+
def put(opts, content)
|
47
|
+
File.open( file_path(opts), 'w' ){ |f| f.write(content) }
|
48
|
+
end
|
49
|
+
|
50
|
+
def delete(opts)
|
51
|
+
File.delete(file_path(opts)) if is_in_cache?(opts)
|
52
|
+
end
|
53
|
+
|
54
|
+
def is_in_cache?( opts={} )
|
55
|
+
File.exists?( file_path(opts) )
|
56
|
+
end
|
57
|
+
|
58
|
+
def expired?( opts={} )
|
59
|
+
opts[:timeout_seconds] ||= self.timeout_seconds
|
60
|
+
Time.now - File.mtime(file_path(opts)) > opts[:timeout_seconds]
|
61
|
+
end
|
62
|
+
|
63
|
+
def file_path( opts )
|
64
|
+
raise ArgumentError.new(":cache_key is required") unless opts[:cache_key]
|
65
|
+
|
66
|
+
opts[:cache_dir] ||= self.cache_dir
|
67
|
+
|
68
|
+
File.expand_path( File.join(opts[:cache_dir], opts[:cache_key]) )
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'cache', 'itrigga-file_cache')
|
@@ -0,0 +1,250 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), 'spec_helper' )
|
2
|
+
|
3
|
+
describe Trigga::Cache::FileCache do
|
4
|
+
|
5
|
+
describe "file_path" do
|
6
|
+
before(:each) do
|
7
|
+
@opts = {:blart=>'flange', :cache_key=>'cache_key'}
|
8
|
+
end
|
9
|
+
|
10
|
+
context "when not given a cache_key" do
|
11
|
+
it "should raise an ArgumentError" do
|
12
|
+
lambda{ Trigga::Cache::FileCache.file_path(:blart=>'flange') }.should raise_error(ArgumentError)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
context "when given a cache_key" do
|
16
|
+
it "should not raise an ArgumentError" do
|
17
|
+
lambda{ Trigga::Cache::FileCache.file_path(@opts) }.should_not raise_error(ArgumentError)
|
18
|
+
end
|
19
|
+
|
20
|
+
context "when not given a cache_dir" do
|
21
|
+
it "should default cache_dir to the module's attribute" do
|
22
|
+
Trigga::Cache::FileCache.cache_dir = "/blart/flange/foo/bar/"
|
23
|
+
Trigga::Cache::FileCache.file_path(@opts)
|
24
|
+
@opts[:cache_dir].should == "/blart/flange/foo/bar/"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should expand the cache_dir plus cache_key" do
|
29
|
+
File.should_receive(:expand_path).with( "/blart/flange/foo/bar/baz.cache" ).and_return("blart")
|
30
|
+
Trigga::Cache::FileCache.file_path( :cache_dir=> "/blart/flange/foo/bar", :cache_key => "baz.cache")
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should return the expanded path" do
|
34
|
+
File.stub!(:expand_path).and_return("blart")
|
35
|
+
Trigga::Cache::FileCache.file_path( :cache_dir=> "/blart/flange/foo/bar", :cache_key => "baz.cache").should == "blart"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "expired?" do
|
41
|
+
before(:each) do
|
42
|
+
Trigga::Cache::FileCache.stub!(:file_path).and_return('/foo/bar/baz.txt')
|
43
|
+
File.stub!(:mtime).and_return(Time.now - 10)
|
44
|
+
@opts = {}
|
45
|
+
end
|
46
|
+
|
47
|
+
context "when not given :timeout_seconds" do
|
48
|
+
it "should default :timeout_seconds to the module's attribute" do
|
49
|
+
Trigga::Cache::FileCache.timeout_seconds = 345
|
50
|
+
Trigga::Cache::FileCache.expired?(@opts)
|
51
|
+
@opts[:timeout_seconds].should == 345
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should get the file_path for the given opts" do
|
56
|
+
Trigga::Cache::FileCache.should_receive(:file_path).with(hash_including( {:blart=>'flange'} )).and_return('/foo/bar/baz.txt')
|
57
|
+
Trigga::Cache::FileCache.expired?( :blart=>'flange' )
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should get the mtime of the file_path" do
|
61
|
+
File.should_receive(:mtime).with('/foo/bar/baz.txt').and_return(Time.now - 10)
|
62
|
+
Trigga::Cache::FileCache.expired?
|
63
|
+
end
|
64
|
+
|
65
|
+
context "when the file is dated more than timeout_seconds ago" do
|
66
|
+
it "should return true" do
|
67
|
+
Trigga::Cache::FileCache.expired?(:timeout_seconds=>9).should == true
|
68
|
+
end
|
69
|
+
end
|
70
|
+
context "when the file is dated less than timeout_seconds ago" do
|
71
|
+
it "should return false" do
|
72
|
+
Trigga::Cache::FileCache.expired?(:timeout_seconds=>11).should == false
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "is_in_cache?" do
|
78
|
+
before(:each) do
|
79
|
+
Trigga::Cache::FileCache.stub!(:file_path).and_return('/foo/bar/baz.txt')
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should get the file_path for the given opts" do
|
83
|
+
Trigga::Cache::FileCache.should_receive(:file_path).with(hash_including( {:blart=>'flange'} )).and_return('/foo/bar/baz.txt')
|
84
|
+
Trigga::Cache::FileCache.is_in_cache?( :blart=>'flange' )
|
85
|
+
end
|
86
|
+
|
87
|
+
context "when the file_path exists" do
|
88
|
+
before(:each) do
|
89
|
+
File.stub!(:exists?).with('/foo/bar/baz.txt').and_return(true)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should return true" do
|
93
|
+
Trigga::Cache::FileCache.is_in_cache?.should == true
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context "when the file_path does not exist" do
|
98
|
+
before(:each) do
|
99
|
+
File.stub!(:exists?).with('/foo/bar/baz.txt').and_return(false)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should return false" do
|
103
|
+
Trigga::Cache::FileCache.is_in_cache?.should == false
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "with_cache" do
|
109
|
+
context "when the entry is in cache" do
|
110
|
+
before(:each) do
|
111
|
+
Trigga::Cache::FileCache.stub!(:is_in_cache?).and_return(true)
|
112
|
+
end
|
113
|
+
|
114
|
+
context "and has not expired" do
|
115
|
+
before(:each) do
|
116
|
+
Trigga::Cache::FileCache.stub!(:expired?).and_return(false)
|
117
|
+
Trigga::Cache::FileCache.stub!(:get).and_return('result')
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should get the entry" do
|
121
|
+
Trigga::Cache::FileCache.should_receive(:get).with( :blart=>'flange' ).and_return('result')
|
122
|
+
Trigga::Cache::FileCache.with_cache(:blart=>'flange')
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should return the results of the get call" do
|
126
|
+
Trigga::Cache::FileCache.with_cache.should == 'result'
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should not call the given block" do
|
130
|
+
@called = false
|
131
|
+
Trigga::Cache::FileCache.with_cache{ @called = true }
|
132
|
+
@called.should_not == true
|
133
|
+
end
|
134
|
+
end
|
135
|
+
context "but has expired" do
|
136
|
+
before(:each) do
|
137
|
+
Trigga::Cache::FileCache.stub!(:expired?).and_return(true)
|
138
|
+
Trigga::Cache::FileCache.stub!(:delete)
|
139
|
+
Trigga::Cache::FileCache.stub!(:put)
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should delete the existing entry" do
|
143
|
+
Trigga::Cache::FileCache.should_receive(:delete).with(:blart=>'flange')
|
144
|
+
Trigga::Cache::FileCache.with_cache(:blart=>'flange') {}
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should call the given block" do
|
148
|
+
@called = false
|
149
|
+
Trigga::Cache::FileCache.with_cache{ @called = true }
|
150
|
+
@called.should == true
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should return the result of the block call" do
|
154
|
+
Trigga::Cache::FileCache.with_cache{ 'donkeys' }.should == 'donkeys'
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should put the result to the cache" do
|
158
|
+
Trigga::Cache::FileCache.should_receive(:put).with( {:blart=>'flange'}, 'cheese' )
|
159
|
+
Trigga::Cache::FileCache.with_cache(:blart=>'flange'){ 'cheese' }
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe "get" do
|
166
|
+
before(:each) do
|
167
|
+
Trigga::Cache::FileCache.stub!(:file_path).and_return('/foo/bar/baz.txt')
|
168
|
+
File.stub!(:open).and_yield(@mock_file = mock('file'))
|
169
|
+
@mock_file.stub!(:read).and_return("contents")
|
170
|
+
end
|
171
|
+
|
172
|
+
it "should get the file_path for the given opts" do
|
173
|
+
Trigga::Cache::FileCache.should_receive(:file_path).with(hash_including( {:blart=>'flange'} )).and_return('/foo/bar/baz.txt')
|
174
|
+
Trigga::Cache::FileCache.get( :blart=>'flange' )
|
175
|
+
end
|
176
|
+
|
177
|
+
it "should open the file in read-only mode" do
|
178
|
+
File.should_receive(:open).with('/foo/bar/baz.txt','r').and_return(@mock_file)
|
179
|
+
Trigga::Cache::FileCache.get( :blart=>'flange' )
|
180
|
+
end
|
181
|
+
|
182
|
+
|
183
|
+
it "should read the file" do
|
184
|
+
@mock_file.should_receive(:read).and_return("contents")
|
185
|
+
Trigga::Cache::FileCache.get( :blart=>'flange' )
|
186
|
+
end
|
187
|
+
|
188
|
+
it "should return the file contents" do
|
189
|
+
Trigga::Cache::FileCache.get( :blart=>'flange' ).should == 'contents'
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
describe "delete" do
|
194
|
+
before(:each) do
|
195
|
+
Trigga::Cache::FileCache.stub!(:file_path).and_return('/foo/bar/baz.txt')
|
196
|
+
Trigga::Cache::FileCache.stub!(:exists?).and_return(true)
|
197
|
+
File.stub!(:delete).and_return(@mock_file = mock('file'))
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should get the file_path for the given opts" do
|
201
|
+
Trigga::Cache::FileCache.should_receive(:file_path).with(hash_including( {:blart=>'flange'} )).and_return('/foo/bar/baz.txt')
|
202
|
+
Trigga::Cache::FileCache.delete( :blart=>'flange' )
|
203
|
+
end
|
204
|
+
|
205
|
+
context "when the file is_in_cache" do
|
206
|
+
before(:each) do
|
207
|
+
Trigga::Cache::FileCache.stub!(:is_in_cache?).and_return(true)
|
208
|
+
end
|
209
|
+
it "should delete the file" do
|
210
|
+
File.should_receive(:delete).with('/foo/bar/baz.txt')
|
211
|
+
Trigga::Cache::FileCache.delete( :blart=>'flange' )
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
context "when the file is not in cache" do
|
216
|
+
before(:each) do
|
217
|
+
Trigga::Cache::FileCache.stub!(:is_in_cache?).and_return(false)
|
218
|
+
end
|
219
|
+
it "should not delete the file" do
|
220
|
+
File.should_not_receive(:delete)
|
221
|
+
Trigga::Cache::FileCache.delete( :blart=>'flange' )
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
describe "put" do
|
227
|
+
before(:each) do
|
228
|
+
Trigga::Cache::FileCache.stub!(:file_path).and_return('/foo/bar/baz.txt')
|
229
|
+
File.stub!(:open).and_yield( @mock_file = mock('file') )
|
230
|
+
@mock_file.stub!(:write)
|
231
|
+
end
|
232
|
+
|
233
|
+
it "should get the file_path for the given opts" do
|
234
|
+
Trigga::Cache::FileCache.should_receive(:file_path).with(hash_including( {:blart=>'flange'} )).and_return('/foo/bar/baz.txt')
|
235
|
+
Trigga::Cache::FileCache.put( {:blart=>'flange'}, 'content' )
|
236
|
+
end
|
237
|
+
|
238
|
+
it "should open the file for writing" do
|
239
|
+
File.should_receive(:open).with('/foo/bar/baz.txt', 'w')
|
240
|
+
Trigga::Cache::FileCache.put( {:blart=>'flange'}, 'content' )
|
241
|
+
end
|
242
|
+
|
243
|
+
it "should write the given content to the file" do
|
244
|
+
@mock_file.should_receive(:write).with('content')
|
245
|
+
Trigga::Cache::FileCache.put( {:blart=>'flange'}, 'content' )
|
246
|
+
end
|
247
|
+
|
248
|
+
end
|
249
|
+
|
250
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'rspec'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
|
16
|
+
require 'itrigga/file_cache'
|
17
|
+
|
18
|
+
Dir.chdir(File.join(File.dirname(__FILE__), '..'))
|
19
|
+
|
20
|
+
RSpec.configure do |config|
|
21
|
+
# some (optional) config here
|
22
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'itrigga-file_cache'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: itrigga-file_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Al Davidson
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-10-03 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
23
|
+
name: bundler
|
24
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 1.0.0
|
35
|
+
requirement: *id001
|
36
|
+
type: :development
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
prerelease: false
|
39
|
+
name: jeweler
|
40
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 7
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 6
|
49
|
+
- 4
|
50
|
+
version: 1.6.4
|
51
|
+
requirement: *id002
|
52
|
+
type: :development
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
prerelease: false
|
55
|
+
name: rcov
|
56
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirement: *id003
|
66
|
+
type: :development
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
prerelease: false
|
69
|
+
name: rspec
|
70
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
requirement: *id004
|
80
|
+
type: :development
|
81
|
+
description: |-
|
82
|
+
A simple file-system-based cache wrapper.
|
83
|
+
|
84
|
+
The main method is 'with_cache( :cache_key=>'something_unique', :timeout_seconds=>(an integer) ){ (...) }'
|
85
|
+
If the given cache key exists and has not timed out, it will return the cached value
|
86
|
+
If not, it will
|
87
|
+
* yield to the given block
|
88
|
+
* store the result of the given block in the cache with the given key
|
89
|
+
* return the result of the given block
|
90
|
+
|
91
|
+
Required params:
|
92
|
+
* :cache_key=>'some unique string that is valid in a filename'
|
93
|
+
|
94
|
+
Optional params:
|
95
|
+
* :timeout_seconds => (an integer - default 3600)
|
96
|
+
* :cache_dir => (an absolute path - defaults to RAILS_ROOT/tmp/cache if RAILS_ROOT is defined, otherwise /tmp/cache )
|
97
|
+
|
98
|
+
|
99
|
+
Example usage:
|
100
|
+
|
101
|
+
|
102
|
+
@stats_json = Trigga::Cache::FileCache.with_cache(:cache_key=>'admin_stats.json', :timeout_seconds=>600){
|
103
|
+
/* some expensive remote API / slow IO call here /*
|
104
|
+
}
|
105
|
+
email: aldavidson@trigga.com
|
106
|
+
executables: []
|
107
|
+
|
108
|
+
extensions: []
|
109
|
+
|
110
|
+
extra_rdoc_files:
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.rdoc
|
113
|
+
files:
|
114
|
+
- Gemfile
|
115
|
+
- LICENSE.txt
|
116
|
+
- README.rdoc
|
117
|
+
- Rakefile
|
118
|
+
- VERSION
|
119
|
+
- lib/itrigga/cache/itrigga-file_cache.rb
|
120
|
+
- lib/itrigga/file_cache.rb
|
121
|
+
- spec/file_cache_spec.rb
|
122
|
+
- spec/spec_helper.rb
|
123
|
+
- test/helper.rb
|
124
|
+
- test/test_itrigga-file_cache.rb
|
125
|
+
has_rdoc: true
|
126
|
+
homepage: http://github.com/itrigga/itrigga-file_cache
|
127
|
+
licenses:
|
128
|
+
- MIT
|
129
|
+
post_install_message:
|
130
|
+
rdoc_options: []
|
131
|
+
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
- lib/itrigga
|
135
|
+
- lib/itrigga
|
136
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
hash: 3
|
142
|
+
segments:
|
143
|
+
- 0
|
144
|
+
version: "0"
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
none: false
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
hash: 3
|
151
|
+
segments:
|
152
|
+
- 0
|
153
|
+
version: "0"
|
154
|
+
requirements: []
|
155
|
+
|
156
|
+
rubyforge_project:
|
157
|
+
rubygems_version: 1.3.7
|
158
|
+
signing_key:
|
159
|
+
specification_version: 3
|
160
|
+
summary: A simple file-system-based cache wrapper.
|
161
|
+
test_files: []
|
162
|
+
|