lib/async/http/cache/general.rb in async-http-cache-0.1.0 vs lib/async/http/cache/general.rb in async-http-cache-0.1.1
- old
+ new
@@ -31,23 +31,32 @@
module Cache
class General < ::Protocol::HTTP::Middleware
CACHE_CONTROL = 'cache-control'
CONTENT_TYPE = 'content-type'
AUTHORIZATION = 'authorization'
+ COOKIE = 'cookie'
def initialize(app, store: Store.default)
super(app)
@count = 0
@store = store
- @maximum_length = 128 * 1024
end
attr :count
+ attr :store
+ def close
+ @store.close
+ ensure
+ super
+ end
+
def key(request)
+ @store.normalize(request)
+
[request.authority, request.method, request.path]
end
def cacheable?(request)
# We don't support caching requests which have a body:
@@ -58,39 +67,34 @@
# We can't cache upgraded requests:
if request.protocol
return false
end
+ # We only support caching GET and HEAD requests:
+ unless request.method == 'GET' || request.method == 'HEAD'
+ return false
+ end
+
if request.headers[AUTHORIZATION]
return false
end
- # We only support caching GET and HEAD requests:
- if request.method == 'GET' || request.method == 'HEAD'
- return true
+ if request.headers[COOKIE]
+ return false
end
- # Otherwise, we can't cache it:
- return false
+ # Otherwise, we can cache it:
+ return true
end
def wrap(key, request, response)
if response.status != 200
return response
end
- if body = response.body
- if length = body.length
- # Don't cache responses bigger than 128Kb:
- return response if length > @maximum_length
- else
- # Don't cache responses without length:
- return response
- end
- end
-
return Body.wrap(response) do |message, body|
+ Async.logger.debug(self) {"Updating cache for #{key}..."}
@store.insert(key, request, Response.new(message, body))
end
end
def call(request)
@@ -108,10 +112,9 @@
end
end
unless cache_control&.no_store?
if cacheable?(request)
- Async.logger.debug(self) {"Updating cache for #{key}..."}
return wrap(key, request, super)
end
end
return super