Skip to content

Commit 31b5448

Browse files
mje113mezis
authored andcommitted
Enabling native ttl option for metastore
1 parent b6d8715 commit 31b5448

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

lib/rack/cache/metastore.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ def store(request, response, entity_store)
8080
headers.delete 'Age'
8181

8282
entries.unshift [stored_env, headers]
83-
write key, entries
83+
if request.env['rack-cache.use_native_ttl']
84+
write key, entries, response.ttl
85+
else
86+
write key, entries
87+
end
8488
key
8589
end
8690

@@ -154,7 +158,7 @@ def read(key)
154158
# Store an Array of request/response pairs for the given key. Concrete
155159
# implementations should not attempt to filter or concatenate the
156160
# list in any way.
157-
def write(key, negotiations)
161+
def write(key, negotiations, ttl = nil)
158162
raise NotImplemented
159163
end
160164

@@ -187,7 +191,7 @@ def read(key)
187191
end
188192
end
189193

190-
def write(key, entries)
194+
def write(key, entries, ttl = nil)
191195
@hash[key] = Marshal.dump(entries)
192196
end
193197

@@ -225,7 +229,7 @@ def read(key)
225229
[]
226230
end
227231

228-
def write(key, entries)
232+
def write(key, entries, ttl = nil)
229233
tries = 0
230234
begin
231235
path = key_path(key)
@@ -324,9 +328,9 @@ def read(key)
324328
cache.get(key) || []
325329
end
326330

327-
def write(key, entries)
331+
def write(key, entries, ttl = 0)
328332
key = hexdigest(key)
329-
cache.set(key, entries)
333+
cache.set(key, entries, ttl)
330334
end
331335

332336
def purge(key)
@@ -358,9 +362,9 @@ def read(key)
358362
[]
359363
end
360364

361-
def write(key, entries)
365+
def write(key, entries, ttl = 0)
362366
key = hexdigest(key)
363-
cache.set(key, entries)
367+
cache.set(key, entries, ttl)
364368
end
365369

366370
def purge(key)

test/metastore_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ def purge(*args); nil end
251251

252252
@store.read(key).length.should.equal 2
253253
end
254+
255+
it 'takes a ttl parameter for #write' do
256+
@store.write('/test', [[{},{}],[{},{}]], 0)
257+
tuples = @store.read('/test')
258+
tuples.should.equal [ [{},{}], [{},{}] ]
259+
end
254260
end
255261

256262

0 commit comments

Comments
 (0)