mirror of
https://github.com/SickGear/SickGear.git
synced 2024-12-01 00:43:37 +00:00
Fixed bugs in cache control
This commit is contained in:
parent
c2f7f766f4
commit
2acafcb603
4 changed files with 17 additions and 15 deletions
|
@ -11,7 +11,7 @@ class CacheControlAdapter(HTTPAdapter):
|
|||
super(CacheControlAdapter, self).__init__(*args, **kw)
|
||||
self.sess = sess or CacheControlSession()
|
||||
self.cache = cache or DictCache()
|
||||
self.controller = CacheController(sess=sess, cache=cache, cache_etags=cache_etags)
|
||||
self.controller = CacheController(self.sess, self.cache, cache_etags=cache_etags)
|
||||
|
||||
def send(self, request, **kw):
|
||||
"""Send a request. Use the request information to see if it
|
||||
|
|
|
@ -34,4 +34,3 @@ class DictCache(BaseCache):
|
|||
with self.lock:
|
||||
if key in self.data:
|
||||
self.data.pop(key)
|
||||
0
|
|
@ -26,9 +26,9 @@ class CacheController(object):
|
|||
"""An interface to see if request should cached or not.
|
||||
"""
|
||||
def __init__(self, sess=None, cache=None, cache_etags=True):
|
||||
self.sess = sess or CacheControlSession()
|
||||
self.cache = cache or DictCache()
|
||||
self.cache_etags = cache_etags
|
||||
self.sess = sess or CacheControlSession()
|
||||
|
||||
def _urlnorm(self, uri):
|
||||
"""Normalize the URL to create a safe key for the cache"""
|
||||
|
@ -185,13 +185,18 @@ class CacheController(object):
|
|||
if resp.status_code not in [200, 203]:
|
||||
return
|
||||
|
||||
|
||||
cache_url = self.cache_url(request.url)
|
||||
if self.sess.cache_urls and not any(s in cache_url for s in self.sess.cache_urls):
|
||||
cache_auto = getattr(self.sess, 'cache_auto', None)
|
||||
cache_urls = getattr(self.sess, 'cache_urls', None)
|
||||
cache_max_age = getattr(self.sess, 'cache_max_age', None)
|
||||
|
||||
if cache_urls:
|
||||
if not any(s in cache_url for s in cache_urls):
|
||||
return
|
||||
|
||||
if self.sess.cache_auto and ('cache-control' not in resp.headers or 'Cache-Control' not in resp.headers):
|
||||
cache_max_age = int(self.sess.cache_max_age or 900)
|
||||
# If we want to cache sites not setup with cache headers then add the proper headers and keep the response
|
||||
if cache_auto and getattr(resp.headers, 'cache-control', None) is None:
|
||||
cache_max_age = int(cache_max_age or 3600)
|
||||
headers = {'Cache-Control': 'public,max-age=%d' % int(cache_max_age)}
|
||||
resp.headers.update(headers)
|
||||
|
||||
|
@ -220,15 +225,14 @@ class CacheController(object):
|
|||
# cache when there is a max-age > 0
|
||||
if cc and cc.get('max-age'):
|
||||
if int(cc['max-age']) > 0:
|
||||
if self.sess.cache_max_age:
|
||||
cc['max-age'] = int(self.sess.cache_max_age)
|
||||
if cache_max_age:
|
||||
cc['max-age'] = int(cache_max_age)
|
||||
resp.headers['cache-control'] = ''.join(['%s=%s' % (key, value) for (key, value) in cc.items()])
|
||||
self.cache.set(cache_url, resp)
|
||||
|
||||
# If the request can expire, it means we should cache it
|
||||
# in the meantime.
|
||||
elif 'expires' in resp.headers:
|
||||
if resp.headers['expires']:
|
||||
elif getattr(resp.headers, 'expires', None) is None:
|
||||
self.cache.set(cache_url, resp)
|
||||
|
||||
def update_cached_response(self, request, response):
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import datetime
|
||||
from requests.sessions import Session
|
||||
|
||||
class CacheControlSession(Session):
|
||||
|
@ -12,11 +11,11 @@ class CacheControlSession(Session):
|
|||
self.cache_auto = kw.pop('cache_auto')
|
||||
|
||||
# urls allowed to cache
|
||||
self.cache_urls = []
|
||||
self.cache_urls = None
|
||||
if kw.has_key('cache_urls'):
|
||||
self.cache_urls = [str(args[1])] + kw.pop('cache_urls')
|
||||
|
||||
# timeout for cacheed responses
|
||||
# timeout for cached responses
|
||||
self.cache_max_age = None
|
||||
if kw.has_key('cache_max_age'):
|
||||
self.cache_max_age = int(kw.pop('cache_max_age'))
|
||||
|
|
Loading…
Reference in a new issue