From 1dee5616923877d72f9b96cd52bd44a8775c865b Mon Sep 17 00:00:00 2001 From: Unrud Date: Fri, 2 Jun 2017 12:44:34 +0200 Subject: [PATCH] Add helper method for cleaning caches --- radicale/storage.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/radicale/storage.py b/radicale/storage.py index 049f94a..a256603 100644 --- a/radicale/storage.py +++ b/radicale/storage.py @@ -642,6 +642,34 @@ class Collection(BaseCollection): if item.collection._filesystem_path != to_collection._filesystem_path: cls._sync_directory(item.collection._filesystem_path) + @classmethod + def _clean_cache(cls, folder, names, max_age=None): + """Delete all ``names`` in ``folder`` that are older than ``max_age``. + """ + age_limit = time.time() - max_age if max_age is not None else None + modified = False + for name in names: + if not is_safe_filesystem_path_component(name): + continue + if age_limit is not None: + try: + # Race: Another process might have deleted the file. + mtime = os.path.getmtime(os.path.join(folder, name)) + except FileNotFoundError: + continue + if mtime > age_limit: + continue + cls.logger.debug("Found expired item in cache: %r", name) + # Race: Another process might have deleted or locked the + # file. + try: + os.remove(os.path.join(folder, name)) + except (FileNotFoundError, PermissionError): + continue + modified = True + if modified: + cls._sync_directory(folder) + def list(self): for href in os.listdir(self._filesystem_path): if not is_safe_filesystem_path_component(href):