Add helper method for cleaning caches
This commit is contained in:
parent
3009ce5414
commit
1dee561692
@ -642,6 +642,34 @@ class Collection(BaseCollection):
|
|||||||
if item.collection._filesystem_path != to_collection._filesystem_path:
|
if item.collection._filesystem_path != to_collection._filesystem_path:
|
||||||
cls._sync_directory(item.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):
|
def list(self):
|
||||||
for href in os.listdir(self._filesystem_path):
|
for href in os.listdir(self._filesystem_path):
|
||||||
if not is_safe_filesystem_path_component(href):
|
if not is_safe_filesystem_path_component(href):
|
||||||
|
Loading…
Reference in New Issue
Block a user