cache: move prune-cache function to FileCache
This commit is contained in:
parent
3194388616
commit
a9d28df9ad
|
@ -13,8 +13,6 @@ CacheFallback = Union[bytes, Callable[[str], Awaitable[bytes]]]
|
|||
class Cache(ABC):
|
||||
"""Base class for caches"""
|
||||
|
||||
cache_base = xdg_cache_home() / "frontools"
|
||||
|
||||
@abstractmethod
|
||||
async def get(self, key: str, fallback: CacheFallback) -> bytes:
|
||||
"""Get an item in the cache, call fallback if it's not present"""
|
||||
|
@ -23,22 +21,6 @@ class Cache(ABC):
|
|||
def set(self, key: str, data: bytes) -> None:
|
||||
"""Set a resource in the cache"""
|
||||
|
||||
@staticmethod
|
||||
def prune(cache_names: list[str]) -> None:
|
||||
"""Remove caches from filesystem.
|
||||
|
||||
If empty list is provided, all caches will be cleaned
|
||||
"""
|
||||
if not len(cache_names):
|
||||
cache_names = [it.name for it in Cache.cache_base.iterdir() if it.is_dir()]
|
||||
for cache_name in cache_names:
|
||||
cache_path: Path = Cache.cache_base / cache_name
|
||||
if not cache_path.is_dir():
|
||||
echo(f"{cache_path} isn't a chache directory", err=True)
|
||||
continue
|
||||
echo(f"Removing {cache_path}")
|
||||
rmtree(cache_path)
|
||||
|
||||
@staticmethod
|
||||
async def _get_fallback_value(key: str, fallback: CacheFallback) -> bytes:
|
||||
if callable(fallback):
|
||||
|
@ -62,6 +44,8 @@ class NullCache(Cache):
|
|||
class FileCache(Cache):
|
||||
"""Cache on the local filesystem"""
|
||||
|
||||
cache_base = xdg_cache_home() / "frontools"
|
||||
|
||||
def __init__(self, name: str) -> None:
|
||||
self._name = name
|
||||
|
||||
|
@ -87,6 +71,24 @@ class FileCache(Cache):
|
|||
with open(cache_file_path, "wb") as cache_file:
|
||||
cache_file.write(resource)
|
||||
|
||||
@staticmethod
|
||||
def prune(cache_names: list[str]) -> None:
|
||||
"""Remove caches from filesystem.
|
||||
|
||||
If empty list is provided, all caches will be cleaned
|
||||
"""
|
||||
if not len(cache_names):
|
||||
cache_names = [
|
||||
it.name for it in FileCache.cache_base.iterdir() if it.is_dir()
|
||||
]
|
||||
for cache_name in cache_names:
|
||||
cache_path: Path = FileCache.cache_base / cache_name
|
||||
if not cache_path.is_dir():
|
||||
echo(f"{cache_path} isn't a chache directory", err=True)
|
||||
continue
|
||||
echo(f"Removing {cache_path}")
|
||||
rmtree(cache_path)
|
||||
|
||||
def _get_cache_file_path(self, key: str) -> Path:
|
||||
key_slug = _get_key_slug(key)
|
||||
cache_directory = self.cache_base / self._name
|
||||
|
|
|
@ -8,7 +8,7 @@ from click import Context as ClickContext
|
|||
from click import Path as PathArgument
|
||||
from click import argument, group, option, pass_context, pass_obj
|
||||
|
||||
from frontools.cache import Cache
|
||||
from frontools.cache import FileCache
|
||||
from frontools.config import Config
|
||||
from frontools.css import css_diff
|
||||
from frontools.screenshot import screenshot_diff
|
||||
|
@ -76,7 +76,7 @@ async def main(
|
|||
@argument("cache_names", nargs=-1)
|
||||
def prune_caches(cache_names: list[str]) -> None:
|
||||
"""Prune frontools caches"""
|
||||
Cache.prune(cache_names)
|
||||
FileCache.prune(cache_names)
|
||||
|
||||
|
||||
@main.command(name="css-diff")
|
||||
|
|
Loading…
Reference in New Issue