diff --git a/api/advent22_api/core/dav/helpers.py b/api/advent22_api/core/dav/helpers.py index e15617d..a6e2bd2 100644 --- a/api/advent22_api/core/dav/helpers.py +++ b/api/advent22_api/core/dav/helpers.py @@ -1,4 +1,5 @@ from json import JSONDecodeError +from typing import Callable, Hashable import requests from cachetools.keys import hashkey @@ -8,11 +9,17 @@ from redis.typing import EncodableT from webdav3.client import Client as __WebDAVclient -def davkey(name, _, *args, **kwargs): - """Return a cache key for use with cached methods.""" +def davkey( + name: str, + slice: slice = slice(1, None), +) -> Callable[..., tuple[Hashable, ...]]: + def func(*args, **kwargs) -> tuple[Hashable, ...]: + """Return a cache key for use with cached methods.""" - key = hashkey(name, *args, **kwargs) - return hashkey(*(str(key_item) for key_item in key)) + key = hashkey(name, *args[slice], **kwargs) + return hashkey(*(str(key_item) for key_item in key)) + + return func class WebDAVclient(__WebDAVclient): diff --git a/api/advent22_api/core/dav/webdav.py b/api/advent22_api/core/dav/webdav.py index 4c316a5..acb5097 100644 --- a/api/advent22_api/core/dav/webdav.py +++ b/api/advent22_api/core/dav/webdav.py @@ -1,4 +1,3 @@ -import functools import logging import re from io import BytesIO @@ -34,10 +33,7 @@ class WebDAV: @classmethod @asyncify - @cachedmethod( - cache=lambda cls: cls._cache, - key=functools.partial(davkey, "list_files"), - ) + @cachedmethod(cache=lambda cls: cls._cache, key=davkey("list_files")) def list_files( cls, directory: str = "", @@ -55,10 +51,7 @@ class WebDAV: @classmethod @asyncify - @cachedmethod( - cache=lambda cls: cls._cache, - key=functools.partial(davkey, "exists"), - ) + @cachedmethod(cache=lambda cls: cls._cache, key=davkey("exists")) def exists(cls, path: str) -> bool: """ `True` iff there is a WebDAV resource at `path` @@ -69,10 +62,7 @@ class WebDAV: @classmethod @asyncify - @cachedmethod( - cache=lambda cls: cls._cache, - key=functools.partial(davkey, "read_bytes"), - ) + @cachedmethod(cache=lambda cls: cls._cache, key=davkey("read_bytes")) def read_bytes(cls, path: str) -> bytes: """ Load WebDAV file from `path` as bytes @@ -105,7 +95,7 @@ class WebDAV: cls._webdav_client.upload_to(buffer, path) # invalidate cache entry - cls._cache.pop(davkey("read_bytes", None, path)) + cls._cache.pop(davkey("read_bytes")(path)) @classmethod async def write_str(cls, path: str, content: str, encoding="utf-8") -> None: