mirror of
https://github.com/SickGear/SickGear.git
synced 2024-12-02 17:33: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)
|
super(CacheControlAdapter, self).__init__(*args, **kw)
|
||||||
self.sess = sess or CacheControlSession()
|
self.sess = sess or CacheControlSession()
|
||||||
self.cache = cache or DictCache()
|
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):
|
def send(self, request, **kw):
|
||||||
"""Send a request. Use the request information to see if it
|
"""Send a request. Use the request information to see if it
|
||||||
|
|
|
@ -34,4 +34,3 @@ class DictCache(BaseCache):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
if key in self.data:
|
if key in self.data:
|
||||||
self.data.pop(key)
|
self.data.pop(key)
|
||||||
0
|
|
|
@ -26,9 +26,9 @@ class CacheController(object):
|
||||||
"""An interface to see if request should cached or not.
|
"""An interface to see if request should cached or not.
|
||||||
"""
|
"""
|
||||||
def __init__(self, sess=None, cache=None, cache_etags=True):
|
def __init__(self, sess=None, cache=None, cache_etags=True):
|
||||||
|
self.sess = sess or CacheControlSession()
|
||||||
self.cache = cache or DictCache()
|
self.cache = cache or DictCache()
|
||||||
self.cache_etags = cache_etags
|
self.cache_etags = cache_etags
|
||||||
self.sess = sess or CacheControlSession()
|
|
||||||
|
|
||||||
def _urlnorm(self, uri):
|
def _urlnorm(self, uri):
|
||||||
"""Normalize the URL to create a safe key for the cache"""
|
"""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]:
|
if resp.status_code not in [200, 203]:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
cache_url = self.cache_url(request.url)
|
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)
|
||||||
return
|
cache_urls = getattr(self.sess, 'cache_urls', None)
|
||||||
|
cache_max_age = getattr(self.sess, 'cache_max_age', None)
|
||||||
|
|
||||||
if self.sess.cache_auto and ('cache-control' not in resp.headers or 'Cache-Control' not in resp.headers):
|
if cache_urls:
|
||||||
cache_max_age = int(self.sess.cache_max_age or 900)
|
if not any(s in cache_url for s in cache_urls):
|
||||||
|
return
|
||||||
|
|
||||||
|
# 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)}
|
headers = {'Cache-Control': 'public,max-age=%d' % int(cache_max_age)}
|
||||||
resp.headers.update(headers)
|
resp.headers.update(headers)
|
||||||
|
|
||||||
|
@ -220,15 +225,14 @@ class CacheController(object):
|
||||||
# cache when there is a max-age > 0
|
# cache when there is a max-age > 0
|
||||||
if cc and cc.get('max-age'):
|
if cc and cc.get('max-age'):
|
||||||
if int(cc['max-age']) > 0:
|
if int(cc['max-age']) > 0:
|
||||||
if self.sess.cache_max_age:
|
if cache_max_age:
|
||||||
cc['max-age'] = int(self.sess.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()])
|
resp.headers['cache-control'] = ''.join(['%s=%s' % (key, value) for (key, value) in cc.items()])
|
||||||
self.cache.set(cache_url, resp)
|
self.cache.set(cache_url, resp)
|
||||||
|
|
||||||
# If the request can expire, it means we should cache it
|
# If the request can expire, it means we should cache it
|
||||||
# in the meantime.
|
# in the meantime.
|
||||||
elif 'expires' in resp.headers:
|
elif getattr(resp.headers, 'expires', None) is None:
|
||||||
if resp.headers['expires']:
|
|
||||||
self.cache.set(cache_url, resp)
|
self.cache.set(cache_url, resp)
|
||||||
|
|
||||||
def update_cached_response(self, request, response):
|
def update_cached_response(self, request, response):
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import datetime
|
|
||||||
from requests.sessions import Session
|
from requests.sessions import Session
|
||||||
|
|
||||||
class CacheControlSession(Session):
|
class CacheControlSession(Session):
|
||||||
|
@ -12,11 +11,11 @@ class CacheControlSession(Session):
|
||||||
self.cache_auto = kw.pop('cache_auto')
|
self.cache_auto = kw.pop('cache_auto')
|
||||||
|
|
||||||
# urls allowed to cache
|
# urls allowed to cache
|
||||||
self.cache_urls = []
|
self.cache_urls = None
|
||||||
if kw.has_key('cache_urls'):
|
if kw.has_key('cache_urls'):
|
||||||
self.cache_urls = [str(args[1])] + kw.pop('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
|
self.cache_max_age = None
|
||||||
if kw.has_key('cache_max_age'):
|
if kw.has_key('cache_max_age'):
|
||||||
self.cache_max_age = int(kw.pop('cache_max_age'))
|
self.cache_max_age = int(kw.pop('cache_max_age'))
|
||||||
|
|
Loading…
Reference in a new issue