2015-04-28 17:32:10 +00:00
|
|
|
import functools
|
|
|
|
|
|
|
|
from requests.adapters import HTTPAdapter
|
2014-03-27 21:06:03 +00:00
|
|
|
|
2014-04-23 06:24:08 +00:00
|
|
|
from .controller import CacheController
|
|
|
|
from .cache import DictCache
|
2015-04-28 17:32:10 +00:00
|
|
|
from .filewrapper import CallbackFileWrapper
|
2014-03-27 21:06:03 +00:00
|
|
|
|
2015-02-13 00:01:49 +00:00
|
|
|
|
2014-03-27 21:06:03 +00:00
|
|
|
class CacheControlAdapter(HTTPAdapter):
|
|
|
|
invalidating_methods = set(['PUT', 'DELETE'])
|
|
|
|
|
2015-04-28 17:32:10 +00:00
|
|
|
def __init__(self, cache=None,
|
|
|
|
cache_etags=True,
|
|
|
|
controller_class=None,
|
|
|
|
serializer=None,
|
|
|
|
heuristic=None,
|
|
|
|
*args, **kw):
|
2014-03-27 21:06:03 +00:00
|
|
|
super(CacheControlAdapter, self).__init__(*args, **kw)
|
|
|
|
self.cache = cache or DictCache()
|
2015-04-28 17:32:10 +00:00
|
|
|
self.heuristic = heuristic
|
2014-04-23 06:24:08 +00:00
|
|
|
|
|
|
|
controller_factory = controller_class or CacheController
|
|
|
|
self.controller = controller_factory(
|
|
|
|
self.cache,
|
|
|
|
cache_etags=cache_etags,
|
|
|
|
serializer=serializer,
|
|
|
|
)
|
2014-03-27 21:06:03 +00:00
|
|
|
|
|
|
|
def send(self, request, **kw):
|
2014-04-23 06:24:08 +00:00
|
|
|
"""
|
|
|
|
Send a request. Use the request information to see if it
|
|
|
|
exists in the cache and cache the response if we need to and can.
|
2014-03-27 21:06:03 +00:00
|
|
|
"""
|
|
|
|
if request.method == 'GET':
|
2014-04-23 06:24:08 +00:00
|
|
|
cached_response = self.controller.cached_request(request)
|
2014-03-27 21:06:03 +00:00
|
|
|
if cached_response:
|
2015-04-28 17:32:10 +00:00
|
|
|
return self.build_response(request, cached_response,
|
|
|
|
from_cache=True)
|
2014-03-27 21:06:03 +00:00
|
|
|
|
|
|
|
# check for etags and add headers if appropriate
|
2015-04-28 17:32:10 +00:00
|
|
|
request.headers.update(
|
|
|
|
self.controller.conditional_headers(request)
|
|
|
|
)
|
2014-03-27 21:06:03 +00:00
|
|
|
|
|
|
|
resp = super(CacheControlAdapter, self).send(request, **kw)
|
2014-04-23 06:24:08 +00:00
|
|
|
|
2014-03-27 21:06:03 +00:00
|
|
|
return resp
|
|
|
|
|
2014-04-23 06:24:08 +00:00
|
|
|
def build_response(self, request, response, from_cache=False):
|
|
|
|
"""
|
|
|
|
Build a response by making a request or using the cache.
|
2014-03-27 21:06:03 +00:00
|
|
|
|
|
|
|
This will end up calling send and returning a potentially
|
|
|
|
cached response
|
|
|
|
"""
|
2014-04-23 06:24:08 +00:00
|
|
|
if not from_cache and request.method == 'GET':
|
2015-04-28 17:32:10 +00:00
|
|
|
|
|
|
|
# apply any expiration heuristics
|
2014-03-27 21:06:03 +00:00
|
|
|
if response.status == 304:
|
|
|
|
# We must have sent an ETag request. This could mean
|
|
|
|
# that we've been expired already or that we simply
|
|
|
|
# have an etag. In either case, we want to try and
|
|
|
|
# update the cache if that is the case.
|
2014-04-23 06:24:08 +00:00
|
|
|
cached_response = self.controller.update_cached_response(
|
2014-03-27 21:06:03 +00:00
|
|
|
request, response
|
|
|
|
)
|
2014-04-23 06:24:08 +00:00
|
|
|
|
|
|
|
if cached_response is not response:
|
|
|
|
from_cache = True
|
|
|
|
|
2015-04-28 17:32:10 +00:00
|
|
|
# We are done with the server response, read a
|
|
|
|
# possible response body (compliant servers will
|
|
|
|
# not return one, but we cannot be 100% sure) and
|
|
|
|
# release the connection back to the pool.
|
|
|
|
response.read(decode_content=False)
|
|
|
|
response.release_conn()
|
|
|
|
|
2014-04-23 06:24:08 +00:00
|
|
|
response = cached_response
|
2015-04-28 17:32:10 +00:00
|
|
|
|
|
|
|
# We always cache the 301 responses
|
|
|
|
elif response.status == 301:
|
|
|
|
self.controller.cache_response(request, response)
|
2014-03-27 21:06:03 +00:00
|
|
|
else:
|
2015-04-28 17:32:10 +00:00
|
|
|
# Check for any heuristics that might update headers
|
|
|
|
# before trying to cache.
|
|
|
|
if self.heuristic:
|
|
|
|
response = self.heuristic.apply(response)
|
|
|
|
|
|
|
|
# Wrap the response file with a wrapper that will cache the
|
|
|
|
# response when the stream has been consumed.
|
|
|
|
response._fp = CallbackFileWrapper(
|
|
|
|
response._fp,
|
|
|
|
functools.partial(
|
|
|
|
self.controller.cache_response,
|
|
|
|
request,
|
|
|
|
response,
|
|
|
|
)
|
|
|
|
)
|
2014-04-23 06:24:08 +00:00
|
|
|
|
|
|
|
resp = super(CacheControlAdapter, self).build_response(
|
|
|
|
request, response
|
|
|
|
)
|
|
|
|
|
|
|
|
# See if we should invalidate the cache.
|
|
|
|
if request.method in self.invalidating_methods and resp.ok:
|
|
|
|
cache_url = self.controller.cache_url(request.url)
|
|
|
|
self.cache.delete(cache_url)
|
2014-03-27 21:06:03 +00:00
|
|
|
|
|
|
|
# Give the request a from_cache attr to let people use it
|
2014-04-23 06:24:08 +00:00
|
|
|
resp.from_cache = from_cache
|
2014-03-27 21:06:03 +00:00
|
|
|
|
|
|
|
return resp
|
2015-04-28 17:32:10 +00:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.cache.close()
|
|
|
|
super(CacheControlAdapter, self).close()
|