fat_cache 0.0.6 → 0.0.7
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/VERSION +1 -1
- data/lib/fat_cache.rb +19 -0
- data/spec/fat_cache_spec.rb +38 -0
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.7
|
data/lib/fat_cache.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'ruby-debug'
|
3
|
+
|
3
4
|
class FatCache
|
4
5
|
|
5
6
|
class << self
|
@@ -7,6 +8,8 @@ class FatCache
|
|
7
8
|
attr_accessor :fatcache, :indexed_fatcache
|
8
9
|
attr_accessor :fetchers, :index_fetchers
|
9
10
|
|
11
|
+
attr_accessor :logger
|
12
|
+
|
10
13
|
# Simply store value as key
|
11
14
|
def set(key, &fetcher)
|
12
15
|
init unless initted? # for first time set
|
@@ -69,7 +72,15 @@ class FatCache
|
|
69
72
|
|
70
73
|
def fetch!(key)
|
71
74
|
ensure_fetchable(key)
|
75
|
+
if logger
|
76
|
+
start_time = Time.now
|
77
|
+
logger.info "[fatcache] <#{key}> fetching ..."
|
78
|
+
end
|
72
79
|
fatcache[key] = fetchers[key].call(self)
|
80
|
+
if logger
|
81
|
+
took = Time.now - start_time
|
82
|
+
logger.info "[fatcache] <#{key}> done! #{(took / 60).floor} minutes, #{(took % 60).floor} seconds"
|
83
|
+
end
|
73
84
|
end
|
74
85
|
|
75
86
|
def fetch_index!(key, on)
|
@@ -82,7 +93,15 @@ class FatCache
|
|
82
93
|
|
83
94
|
raw_data = get(key)
|
84
95
|
|
96
|
+
if logger
|
97
|
+
start_time = Time.now
|
98
|
+
logger.info "[fatcache] <#{key}> indexing on #{on.inspect} ..."
|
99
|
+
end
|
85
100
|
indexed_fatcache[key][on] = index_fetchers[key][on].call(raw_data)
|
101
|
+
if logger
|
102
|
+
took = Time.now - start_time
|
103
|
+
logger.info "[fatcache] <#{key}> indexing on #{on.inspect} done! #{(took / 60).floor} minutes, #{(took % 60).floor} seconds"
|
104
|
+
end
|
86
105
|
end
|
87
106
|
|
88
107
|
def get_index(key, on)
|
data/spec/fat_cache_spec.rb
CHANGED
@@ -59,6 +59,19 @@ describe FatCache do
|
|
59
59
|
FatCache.fetch!(:dont_fool_me_twice)
|
60
60
|
FatCache.get(:dont_fool_me_twice)
|
61
61
|
end
|
62
|
+
|
63
|
+
describe 'when logger is set' do
|
64
|
+
before do
|
65
|
+
@logger = stub('fakelogger')
|
66
|
+
FatCache.logger = @logger
|
67
|
+
end
|
68
|
+
after { FatCache.logger = nil }
|
69
|
+
it 'prints out once before beginning and once after' do
|
70
|
+
FatCache.set(:you) { 'are my sunshine' }
|
71
|
+
@logger.should_receive(:info).with(/you/).twice
|
72
|
+
FatCache.fetch!(:you)
|
73
|
+
end
|
74
|
+
end
|
62
75
|
end
|
63
76
|
|
64
77
|
describe 'lookup(key, :by => [:method_names], :using => [:index_key])' do
|
@@ -152,6 +165,31 @@ describe FatCache do
|
|
152
165
|
|
153
166
|
end
|
154
167
|
|
168
|
+
describe 'fetch_index!(key, on)' do
|
169
|
+
it 'actually runs through and fetches index'
|
170
|
+
it 'defines an index based on the key if one does not exist'
|
171
|
+
it 'uses the existing index if one is defined'
|
172
|
+
describe 'when logger is set' do
|
173
|
+
before do
|
174
|
+
@logger = stub('fakelogger')
|
175
|
+
end
|
176
|
+
after { FatCache.logger = nil }
|
177
|
+
it 'prints out once before beginning and once after' do
|
178
|
+
FatCache.set(:you) { 'are my sunshine' }
|
179
|
+
FatCache.fetch!(:you)
|
180
|
+
|
181
|
+
FatCache.logger = @logger
|
182
|
+
@logger.should_receive(:info).twice { |str|
|
183
|
+
str.should match(/you/)
|
184
|
+
str.should match(/index/)
|
185
|
+
str.should match(/nil/)
|
186
|
+
}
|
187
|
+
|
188
|
+
FatCache.fetch_index!(:you, :nil?)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
155
193
|
describe 'invalidate!(key)' do
|
156
194
|
describe 'when no fetcher has been specified for key' do
|
157
195
|
it 'returns the last value the cache had for the key' do
|