From c5c5383e0e7a8ce2605538779de328a9c061423e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 8 Dec 2015 14:23:40 +0100 Subject: [PATCH] Fixed clean up of preemptive cache entries --- src/octoprint/server/__init__.py | 20 +++++++++++++------- src/octoprint/server/util/flask.py | 16 +++++++++------- src/octoprint/server/views.py | 2 +- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 7c8577a5..22523d65 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -691,24 +691,30 @@ class Server(): # we clean up entries from our preemptive cache settings that haven't been # accessed longer than server.preemptiveCache.until days preemptive_cache_timeout = settings().getInt(["server", "preemptiveCache", "until"]) - cutoff_timestamp = time.time() + preemptive_cache_timeout * 24 * 60 * 60 + cutoff_timestamp = time.time() - preemptive_cache_timeout * 24 * 60 * 60 - cache_data = preemptive_cache.clean_all_data(lambda root, entries: filter(lambda entry: "_timestamp" in entry and entry["_timestamp"] <= cutoff_timestamp, entries)) + def filter_old_entries(entry): + return "_timestamp" in entry and entry["_timestamp"] > cutoff_timestamp + + cache_data = preemptive_cache.clean_all_data(lambda root, entries: filter(filter_old_entries, entries)) if not cache_data: return def execute_caching(): for route in sorted(cache_data.keys(), key=lambda x: (x.count("/"), x)): - entries = cache_data[route] + entries = reversed(sorted(cache_data[route], key=lambda x: x.get("_count", 0))) for kwargs in entries: + plugin = kwargs.get("plugin", None) additional_request_data = kwargs.get("_additional_request_data", dict()) - kwargs = dict((k, v) for k, v in kwargs.items() if not k.startswith("_")) + kwargs = dict((k, v) for k, v in kwargs.items() if not k.startswith("_") and not k == "plugin") kwargs.update(additional_request_data) try: - - self._logger.info("Preemptively caching {} for {!r}".format(route, kwargs)) + if plugin: + self._logger.info("Preemptively caching {} (plugin {}) for {!r}".format(route, plugin, kwargs)) + else: + self._logger.info("Preemptively caching {} for {!r}".format(route, kwargs)) builder = EnvironBuilder(**kwargs) - with preemptive_cache.disable_timestamp_update(): + with preemptive_cache.disable_access_logging(): app(builder.get_environ(), lambda *a, **kw: None) except: self._logger.exception("Error while trying to preemptively cache {} for {!r}".format(route, kwargs)) diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index d621671a..d1c64bb2 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -386,7 +386,7 @@ class PreemptiveCache(object): self._lock = threading.RLock() self._logger = logging.getLogger(__name__ + "." + self.__class__.__name__) - self._update_timestamp = True + self._log_access = True def record(self, data, unless=None): if callable(unless) and unless(): @@ -401,11 +401,11 @@ class PreemptiveCache(object): self.add_data(request.path, entry_data) @contextlib.contextmanager - def disable_timestamp_update(self): + def disable_access_logging(self): with self._lock: - self._update_timestamp = False + self._log_access = False yield - self._update_timestamp = True + self._log_access = True def clean_all_data(self, cleanup_function): assert callable(cleanup_function) @@ -507,12 +507,14 @@ class PreemptiveCache(object): import copy to_persist = copy.deepcopy(data) to_persist["_timestamp"] = time.time() + to_persist["_count"] = 1 self._logger.info("Adding entry for {} and {!r}".format(root, to_persist)) - elif self._update_timestamp: + elif self._log_access: to_persist["_timestamp"] = time.time() - self._logger.debug("Updating timestamp for {} and {!r}".format(root, data)) + to_persist["_count"] = to_persist.get("_count", 0) + 1 + self._logger.debug("Updating timestamp and counter for {} and {!r}".format(root, data)) else: - self._logger.debug("Not updating timestamp for {} and {!r}, currently flagged as disabled".format(root, data)) + self._logger.debug("Not updating timestamp and counter for {} and {!r}, currently flagged as disabled".format(root, data)) self.set_data(root, [to_persist] + other) diff --git a/src/octoprint/server/views.py b/src/octoprint/server/views.py index 2632323b..987f7429 100644 --- a/src/octoprint/server/views.py +++ b/src/octoprint/server/views.py @@ -542,7 +542,7 @@ def _check_etag_and_lastmodified_for_i18n(): def _get_all_templates(): from octoprint.util.jinja import get_all_template_paths - return get_all_template_paths(app.jinja_loader, lambda path: not octoprint.util.is_hidden_path(path)) + return get_all_template_paths(app.jinja_loader) def _get_all_assets():