From: kaliko Date: Mon, 14 May 2018 14:31:00 +0000 (+0200) Subject: http client hardcoded caching (workaround for #7) X-Git-Tag: 0.15.0~9 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=cfe6dafed6e43714316c39c401475ed840e66a02;p=mpd-sima.git http client hardcoded caching (workaround for #7) --- diff --git a/doc/Changelog b/doc/Changelog index d5106ed..dcb72d9 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -5,6 +5,7 @@ MPD_sima v0.15.0 * Add option to prevent single & repeat mode to disable queuing (Closes #19) * Honor MPC password/host format on command line option 'host' * Fixed fuzzy search for short artist names + * Client side hardcoded one month caching (ugly workaround for #7) -- kaliko jack UNRELEASED diff --git a/sima/lib/http.py b/sima/lib/http.py index f9a08be..d253568 100644 --- a/sima/lib/http.py +++ b/sima/lib/http.py @@ -48,6 +48,8 @@ def parse_uri(uri): class CacheController(object): """An interface to see if request should cached or not. """ + CACHE_ANYWAY = False + def __init__(self, cache=None, cache_etags=True): self.cache = cache or DictCache() self.cache_etags = cache_etags @@ -102,7 +104,6 @@ class CacheController(object): no_cache = True if 'no-cache' in cc else False if 'max-age' in cc and cc['max-age'] == 0: no_cache = True - # see if it is in the cache anyways in_cache = self.cache.get(cache_url) if no_cache or not in_cache: @@ -229,6 +230,12 @@ class CacheController(object): elif 'expires' in resp.headers: if resp.headers['expires']: self.cache.set(cache_url, resp) + # Force one month max age if no Cache-Control header is found + # Overriding header disappearance on LastFM web service... + # https://getsatisfaction.com/lastfm/topics/-web-api-http-cache-control-header + elif CacheController.CACHE_ANYWAY: + resp.headers['Cache-Control'] = 'max-age=2419200' + self.cache.set(cache_url, resp) def update_cached_response(self, request, response): """On a 304 we will get a new set of headers that we want to diff --git a/sima/plugins/internal/lastfm.py b/sima/plugins/internal/lastfm.py index 125f009..2805197 100644 --- a/sima/plugins/internal/lastfm.py +++ b/sima/plugins/internal/lastfm.py @@ -30,6 +30,7 @@ from os.path import join from ...lib.simafm import SimaFM from ...lib.webserv import WebService from ...lib.cache import FileCache +from ...lib.http import CacheController class Lastfm(WebService): @@ -42,6 +43,7 @@ class Lastfm(WebService): vardir = daemon.config['sima']['var_dir'] persitent_cache = daemon.config.getboolean('lastfm', 'cache') if persitent_cache: + CacheController.CACHE_ANYWAY = True self.log.debug('Persistant cache enabled in {}'.format(join(vardir, 'http', 'LastFM'))) SimaFM.cache = FileCache(join(vardir, 'http', 'LastFM')) self.ws = SimaFM()