From 6c719c01626569bd29fdeb3a3e5742aee95ad6f9 Mon Sep 17 00:00:00 2001 From: Sebastian Goscik Date: Sat, 8 Jul 2023 15:32:47 +0100 Subject: [PATCH] Cache camera names so an active protect connection is not need to perform actions like uploads which don't rely on protect. --- poetry.lock | 17 ++++++++++++++++- pyproject.toml | 1 + unifi_protect_backup/utils.py | 4 ++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/poetry.lock b/poetry.lock index 414bf76..4c8f130 100644 --- a/poetry.lock +++ b/poetry.lock @@ -201,6 +201,21 @@ PyYAML = "*" requests = "*" requests-oauthlib = "*" +[[package]] +name = "async-lru" +version = "2.0.3" +description = "Simple LRU cache for asyncio" +category = "main" +optional = false +python-versions = ">=3.8" +files = [ + {file = "async-lru-2.0.3.tar.gz", hash = "sha256:b714c9d1415fca4e264da72a9e2abc66880ce7430e03a973341f88ea4c0d4869"}, + {file = "async_lru-2.0.3-py3-none-any.whl", hash = "sha256:00c0a8899c20b9c88663a47732689ff98189c9fa08ad9f734d7722f934d250b1"}, +] + +[package.dependencies] +typing-extensions = {version = ">=4.0.0", markers = "python_version < \"3.11\""} + [[package]] name = "async-timeout" version = "4.0.2" @@ -2421,4 +2436,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = ">=3.9.0,<4.0" -content-hash = "5deb339fab9bbf61b295ef5ee811d233f573f6473d83aab5e3951c7216fb2897" +content-hash = "42a25af6210e11892bc9e94fb4a4565e06f9dfe9239a8f277f4970e85de72218" diff --git a/pyproject.toml b/pyproject.toml index 2dd37e9..492b575 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,6 +29,7 @@ aiosqlite = "^0.17.0" python-dateutil = "^2.8.2" apprise = "^1.3.0" expiring-dict = "^1.1.0" +async-lru = "^2.0.3" [tool.poetry.group.dev] optional = true diff --git a/unifi_protect_backup/utils.py b/unifi_protect_backup/utils.py index 1e8ae56..88a0cfd 100644 --- a/unifi_protect_backup/utils.py +++ b/unifi_protect_backup/utils.py @@ -10,6 +10,7 @@ from apprise import NotifyType from dateutil.relativedelta import relativedelta from pyunifiprotect import ProtectApiClient from pyunifiprotect.data.nvr import Event +from async_lru import alru_cache from unifi_protect_backup import notifications @@ -277,6 +278,9 @@ def human_readable_to_float(num: str): return value * multiplier +# Cached so that actions like uploads can continue when the connection to the api is lost +# No max size, and a 6 hour ttl +@alru_cache(None, ttl=60 * 60 * 6) async def get_camera_name(protect: ProtectApiClient, id: str): """Returns the name for the camera with the given ID.