itrigga-file_cache 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/itrigga/cache/itrigga-file_cache.rb +1 -1
- data/spec/file_cache_spec.rb +55 -55
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -20,7 +20,7 @@ Optional params:
|
|
20
20
|
Example usage:
|
21
21
|
|
22
22
|
|
23
|
-
@stats_json =
|
23
|
+
@stats_json = Itrigga::Cache::FileCache.with_cache(:cache_key=>'admin_stats.json', :timeout_seconds=>600){
|
24
24
|
/* some expensive remote API / slow IO call here /*
|
25
25
|
}
|
26
26
|
|
data/Rakefile
CHANGED
@@ -38,7 +38,7 @@ Optional params:
|
|
38
38
|
Example usage:
|
39
39
|
|
40
40
|
|
41
|
-
@stats_json =
|
41
|
+
@stats_json = Itrigga::Cache::FileCache.with_cache(:cache_key=>'admin_stats.json', :timeout_seconds=>600){
|
42
42
|
/* some expensive remote API / slow IO call here /*
|
43
43
|
}}
|
44
44
|
gem.email = "aldavidson@trigga.com"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/spec/file_cache_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require File.join( File.dirname(__FILE__), 'spec_helper' )
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Itrigga::Cache::FileCache do
|
4
4
|
|
5
5
|
describe "file_path" do
|
6
6
|
before(:each) do
|
@@ -9,79 +9,79 @@ describe Trigga::Cache::FileCache do
|
|
9
9
|
|
10
10
|
context "when not given a cache_key" do
|
11
11
|
it "should raise an ArgumentError" do
|
12
|
-
lambda{
|
12
|
+
lambda{ Itrigga::Cache::FileCache.file_path(:blart=>'flange') }.should raise_error(ArgumentError)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
context "when given a cache_key" do
|
16
16
|
it "should not raise an ArgumentError" do
|
17
|
-
lambda{
|
17
|
+
lambda{ Itrigga::Cache::FileCache.file_path(@opts) }.should_not raise_error(ArgumentError)
|
18
18
|
end
|
19
19
|
|
20
20
|
context "when not given a cache_dir" do
|
21
21
|
it "should default cache_dir to the module's attribute" do
|
22
|
-
|
23
|
-
|
22
|
+
Itrigga::Cache::FileCache.cache_dir = "/blart/flange/foo/bar/"
|
23
|
+
Itrigga::Cache::FileCache.file_path(@opts)
|
24
24
|
@opts[:cache_dir].should == "/blart/flange/foo/bar/"
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should expand the cache_dir plus cache_key" do
|
29
29
|
File.should_receive(:expand_path).with( "/blart/flange/foo/bar/baz.cache" ).and_return("blart")
|
30
|
-
|
30
|
+
Itrigga::Cache::FileCache.file_path( :cache_dir=> "/blart/flange/foo/bar", :cache_key => "baz.cache")
|
31
31
|
end
|
32
32
|
|
33
33
|
it "should return the expanded path" do
|
34
34
|
File.stub!(:expand_path).and_return("blart")
|
35
|
-
|
35
|
+
Itrigga::Cache::FileCache.file_path( :cache_dir=> "/blart/flange/foo/bar", :cache_key => "baz.cache").should == "blart"
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
40
|
describe "expired?" do
|
41
41
|
before(:each) do
|
42
|
-
|
42
|
+
Itrigga::Cache::FileCache.stub!(:file_path).and_return('/foo/bar/baz.txt')
|
43
43
|
File.stub!(:mtime).and_return(Time.now - 10)
|
44
44
|
@opts = {}
|
45
45
|
end
|
46
46
|
|
47
47
|
context "when not given :timeout_seconds" do
|
48
48
|
it "should default :timeout_seconds to the module's attribute" do
|
49
|
-
|
50
|
-
|
49
|
+
Itrigga::Cache::FileCache.timeout_seconds = 345
|
50
|
+
Itrigga::Cache::FileCache.expired?(@opts)
|
51
51
|
@opts[:timeout_seconds].should == 345
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
55
|
it "should get the file_path for the given opts" do
|
56
|
-
|
57
|
-
|
56
|
+
Itrigga::Cache::FileCache.should_receive(:file_path).with(hash_including( {:blart=>'flange'} )).and_return('/foo/bar/baz.txt')
|
57
|
+
Itrigga::Cache::FileCache.expired?( :blart=>'flange' )
|
58
58
|
end
|
59
59
|
|
60
60
|
it "should get the mtime of the file_path" do
|
61
61
|
File.should_receive(:mtime).with('/foo/bar/baz.txt').and_return(Time.now - 10)
|
62
|
-
|
62
|
+
Itrigga::Cache::FileCache.expired?
|
63
63
|
end
|
64
64
|
|
65
65
|
context "when the file is dated more than timeout_seconds ago" do
|
66
66
|
it "should return true" do
|
67
|
-
|
67
|
+
Itrigga::Cache::FileCache.expired?(:timeout_seconds=>9).should == true
|
68
68
|
end
|
69
69
|
end
|
70
70
|
context "when the file is dated less than timeout_seconds ago" do
|
71
71
|
it "should return false" do
|
72
|
-
|
72
|
+
Itrigga::Cache::FileCache.expired?(:timeout_seconds=>11).should == false
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
77
77
|
describe "is_in_cache?" do
|
78
78
|
before(:each) do
|
79
|
-
|
79
|
+
Itrigga::Cache::FileCache.stub!(:file_path).and_return('/foo/bar/baz.txt')
|
80
80
|
end
|
81
81
|
|
82
82
|
it "should get the file_path for the given opts" do
|
83
|
-
|
84
|
-
|
83
|
+
Itrigga::Cache::FileCache.should_receive(:file_path).with(hash_including( {:blart=>'flange'} )).and_return('/foo/bar/baz.txt')
|
84
|
+
Itrigga::Cache::FileCache.is_in_cache?( :blart=>'flange' )
|
85
85
|
end
|
86
86
|
|
87
87
|
context "when the file_path exists" do
|
@@ -90,7 +90,7 @@ describe Trigga::Cache::FileCache do
|
|
90
90
|
end
|
91
91
|
|
92
92
|
it "should return true" do
|
93
|
-
|
93
|
+
Itrigga::Cache::FileCache.is_in_cache?.should == true
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
@@ -100,7 +100,7 @@ describe Trigga::Cache::FileCache do
|
|
100
100
|
end
|
101
101
|
|
102
102
|
it "should return false" do
|
103
|
-
|
103
|
+
Itrigga::Cache::FileCache.is_in_cache?.should == false
|
104
104
|
end
|
105
105
|
end
|
106
106
|
end
|
@@ -108,55 +108,55 @@ describe Trigga::Cache::FileCache do
|
|
108
108
|
describe "with_cache" do
|
109
109
|
context "when the entry is in cache" do
|
110
110
|
before(:each) do
|
111
|
-
|
111
|
+
Itrigga::Cache::FileCache.stub!(:is_in_cache?).and_return(true)
|
112
112
|
end
|
113
113
|
|
114
114
|
context "and has not expired" do
|
115
115
|
before(:each) do
|
116
|
-
|
117
|
-
|
116
|
+
Itrigga::Cache::FileCache.stub!(:expired?).and_return(false)
|
117
|
+
Itrigga::Cache::FileCache.stub!(:get).and_return('result')
|
118
118
|
end
|
119
119
|
|
120
120
|
it "should get the entry" do
|
121
|
-
|
122
|
-
|
121
|
+
Itrigga::Cache::FileCache.should_receive(:get).with( :blart=>'flange' ).and_return('result')
|
122
|
+
Itrigga::Cache::FileCache.with_cache(:blart=>'flange')
|
123
123
|
end
|
124
124
|
|
125
125
|
it "should return the results of the get call" do
|
126
|
-
|
126
|
+
Itrigga::Cache::FileCache.with_cache.should == 'result'
|
127
127
|
end
|
128
128
|
|
129
129
|
it "should not call the given block" do
|
130
130
|
@called = false
|
131
|
-
|
131
|
+
Itrigga::Cache::FileCache.with_cache{ @called = true }
|
132
132
|
@called.should_not == true
|
133
133
|
end
|
134
134
|
end
|
135
135
|
context "but has expired" do
|
136
136
|
before(:each) do
|
137
|
-
|
138
|
-
|
139
|
-
|
137
|
+
Itrigga::Cache::FileCache.stub!(:expired?).and_return(true)
|
138
|
+
Itrigga::Cache::FileCache.stub!(:delete)
|
139
|
+
Itrigga::Cache::FileCache.stub!(:put)
|
140
140
|
end
|
141
141
|
|
142
142
|
it "should delete the existing entry" do
|
143
|
-
|
144
|
-
|
143
|
+
Itrigga::Cache::FileCache.should_receive(:delete).with(:blart=>'flange')
|
144
|
+
Itrigga::Cache::FileCache.with_cache(:blart=>'flange') {}
|
145
145
|
end
|
146
146
|
|
147
147
|
it "should call the given block" do
|
148
148
|
@called = false
|
149
|
-
|
149
|
+
Itrigga::Cache::FileCache.with_cache{ @called = true }
|
150
150
|
@called.should == true
|
151
151
|
end
|
152
152
|
|
153
153
|
it "should return the result of the block call" do
|
154
|
-
|
154
|
+
Itrigga::Cache::FileCache.with_cache{ 'donkeys' }.should == 'donkeys'
|
155
155
|
end
|
156
156
|
|
157
157
|
it "should put the result to the cache" do
|
158
|
-
|
159
|
-
|
158
|
+
Itrigga::Cache::FileCache.should_receive(:put).with( {:blart=>'flange'}, 'cheese' )
|
159
|
+
Itrigga::Cache::FileCache.with_cache(:blart=>'flange'){ 'cheese' }
|
160
160
|
end
|
161
161
|
end
|
162
162
|
end
|
@@ -164,85 +164,85 @@ describe Trigga::Cache::FileCache do
|
|
164
164
|
|
165
165
|
describe "get" do
|
166
166
|
before(:each) do
|
167
|
-
|
167
|
+
Itrigga::Cache::FileCache.stub!(:file_path).and_return('/foo/bar/baz.txt')
|
168
168
|
File.stub!(:open).and_yield(@mock_file = mock('file'))
|
169
169
|
@mock_file.stub!(:read).and_return("contents")
|
170
170
|
end
|
171
171
|
|
172
172
|
it "should get the file_path for the given opts" do
|
173
|
-
|
174
|
-
|
173
|
+
Itrigga::Cache::FileCache.should_receive(:file_path).with(hash_including( {:blart=>'flange'} )).and_return('/foo/bar/baz.txt')
|
174
|
+
Itrigga::Cache::FileCache.get( :blart=>'flange' )
|
175
175
|
end
|
176
176
|
|
177
177
|
it "should open the file in read-only mode" do
|
178
178
|
File.should_receive(:open).with('/foo/bar/baz.txt','r').and_return(@mock_file)
|
179
|
-
|
179
|
+
Itrigga::Cache::FileCache.get( :blart=>'flange' )
|
180
180
|
end
|
181
181
|
|
182
182
|
|
183
183
|
it "should read the file" do
|
184
184
|
@mock_file.should_receive(:read).and_return("contents")
|
185
|
-
|
185
|
+
Itrigga::Cache::FileCache.get( :blart=>'flange' )
|
186
186
|
end
|
187
187
|
|
188
188
|
it "should return the file contents" do
|
189
|
-
|
189
|
+
Itrigga::Cache::FileCache.get( :blart=>'flange' ).should == 'contents'
|
190
190
|
end
|
191
191
|
end
|
192
192
|
|
193
193
|
describe "delete" do
|
194
194
|
before(:each) do
|
195
|
-
|
196
|
-
|
195
|
+
Itrigga::Cache::FileCache.stub!(:file_path).and_return('/foo/bar/baz.txt')
|
196
|
+
Itrigga::Cache::FileCache.stub!(:exists?).and_return(true)
|
197
197
|
File.stub!(:delete).and_return(@mock_file = mock('file'))
|
198
198
|
end
|
199
199
|
|
200
200
|
it "should get the file_path for the given opts" do
|
201
|
-
|
202
|
-
|
201
|
+
Itrigga::Cache::FileCache.should_receive(:file_path).with(hash_including( {:blart=>'flange'} )).and_return('/foo/bar/baz.txt')
|
202
|
+
Itrigga::Cache::FileCache.delete( :blart=>'flange' )
|
203
203
|
end
|
204
204
|
|
205
205
|
context "when the file is_in_cache" do
|
206
206
|
before(:each) do
|
207
|
-
|
207
|
+
Itrigga::Cache::FileCache.stub!(:is_in_cache?).and_return(true)
|
208
208
|
end
|
209
209
|
it "should delete the file" do
|
210
210
|
File.should_receive(:delete).with('/foo/bar/baz.txt')
|
211
|
-
|
211
|
+
Itrigga::Cache::FileCache.delete( :blart=>'flange' )
|
212
212
|
end
|
213
213
|
end
|
214
214
|
|
215
215
|
context "when the file is not in cache" do
|
216
216
|
before(:each) do
|
217
|
-
|
217
|
+
Itrigga::Cache::FileCache.stub!(:is_in_cache?).and_return(false)
|
218
218
|
end
|
219
219
|
it "should not delete the file" do
|
220
220
|
File.should_not_receive(:delete)
|
221
|
-
|
221
|
+
Itrigga::Cache::FileCache.delete( :blart=>'flange' )
|
222
222
|
end
|
223
223
|
end
|
224
224
|
end
|
225
225
|
|
226
226
|
describe "put" do
|
227
227
|
before(:each) do
|
228
|
-
|
228
|
+
Itrigga::Cache::FileCache.stub!(:file_path).and_return('/foo/bar/baz.txt')
|
229
229
|
File.stub!(:open).and_yield( @mock_file = mock('file') )
|
230
230
|
@mock_file.stub!(:write)
|
231
231
|
end
|
232
232
|
|
233
233
|
it "should get the file_path for the given opts" do
|
234
|
-
|
235
|
-
|
234
|
+
Itrigga::Cache::FileCache.should_receive(:file_path).with(hash_including( {:blart=>'flange'} )).and_return('/foo/bar/baz.txt')
|
235
|
+
Itrigga::Cache::FileCache.put( {:blart=>'flange'}, 'content' )
|
236
236
|
end
|
237
237
|
|
238
238
|
it "should open the file for writing" do
|
239
239
|
File.should_receive(:open).with('/foo/bar/baz.txt', 'w')
|
240
|
-
|
240
|
+
Itrigga::Cache::FileCache.put( {:blart=>'flange'}, 'content' )
|
241
241
|
end
|
242
242
|
|
243
243
|
it "should write the given content to the file" do
|
244
244
|
@mock_file.should_receive(:write).with('content')
|
245
|
-
|
245
|
+
Itrigga::Cache::FileCache.put( {:blart=>'flange'}, 'content' )
|
246
246
|
end
|
247
247
|
|
248
248
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: itrigga-file_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Al Davidson
|
@@ -99,7 +99,7 @@ description: |-
|
|
99
99
|
Example usage:
|
100
100
|
|
101
101
|
|
102
|
-
@stats_json =
|
102
|
+
@stats_json = Itrigga::Cache::FileCache.with_cache(:cache_key=>'admin_stats.json', :timeout_seconds=>600){
|
103
103
|
/* some expensive remote API / slow IO call here /*
|
104
104
|
}
|
105
105
|
email: aldavidson@trigga.com
|