From c72a11e35eccd21fee16642aecaf7d6a999c941e Mon Sep 17 00:00:00 2001 From: Rodja Trappe Date: Sun, 15 Dec 2024 07:09:29 +0100 Subject: [PATCH] only use redis if NICEGUI_REDIS_URL is provided as environment variable --- nicegui/persistence/file_persistent_dict.py | 6 ++++-- nicegui/persistence/persistent_dict.py | 11 +++++++++++ nicegui/persistence/redis_persistent_dict.py | 8 +++++--- nicegui/storage.py | 6 +++++- 4 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 nicegui/persistence/persistent_dict.py diff --git a/nicegui/persistence/file_persistent_dict.py b/nicegui/persistence/file_persistent_dict.py index 7a882e583..50a58679d 100644 --- a/nicegui/persistence/file_persistent_dict.py +++ b/nicegui/persistence/file_persistent_dict.py @@ -3,11 +3,13 @@ import aiofiles -from nicegui import background_tasks, core, json, observables +from nicegui import background_tasks, core, json from nicegui.logging import log +from .persistent_dict import PersistentDict -class FilePersistentDict(observables.ObservableDict): + +class FilePersistentDict(PersistentDict): def __init__(self, filepath: Path, encoding: Optional[str] = None, *, indent: bool = False) -> None: self.filepath = filepath diff --git a/nicegui/persistence/persistent_dict.py b/nicegui/persistence/persistent_dict.py new file mode 100644 index 000000000..ce113ec4c --- /dev/null +++ b/nicegui/persistence/persistent_dict.py @@ -0,0 +1,11 @@ + +from nicegui import observables + + +class PersistentDict(observables.ObservableDict): + + async def initialize(self) -> None: + """Load initial data from the persistence layer.""" + + async def close(self) -> None: + """Clean up the persistence layer.""" diff --git a/nicegui/persistence/redis_persistent_dict.py b/nicegui/persistence/redis_persistent_dict.py index 6b9a38f78..d8f0d16f0 100644 --- a/nicegui/persistence/redis_persistent_dict.py +++ b/nicegui/persistence/redis_persistent_dict.py @@ -1,12 +1,14 @@ import redis.asyncio as redis -from nicegui import background_tasks, core, json, observables +from nicegui import background_tasks, core, json from nicegui.logging import log +from .persistent_dict import PersistentDict -class RedisPersistentDict(observables.ObservableDict): - def __init__(self, redis_url: str = 'redis://localhost:6379', key_prefix: str = 'nicegui:', encoding: str = 'utf-8') -> None: +class RedisPersistentDict(PersistentDict): + + def __init__(self, redis_url: str, key_prefix: str = 'nicegui:', encoding: str = 'utf-8') -> None: self.redis_client = redis.from_url(redis_url) self.pubsub = self.redis_client.pubsub() self.key_prefix = key_prefix diff --git a/nicegui/storage.py b/nicegui/storage.py index 1456569f0..48d17094d 100644 --- a/nicegui/storage.py +++ b/nicegui/storage.py @@ -53,9 +53,13 @@ class Storage: def __init__(self) -> None: self.path = Path(os.environ.get('NICEGUI_STORAGE_PATH', '.nicegui')).resolve() + """Path to use for local persistence. Defaults to '.nicegui'.""" self.max_tab_storage_age: float = timedelta(days=30).total_seconds() """Maximum age in seconds before tab storage is automatically purged. Defaults to 30 days.""" - self._general = RedisPersistentDict() # PersistentDict(self.path / 'storage-general.json', encoding='utf-8') + self.redis_url = os.environ.get('NICEGUI_REDIS_URL', None) + """URL to use for shared persistent storage via Redis. Defaults to None, which means local file storage is used.""" + self._general = RedisPersistentDict(self.redis_url) if self.redis_url \ + else FilePersistentDict(self.path / 'storage-general.json', encoding='utf-8') self._users: Dict[str, FilePersistentDict] = {} self._tabs: Dict[str, observables.ObservableDict] = {}