lib/rack/cache/context.rb in rack-cache-0.5 vs lib/rack/cache/context.rb in rack-cache-0.5.2
- old
+ new
@@ -131,13 +131,17 @@
end
# Invalidate POST, PUT, DELETE and all methods not understood by this cache
# See RFC2616 13.10
def invalidate
- record :invalidate
metastore.invalidate(@request, entitystore)
+ rescue Exception => e
+ log_error(e)
pass
+ else
+ record :invalidate
+ pass
end
# Try to serve the response from cache. When a matching cache entry is
# found and is fresh, use it as the response without forwarding any
# request to the backend. When a matching cache entry is found but is
@@ -145,22 +149,30 @@
# GET. When no matching cache entry is found, trigger #miss processing.
def lookup
if @request.no_cache? && allow_reload?
record :reload
fetch
- elsif entry = metastore.lookup(@request, entitystore)
- if fresh_enough?(entry)
- record :fresh
- entry.headers['Age'] = entry.age.to_s
- entry
+ else
+ begin
+ entry = metastore.lookup(@request, entitystore)
+ rescue Exception => e
+ log_error(e)
+ return pass
+ end
+ if entry
+ if fresh_enough?(entry)
+ record :fresh
+ entry.headers['Age'] = entry.age.to_s
+ entry
+ else
+ record :stale
+ validate(entry)
+ end
else
- record :stale
- validate(entry)
+ record :miss
+ fetch
end
- else
- record :miss
- fetch
end
end
# Validate that the cache entry is fresh. The original request is used
# as a template for a conditional GET request with the backend.
@@ -223,11 +235,19 @@
response
end
# Write the response to the cache.
def store(response)
- record :store
metastore.store(@request, response, entitystore)
response.headers['Age'] = response.age.to_s
+ rescue Exception => e
+ log_error(e)
+ nil
+ else
+ record :store
+ end
+
+ def log_error(exception)
+ @env['rack.errors'].write("cache error: #{exception.message}\n#{exception.backtrace.join("\n")}\n")
end
end
end