mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-11-23 08:13:01 +00:00
DAV Caching
This commit is contained in:
parent
c0302fbe3c
commit
cf812f23cd
6 changed files with 29 additions and 8 deletions
|
@ -1,7 +1,7 @@
|
||||||
import re
|
import re
|
||||||
from io import BytesIO, TextIOWrapper
|
from io import BytesIO, TextIOWrapper
|
||||||
from typing import TextIO
|
|
||||||
|
|
||||||
|
from cache import AsyncTTL
|
||||||
from webdav3.client import Client as WebDAVclient
|
from webdav3.client import Client as WebDAVclient
|
||||||
|
|
||||||
from .settings import SETTINGS
|
from .settings import SETTINGS
|
||||||
|
@ -14,6 +14,7 @@ _WEBDAV_CLIENT = WebDAVclient({
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@AsyncTTL(time_to_live=SETTINGS.cache_ttl)
|
||||||
async def dav_list_files(regex: re.Pattern, directory: str = "") -> list[str]:
|
async def dav_list_files(regex: re.Pattern, directory: str = "") -> list[str]:
|
||||||
ls = _WEBDAV_CLIENT.list(directory)
|
ls = _WEBDAV_CLIENT.list(directory)
|
||||||
return [
|
return [
|
||||||
|
@ -23,15 +24,18 @@ async def dav_list_files(regex: re.Pattern, directory: str = "") -> list[str]:
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@AsyncTTL(time_to_live=SETTINGS.cache_ttl)
|
||||||
async def dav_get_file(path: str) -> BytesIO:
|
async def dav_get_file(path: str) -> BytesIO:
|
||||||
resource = _WEBDAV_CLIENT.resource(path)
|
resource = _WEBDAV_CLIENT.resource(path)
|
||||||
buffer = BytesIO()
|
buffer = BytesIO()
|
||||||
resource.write_to(buffer)
|
resource.write_to(buffer)
|
||||||
buffer.seek(0)
|
|
||||||
|
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
|
|
||||||
async def dav_get_textfile(path: str, encoding="utf-8") -> TextIO:
|
@AsyncTTL(time_to_live=SETTINGS.cache_ttl)
|
||||||
|
async def dav_get_textfile_content(path: str, encoding="utf-8") -> str:
|
||||||
buffer = await dav_get_file(path)
|
buffer = await dav_get_file(path)
|
||||||
return TextIOWrapper(buffer, encoding=encoding)
|
tio = TextIOWrapper(buffer, encoding=encoding)
|
||||||
|
tio.seek(0)
|
||||||
|
return tio.read().strip()
|
||||||
|
|
|
@ -2,12 +2,13 @@ import itertools
|
||||||
import random
|
import random
|
||||||
from typing import Any, Sequence
|
from typing import Any, Sequence
|
||||||
|
|
||||||
from ..dav_common import dav_get_textfile
|
from advent22_api.settings import SETTINGS
|
||||||
|
|
||||||
|
from ..dav_common import dav_get_textfile_content
|
||||||
|
|
||||||
|
|
||||||
async def get_loesungswort() -> str:
|
async def get_loesungswort() -> str:
|
||||||
fp = await dav_get_textfile("loesungswort.txt")
|
return await dav_get_textfile_content(SETTINGS.solution_filename)
|
||||||
return fp.read().strip()
|
|
||||||
|
|
||||||
|
|
||||||
async def get_rnd(bonus_salt: Any = "") -> random.Random:
|
async def get_rnd(bonus_salt: Any = "") -> random.Random:
|
||||||
|
|
|
@ -77,6 +77,7 @@ async def load_image(
|
||||||
|
|
||||||
# Bild laden
|
# Bild laden
|
||||||
img_buffer = await dav_get_file(images[index])
|
img_buffer = await dav_get_file(images[index])
|
||||||
|
img_buffer.seek(0)
|
||||||
return await AdventImage.load_standard(img_buffer)
|
return await AdventImage.load_standard(img_buffer)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,9 @@ class Settings(BaseSettings):
|
||||||
|
|
||||||
webdav: DavSettings = DavSettings()
|
webdav: DavSettings = DavSettings()
|
||||||
|
|
||||||
|
cache_ttl: int = 30
|
||||||
|
solution_filename: str = "loesungswort.txt"
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
env_file = ".env"
|
env_file = ".env"
|
||||||
env_file_encoding = "utf-8"
|
env_file_encoding = "utf-8"
|
||||||
|
|
13
api/poetry.lock
generated
13
api/poetry.lock
generated
|
@ -15,6 +15,14 @@ doc = ["packaging", "sphinx-autodoc-typehints (>=1.2.0)", "sphinx-rtd-theme"]
|
||||||
test = ["contextlib2", "coverage[toml] (>=4.5)", "hypothesis (>=4.0)", "mock (>=4)", "pytest (>=7.0)", "pytest-mock (>=3.6.1)", "trustme", "uvloop (<0.15)", "uvloop (>=0.15)"]
|
test = ["contextlib2", "coverage[toml] (>=4.5)", "hypothesis (>=4.0)", "mock (>=4)", "pytest (>=7.0)", "pytest-mock (>=3.6.1)", "trustme", "uvloop (<0.15)", "uvloop (>=0.15)"]
|
||||||
trio = ["trio (>=0.16)"]
|
trio = ["trio (>=0.16)"]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "async-cache"
|
||||||
|
version = "1.1.1"
|
||||||
|
description = "An asyncio Cache"
|
||||||
|
category = "main"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "certifi"
|
name = "certifi"
|
||||||
version = "2022.9.24"
|
version = "2022.9.24"
|
||||||
|
@ -316,13 +324,16 @@ python-versions = ">=3.7"
|
||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "1.1"
|
lock-version = "1.1"
|
||||||
python-versions = "^3.10"
|
python-versions = "^3.10"
|
||||||
content-hash = "eb18799d40638df47cfa261dd96d530cfefdcdc60bde70f156d48820e870a144"
|
content-hash = "d37ea64eccdac520a3649119169642395eaeeb42ab20668c444774f2baccede3"
|
||||||
|
|
||||||
[metadata.files]
|
[metadata.files]
|
||||||
anyio = [
|
anyio = [
|
||||||
{file = "anyio-3.6.1-py3-none-any.whl", hash = "sha256:cb29b9c70620506a9a8f87a309591713446953302d7d995344d0d7c6c0c9a7be"},
|
{file = "anyio-3.6.1-py3-none-any.whl", hash = "sha256:cb29b9c70620506a9a8f87a309591713446953302d7d995344d0d7c6c0c9a7be"},
|
||||||
{file = "anyio-3.6.1.tar.gz", hash = "sha256:413adf95f93886e442aea925f3ee43baa5a765a64a0f52c6081894f9992fdd0b"},
|
{file = "anyio-3.6.1.tar.gz", hash = "sha256:413adf95f93886e442aea925f3ee43baa5a765a64a0f52c6081894f9992fdd0b"},
|
||||||
]
|
]
|
||||||
|
async-cache = [
|
||||||
|
{file = "async-cache-1.1.1.tar.gz", hash = "sha256:81aa9ccd19fb06784aaf30bd5f2043dc0a23fc3e998b93d0c2c17d1af9803393"},
|
||||||
|
]
|
||||||
certifi = [
|
certifi = [
|
||||||
{file = "certifi-2022.9.24-py3-none-any.whl", hash = "sha256:90c1a32f1d68f940488354e36370f6cca89f0f106db09518524c88d6ed83f382"},
|
{file = "certifi-2022.9.24-py3-none-any.whl", hash = "sha256:90c1a32f1d68f940488354e36370f6cca89f0f106db09518524c88d6ed83f382"},
|
||||||
{file = "certifi-2022.9.24.tar.gz", hash = "sha256:0d9c601124e5a6ba9712dbc60d9c53c21e34f5f641fe83002317394311bdce14"},
|
{file = "certifi-2022.9.24.tar.gz", hash = "sha256:0d9c601124e5a6ba9712dbc60d9c53c21e34f5f641fe83002317394311bdce14"},
|
||||||
|
|
|
@ -15,6 +15,7 @@ uvicorn = {extras = ["standard"], version = "^0.18.3"}
|
||||||
Pillow = "^9.2.0"
|
Pillow = "^9.2.0"
|
||||||
numpy = "^1.23.3"
|
numpy = "^1.23.3"
|
||||||
webdavclient3 = "3.14.5"
|
webdavclient3 = "3.14.5"
|
||||||
|
async-cache = "^1.1.1"
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue