Clean up old AND invalid entries in preemptive cache config

This commit is contained in:
Gina Häußge 2016-07-07 10:22:37 +02:00
parent d96a91ecc3
commit a7fe2d5148
2 changed files with 19 additions and 8 deletions

View file

@ -779,10 +779,25 @@ class Server(object):
preemptive_cache_timeout = settings().getInt(["server", "preemptiveCache", "until"])
cutoff_timestamp = time.time() - preemptive_cache_timeout * 24 * 60 * 60
def filter_old_entries(entry):
def filter_current_entries(entry):
"""Returns True for entries younger than the cutoff date"""
return "_timestamp" in entry and entry["_timestamp"] > cutoff_timestamp
cache_data = preemptive_cache.clean_all_data(lambda root, entries: filter(filter_old_entries, entries))
def filter_http_entries(entry):
"""Returns True for entries targeting http or https."""
return "base_url" in entry \
and entry["base_url"] \
and (entry["base_url"].startswith("http://")
or entry["base_url"].startswith("https://"))
def filter_entries(entry):
"""Combined filter."""
filters = (filter_current_entries,
filter_http_entries)
return all([f(entry) for f in filters])
# filter out all old and non-http entries
cache_data = preemptive_cache.clean_all_data(lambda root, entries: filter(filter_entries, entries))
if not cache_data:
return
@ -795,11 +810,6 @@ class Server(object):
kwargs = dict((k, v) for k, v in kwargs.items() if not k.startswith("_") and not k == "plugin")
kwargs.update(additional_request_data)
base_url = kwargs.get("base_url", "")
if not (base_url.startswith("http://") or base_url.startswith("https://")):
self._logger.info("Skipping preemptive cache entry with base url {}, neither http:// nor https:// and possibly broken".format(base_url))
continue
try:
if plugin:
self._logger.info("Preemptively caching {} (plugin {}) for {!r}".format(route, plugin, kwargs))
@ -811,6 +821,7 @@ class Server(object):
except:
self._logger.exception("Error while trying to preemptively cache {} for {!r}".format(route, kwargs))
# asynchronous caching
import threading
cache_thread = threading.Thread(target=execute_caching, name="Preemptive Cache Worker")
cache_thread.daemon = True

View file

@ -420,7 +420,7 @@ class PreemptiveCache(object):
self._logger.debug("Removed root {} from preemptive cache".format(root))
elif len(entries) < old_count:
all_data[root] = entries
self._logger.debug("Removed {} from preemptive cache for root {}".format(old_count - len(entries), root))
self._logger.debug("Removed {} entries from preemptive cache for root {}".format(old_count - len(entries), root))
self.set_all_data(all_data)
return all_data