fat_cache 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.8
1
+ 0.0.9
@@ -26,7 +26,7 @@ class FatCache
26
26
  if fetchable?(key)
27
27
  fetch!(key)
28
28
  else
29
- raise "no data for #{key}"
29
+ raise CacheMiss, "no data for #{key}"
30
30
  end
31
31
  end
32
32
  fatcache[key]
@@ -52,14 +52,14 @@ class FatCache
52
52
 
53
53
  def one!(key, spec={})
54
54
  result = one(key, spec)
55
- raise CacheMiss, "count not find find #{key} with #{spec.inspect}" unless result
55
+ raise CacheMiss, "could not find #{key} with #{spec.inspect}" unless result
56
56
  result
57
57
  end
58
58
 
59
59
  def index(key, on, &block)
60
60
  on = [*on] # ensure we're dealing with an array, we're such a friendly API!
61
61
 
62
- ensure_fetchable(key)
62
+ ensure_data_available_for(key)
63
63
 
64
64
  index_fetchers[key] = {} unless index_fetchers.has_key?(key)
65
65
 
@@ -120,6 +120,20 @@ class FatCache
120
120
  end
121
121
  end
122
122
 
123
+ def dump
124
+ Marshal.dump({
125
+ :fatcache => self.fatcache,
126
+ :indexed_fatcache => self.indexed_fatcache
127
+ })
128
+ end
129
+
130
+ def load!(datastring)
131
+ init unless initted?
132
+ data = Marshal.load(datastring)
133
+ self.fatcache = data[:fatcache]
134
+ self.indexed_fatcache = data[:indexed_fatcache]
135
+ end
136
+
123
137
  def get_index(key, on)
124
138
  on = [*on]
125
139
 
@@ -128,6 +142,10 @@ class FatCache
128
142
  indexed_fatcache[key][on]
129
143
  end
130
144
 
145
+ def ensure_data_available_for(key)
146
+ raise "no data available for #{key}" unless cached?(key) || fetchable?(key)
147
+ end
148
+
131
149
  def ensure_cached(key)
132
150
  raise "no data in cache for #{key}" unless cached?(key)
133
151
  end
@@ -257,4 +257,48 @@ describe FatCache do
257
257
  end
258
258
  end
259
259
  end
260
+
261
+
262
+ describe 'dump and load' do
263
+ it 'marshals out the data in the main cache' do
264
+ FatCache.set(:fetchy_poo) { "Oh what a fetching young chap" }
265
+ FatCache.get(:fetchy_poo) # get it fetched
266
+ data = FatCache.dump
267
+ FatCache.reset!
268
+ FatCache.load!(data)
269
+ FatCache.get(:fetchy_poo).should == "Oh what a fetching young chap"
270
+ end
271
+
272
+ it 'marshals out the indexed data in the main cache' do
273
+ FatCache.set(:numbers) { [0,1,2,3,4] }
274
+ FatCache.index(:numbers, :odd?)
275
+ FatCache.fetch_index!(:numbers, :odd?)
276
+ data = FatCache.dump
277
+ FatCache.reset!
278
+ FatCache.load!(data)
279
+ FatCache.lookup(:numbers, :by => :odd?, :using => true).should == [1,3]
280
+ end
281
+
282
+ describe 'after a dump and load' do
283
+ before do
284
+ FatCache.set(:numbers) { [0,1,2,3,4] }
285
+ FatCache.index(:numbers, :odd?)
286
+ FatCache.fetch_index!(:numbers, :odd?)
287
+ data = FatCache.dump
288
+ FatCache.reset!
289
+ FatCache.load!(data)
290
+ end
291
+ it 'allows new indexes to be defined' do
292
+ FatCache.index(:numbers, :even?)
293
+ FatCache.lookup(:numbers, :by => :even?, :using => true).should == [0,2,4]
294
+ end
295
+
296
+ it 'does not know about fetchers, so invalidate is permanant' do
297
+ FatCache.invalidate!(:numbers)
298
+ lambda {
299
+ FatCache.get(:numbers)
300
+ }.should raise_error FatCache::CacheMiss
301
+ end
302
+ end
303
+ end
260
304
  end
@@ -1 +1,2 @@
1
1
  --color
2
+ --backtrace
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fat_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - phinze
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-05 00:00:00 -06:00
12
+ date: 2010-03-08 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency