Skip to content

Commit 58cee33

Browse files
committed
Allow passing cacheable_methods and cacheable_status_codes to __init__.
1 parent 8c3c547 commit 58cee33

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

cachecontrol/adapter.py

Lines changed: 6 additions & 2 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,11 +30,12 @@ def __init__(self, cache=None,
2830
serializer=serializer,
2931
)
3032

31-
def send(self, request, cacheable=('GET',), **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
"""
38+
cacheable = cacheable_methods or self.cacheable_methods
3639
if request.method in cacheable:
3740
cached_response = self.controller.cached_request(request)
3841
if cached_response:
@@ -49,13 +52,14 @@ def send(self, request, cacheable=('GET',), **kw):
4952
return resp
5053

5154
def build_response(self, request, response, from_cache=False,
52-
cacheable=('GET',)):
55+
cacheable_methods=None):
5356
"""
5457
Build a response by making a request or using the cache.
5558
5659
This will end up calling send and returning a potentially
5760
cached response
5861
"""
62+
cacheable = cacheable_methods or self.cacheable_methods
5963
if not from_cache and request.method in cacheable:
6064
# Check for any heuristics that might update headers
6165
# before trying to cache.

cachecontrol/controller.py

Lines changed: 5 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):
@@ -221,15 +223,15 @@ def conditional_headers(self, request):
221223
return new_headers
222224

223225
def cache_response(self, request, response, body=None,
224-
cacheable_status_codes=None):
226+
status_codes=None):
225227
"""
226228
Algorithm for caching requests.
227229
228230
This assumes a requests Response object.
229231
"""
230232
# From httplib2: Don't cache 206's since we aren't going to
231233
# handle byte range requests
232-
cacheable_status_codes = cacheable_status_codes or (200, 203, 300, 301)
234+
cacheable_status_codes = status_codes or self.cacheable_status_codes
233235
if response.status not in cacheable_status_codes:
234236
logger.debug(
235237
'Status code %s not in %s',

cachecontrol/wrapper.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ def CacheControl(sess,
88
serializer=None,
99
heuristic=None,
1010
controller_class=None,
11-
adapter_class=None):
11+
adapter_class=None,
12+
cacheable_methods=None):
1213

1314
cache = cache or DictCache()
1415
adapter_class = adapter_class or CacheControlAdapter
@@ -17,7 +18,8 @@ def CacheControl(sess,
1718
cache_etags=cache_etags,
1819
serializer=serializer,
1920
heuristic=heuristic,
20-
controller_class=controller_class
21+
controller_class=controller_class,
22+
cacheable_methods=cacheable_methods
2123
)
2224
sess.mount('http://', adapter)
2325
sess.mount('https://', adapter)

0 commit comments

Comments
 (0)