2014-03-27 21:06:03 +00:00
|
|
|
from requests.adapters import HTTPAdapter
|
|
|
|
|
2014-04-23 06:24:08 +00:00
|
|
|
from .controller import CacheController
|
|
|
|
from .cache import DictCache
|
2014-03-27 21:06:03 +00:00
|
|
|
|
|
|
|
class CacheControlAdapter(HTTPAdapter):
|
|
|
|
invalidating_methods = set(['PUT', 'DELETE'])
|
|
|
|
|
2014-04-23 06:24:08 +00:00
|
|
|
def __init__(self, cache=None, cache_etags=True, controller_class=None,
|
|
|
|
serializer=None, *args, **kw):
|
2014-03-27 21:06:03 +00:00
|
|
|
super(CacheControlAdapter, self).__init__(*args, **kw)
|
|
|
|
self.cache = cache or DictCache()
|
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:
|
2014-04-23 06:24:08 +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
|
2014-04-23 06:24:08 +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':
|
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
|
|
|
|
|
|
|
|
response = cached_response
|
2014-03-27 21:06:03 +00:00
|
|
|
else:
|
|
|
|
# try to cache the response
|
2014-04-23 06:24:08 +00:00
|
|
|
self.controller.cache_response(request, response)
|
|
|
|
|
|
|
|
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
|