Skip to content

Commit

Permalink
only use redis if NICEGUI_REDIS_URL is provided as environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
rodja committed Dec 15, 2024
1 parent a387927 commit c72a11e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
6 changes: 4 additions & 2 deletions nicegui/persistence/file_persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions nicegui/persistence/persistent_dict.py
Original file line number Diff line number Diff line change
@@ -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."""
8 changes: 5 additions & 3 deletions nicegui/persistence/redis_persistent_dict.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion nicegui/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}

Expand Down

0 comments on commit c72a11e

Please sign in to comment.