async-lru -> async-cache

This commit is contained in:
Jörn-Michael Miehe 2022-10-28 11:06:02 +00:00
parent 994dc827db
commit db4686fdc5
5 changed files with 17 additions and 52 deletions

View file

@ -4,13 +4,8 @@ Some useful helpers for working in async contexts.
from asyncio import get_running_loop
from functools import partial, wraps
from time import time
from typing import Awaitable, Callable, TypeVar
from async_lru import alru_cache
from .settings import SETTINGS
RT = TypeVar("RT")
@ -32,31 +27,3 @@ def run_in_executor(
)
return wrapper
def get_ttl_hash() -> int:
"""
Return the same value within `seconds` time period.
https://stackoverflow.com/a/55900800
"""
return round(time() / SETTINGS.cache_time)
def timed_alru_cache(*decorator_args, **decorator_kwargs):
"""
Decorator which adds an (unused) param `ttl_hash`
and the `alru_cache` annotation to a function.
"""
def decorate(f):
@alru_cache(*decorator_args, **decorator_kwargs)
@wraps(f)
async def wrapper(ttl_hash: int, *args, **kwargs):
del ttl_hash
return await f(*args, **kwargs)
return wrapper
return decorate

View file

@ -10,12 +10,13 @@ from functools import total_ordering
from logging import getLogger
from typing import Iterator
from cache import AsyncTTL
from caldav import Calendar
from caldav.lib.error import ReportError
from pydantic import BaseModel, validator
from vobject.base import Component
from .async_helpers import get_ttl_hash, run_in_executor, timed_alru_cache
from .async_helpers import run_in_executor
from .config import Config
from .dav_common import caldav_principal
from .settings import SETTINGS
@ -110,7 +111,7 @@ class CalEvent(BaseModel):
return cls.parse_obj(data)
@timed_alru_cache(maxsize=SETTINGS.cache_size)
@AsyncTTL(time_to_live=SETTINGS.cache_time, maxsize=SETTINGS.cache_size)
async def _get_calendar(
calendar_name: str,
) -> Calendar:
@ -125,7 +126,7 @@ async def _get_calendar(
return await _inner()
@timed_alru_cache(maxsize=SETTINGS.cache_size)
@AsyncTTL(time_to_live=SETTINGS.cache_time, maxsize=SETTINGS.cache_size)
async def _get_calendar_events(
calendar_name: str,
) -> list[CalEvent]:
@ -198,7 +199,6 @@ class DavCalendar:
"""
return await _get_calendar(
ttl_hash=get_ttl_hash(), # type: ignore
calendar_name=self.calendar_name,
)
@ -209,6 +209,5 @@ class DavCalendar:
"""
return await _get_calendar_events(
ttl_hash=get_ttl_hash(), # type: ignore
calendar_name=self.calendar_name,
)

View file

@ -9,16 +9,17 @@ from io import BytesIO
from logging import getLogger
from typing import Any
from cache import AsyncTTL
from webdav3.client import Resource
from .async_helpers import get_ttl_hash, run_in_executor, timed_alru_cache
from .async_helpers import run_in_executor
from .dav_common import webdav_resource
from .settings import SETTINGS
_logger = getLogger(__name__)
@timed_alru_cache(maxsize=SETTINGS.cache_size)
@AsyncTTL(time_to_live=SETTINGS.cache_time, maxsize=SETTINGS.cache_size)
async def _get_buffer(
remote_path: Any,
) -> BytesIO:
@ -61,7 +62,6 @@ class DavFile:
"""
return await _get_buffer(
ttl_hash=get_ttl_hash(), # type: ignore
remote_path=self.remote_path,
)

19
api/poetry.lock generated
View file

@ -16,12 +16,12 @@ test = ["contextlib2", "coverage[toml] (>=4.5)", "hypothesis (>=4.0)", "mock (>=
trio = ["trio (>=0.16)"]
[[package]]
name = "async-lru"
version = "1.0.3"
description = "Simple lru_cache for asyncio"
name = "async-cache"
version = "1.1.1"
description = "An asyncio Cache"
category = "main"
optional = false
python-versions = ">=3.6"
python-versions = ">=3.3"
[[package]]
name = "caldav"
@ -56,7 +56,7 @@ optional = false
python-versions = ">=3.6.0"
[package.extras]
unicode_backport = ["unicodedata2"]
unicode-backport = ["unicodedata2"]
[[package]]
name = "click"
@ -248,7 +248,7 @@ urllib3 = ">=1.21.1,<1.27"
[package.extras]
socks = ["PySocks (>=1.5.6,!=1.5.7)"]
use_chardet_on_py3 = ["chardet (>=3.0.2,<6)"]
use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"]
[[package]]
name = "six"
@ -396,16 +396,15 @@ testing = ["func-timeout", "jaraco.itertools", "pytest (>=6)", "pytest-black (>=
[metadata]
lock-version = "1.1"
python-versions = "^3.9"
content-hash = "b25d37c0bf9187d599709a66e05d21c0df7da2b140373f6cd50b070ae2f073ab"
content-hash = "677584ca707d868e2ccfca17482e1cbef7f78c038aae9f995642c83a59e34e5b"
[metadata.files]
anyio = [
{file = "anyio-3.6.1-py3-none-any.whl", hash = "sha256:cb29b9c70620506a9a8f87a309591713446953302d7d995344d0d7c6c0c9a7be"},
{file = "anyio-3.6.1.tar.gz", hash = "sha256:413adf95f93886e442aea925f3ee43baa5a765a64a0f52c6081894f9992fdd0b"},
]
async-lru = [
{file = "async-lru-1.0.3.tar.gz", hash = "sha256:c2cb9b2915eb14e6cf3e717154b40f715bf90e596d73623677affd0d1fbcd32a"},
{file = "async_lru-1.0.3-py3-none-any.whl", hash = "sha256:ea692c303feb6211ff260d230dae1583636f13e05c9ae616eada77855b7f415c"},
async-cache = [
{file = "async-cache-1.1.1.tar.gz", hash = "sha256:81aa9ccd19fb06784aaf30bd5f2043dc0a23fc3e998b93d0c2c17d1af9803393"},
]
caldav = [
{file = "caldav-0.9.1-py3-none-any.whl", hash = "sha256:0a3854adbb0c4e1fe042e9eed26dffbf1e696705c87cadd8f3ceb39cf4b4b274"},

View file

@ -8,7 +8,6 @@ version = "0.1.0"
[tool.poetry.dependencies]
Markdown = "^3.4.1"
Pillow = "^9.2.0"
async-lru = "^1.0.3"
caldav = "^0.9.1"
fastapi = "^0.81.0"
pydantic = {extras = ["dotenv"], version = "^1.9.2"}
@ -18,6 +17,7 @@ tomli = "^2.0.1"
tomli-w = "^1.0.0"
uvicorn = "^0.18.3"
webdavclient3 = "3.14.5"
async-cache = "^1.1.1"
[tool.poetry.dev-dependencies]
# pytest = "^5.2"