diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 2c9e39e1..21fc5698 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -663,15 +663,18 @@ 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()) @@ -683,7 +686,7 @@ class Server(): 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 0ba64d4b..57a53cf7 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)