Skip to content

Commit 783f1d8

Browse files
authored
Merge pull request #144 from elnuno/master
Make CacheControl, CacheControlAdapter and CacheControler easier to customize
2 parents f66639f + 58cee33 commit 783f1d8

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

cachecontrol/adapter.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ def __init__(self, cache=None,
1616
controller_class=None,
1717
serializer=None,
1818
heuristic=None,
19+
cacheable_methods=None,
1920
*args, **kw):
2021
super(CacheControlAdapter, self).__init__(*args, **kw)
2122
self.cache = cache or DictCache()
2223
self.heuristic = heuristic
24+
self.cacheable_methods = cacheable_methods or ('GET',)
2325

2426
controller_factory = controller_class or CacheController
2527
self.controller = controller_factory(
@@ -28,12 +30,13 @@ def __init__(self, cache=None,
2830
serializer=serializer,
2931
)
3032

31-
def send(self, request, **kw):
33+
def send(self, request, cacheable_methods=None, **kw):
3234
"""
3335
Send a request. Use the request information to see if it
3436
exists in the cache and cache the response if we need to and can.
3537
"""
36-
if request.method == 'GET':
38+
cacheable = cacheable_methods or self.cacheable_methods
39+
if request.method in cacheable:
3740
cached_response = self.controller.cached_request(request)
3841
if cached_response:
3942
return self.build_response(request, cached_response,
@@ -48,14 +51,16 @@ def send(self, request, **kw):
4851

4952
return resp
5053

51-
def build_response(self, request, response, from_cache=False):
54+
def build_response(self, request, response, from_cache=False,
55+
cacheable_methods=None):
5256
"""
5357
Build a response by making a request or using the cache.
5458
5559
This will end up calling send and returning a potentially
5660
cached response
5761
"""
58-
if not from_cache and request.method == 'GET':
62+
cacheable = cacheable_methods or self.cacheable_methods
63+
if not from_cache and request.method in cacheable:
5964
# Check for any heuristics that might update headers
6065
# before trying to cache.
6166
if self.heuristic:

cachecontrol/controller.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ def parse_uri(uri):
3030
class CacheController(object):
3131
"""An interface to see if request should cached or not.
3232
"""
33-
def __init__(self, cache=None, cache_etags=True, serializer=None):
33+
def __init__(self, cache=None, cache_etags=True, serializer=None,
34+
status_codes=None):
3435
self.cache = cache or DictCache()
3536
self.cache_etags = cache_etags
3637
self.serializer = serializer or Serializer()
38+
self.cacheable_status_codes = status_codes or (200, 203, 300, 301)
3739

3840
@classmethod
3941
def _urlnorm(cls, uri):
@@ -220,15 +222,16 @@ def conditional_headers(self, request):
220222

221223
return new_headers
222224

223-
def cache_response(self, request, response, body=None):
225+
def cache_response(self, request, response, body=None,
226+
status_codes=None):
224227
"""
225228
Algorithm for caching requests.
226229
227230
This assumes a requests Response object.
228231
"""
229232
# From httplib2: Don't cache 206's since we aren't going to
230233
# handle byte range requests
231-
cacheable_status_codes = [200, 203, 300, 301]
234+
cacheable_status_codes = status_codes or self.cacheable_status_codes
232235
if response.status not in cacheable_status_codes:
233236
logger.debug(
234237
'Status code %s not in %s',

cachecontrol/wrapper.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ def CacheControl(sess,
66
cache=None,
77
cache_etags=True,
88
serializer=None,
9-
heuristic=None):
9+
heuristic=None,
10+
controller_class=None,
11+
adapter_class=None,
12+
cacheable_methods=None):
1013

1114
cache = cache or DictCache()
12-
adapter = CacheControlAdapter(
15+
adapter_class = adapter_class or CacheControlAdapter
16+
adapter = adapter_class(
1317
cache,
1418
cache_etags=cache_etags,
1519
serializer=serializer,
1620
heuristic=heuristic,
21+
controller_class=controller_class,
22+
cacheable_methods=cacheable_methods
1723
)
1824
sess.mount('http://', adapter)
1925
sess.mount('https://', adapter)

0 commit comments

Comments
 (0)