Skip to content
4 changes: 2 additions & 2 deletions application/single_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ def inject_settings():
try:
user_id = get_current_user_id()
if user_id:
from functions_settings import get_user_settings
user_settings = get_user_settings(user_id) or {}
from functions_settings import get_user_ui_settings
user_settings = get_user_ui_settings(user_id) or {}
except Exception as e:
print(f"Error injecting user settings: {e}")
log_event(f"Error injecting user settings: {e}", level=logging.ERROR)
Expand Down
56 changes: 55 additions & 1 deletion application/single_app/app_settings_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
import json
import logging
import copy
import threading
import time
from datetime import datetime
Expand All @@ -19,6 +20,7 @@
_settings = None
_logger = logging.getLogger(__name__)
APP_SETTINGS_CACHE = {}
APP_USER_UI_SETTINGS_CACHE = {}
APP_STREAM_SESSION_METADATA = {}
APP_STREAM_SESSION_EVENTS = {}
APP_SETTINGS_CACHE_VERSION = 0
Expand All @@ -28,6 +30,8 @@
APP_SETTINGS_CACHE_KEY = 'APP_SETTINGS_CACHE'
APP_SETTINGS_CACHE_VERSION_KEY = 'APP_SETTINGS_CACHE_VERSION'
APP_SETTINGS_CACHE_VERSION_DOC_ID = 'app_settings_cache_version'
USER_UI_SETTINGS_CACHE_KEY_PREFIX = 'USER_UI_SETTINGS'
USER_UI_SETTINGS_CACHE_TTL_SECONDS = 120
GOVERNANCE_CACHE_VERSION_KEY = 'GOVERNANCE_CACHE_VERSION'
GOVERNANCE_CACHE_VERSION_DOC_ID = 'governance_cache_version'
CACHE_VERSION_DOC_TYPE = 'cache_version'
Expand All @@ -42,6 +46,9 @@
append_stream_session_event = None
get_stream_session_events = None
delete_stream_session_cache = None
get_user_ui_settings_cache = None
set_user_ui_settings_cache = None
delete_user_ui_settings_cache = None
get_governance_cache_version = None
bump_governance_cache_version = None
app_cache_is_using_redis = False
Expand Down Expand Up @@ -119,11 +126,12 @@ def _set_ttl_cached_version(version_cache, version):

def configure_app_cache(settings, redis_cache_endpoint=None):
global _settings, update_settings_cache, get_settings_cache, APP_SETTINGS_CACHE
global APP_STREAM_SESSION_METADATA, APP_STREAM_SESSION_EVENTS
global APP_USER_UI_SETTINGS_CACHE, APP_STREAM_SESSION_METADATA, APP_STREAM_SESSION_EVENTS
global APP_SETTINGS_CACHE_VERSION, APP_GOVERNANCE_CACHE_VERSION
global APP_SETTINGS_SHARED_VERSION_CACHE, APP_GOVERNANCE_SHARED_VERSION_CACHE
global initialize_stream_session_cache, set_stream_session_meta, get_stream_session_meta
global append_stream_session_event, get_stream_session_events, delete_stream_session_cache
global get_user_ui_settings_cache, set_user_ui_settings_cache, delete_user_ui_settings_cache
global get_app_settings_cache_version, bump_app_settings_cache_version
global get_governance_cache_version, bump_governance_cache_version
global app_cache_is_using_redis
Expand Down Expand Up @@ -288,6 +296,24 @@ def delete_stream_session_cache_redis(cache_key):
get_stream_session_events_key(cache_key),
)

def get_user_ui_settings_cache_key(user_id):
return f'{USER_UI_SETTINGS_CACHE_KEY_PREFIX}:{user_id}'

def get_user_ui_settings_cache_redis(user_id):
cached = redis_client.get(get_user_ui_settings_cache_key(user_id))
return json.loads(cached) if cached else None

def set_user_ui_settings_cache_redis(user_id, ui_settings, ttl_seconds=None):
ttl = int(ttl_seconds or USER_UI_SETTINGS_CACHE_TTL_SECONDS)
redis_client.setex(
get_user_ui_settings_cache_key(user_id),
ttl,
json.dumps(ui_settings or {})
)

def delete_user_ui_settings_cache_redis(user_id):
redis_client.delete(get_user_ui_settings_cache_key(user_id))

def get_governance_cache_version_redis():
cached = redis_client.get(GOVERNANCE_CACHE_VERSION_KEY)
if cached is None:
Expand All @@ -308,6 +334,9 @@ def bump_governance_cache_version_redis():
append_stream_session_event = append_stream_session_event_redis
get_stream_session_events = get_stream_session_events_redis
delete_stream_session_cache = delete_stream_session_cache_redis
get_user_ui_settings_cache = get_user_ui_settings_cache_redis
set_user_ui_settings_cache = set_user_ui_settings_cache_redis
delete_user_ui_settings_cache = delete_user_ui_settings_cache_redis
get_governance_cache_version = get_governance_cache_version_redis
bump_governance_cache_version = bump_governance_cache_version_redis

Expand Down Expand Up @@ -408,6 +437,28 @@ def delete_stream_session_cache_mem(cache_key):
APP_STREAM_SESSION_METADATA.pop(cache_key, None)
APP_STREAM_SESSION_EVENTS.pop(cache_key, None)

def get_user_ui_settings_cache_mem(user_id):
with _app_cache_lock:
entry = APP_USER_UI_SETTINGS_CACHE.get(user_id)
if _is_expired(entry):
APP_USER_UI_SETTINGS_CACHE.pop(user_id, None)
return None
return copy.deepcopy(entry.get('value') or {})

def set_user_ui_settings_cache_mem(user_id, ui_settings, ttl_seconds=None):
expiration_timestamp = _get_expiration_timestamp(
ttl_seconds or USER_UI_SETTINGS_CACHE_TTL_SECONDS
)
with _app_cache_lock:
APP_USER_UI_SETTINGS_CACHE[user_id] = {
'value': copy.deepcopy(ui_settings or {}),
'expires_at': expiration_timestamp,
}

def delete_user_ui_settings_cache_mem(user_id):
with _app_cache_lock:
APP_USER_UI_SETTINGS_CACHE.pop(user_id, None)

def get_app_settings_cache_version_mem():
global APP_SETTINGS_CACHE_VERSION
try:
Expand Down Expand Up @@ -494,5 +545,8 @@ def bump_governance_cache_version_mem():
append_stream_session_event = append_stream_session_event_mem
get_stream_session_events = get_stream_session_events_mem
delete_stream_session_cache = delete_stream_session_cache_mem
get_user_ui_settings_cache = get_user_ui_settings_cache_mem
set_user_ui_settings_cache = set_user_ui_settings_cache_mem
delete_user_ui_settings_cache = delete_user_ui_settings_cache_mem
get_governance_cache_version = get_governance_cache_version_mem
bump_governance_cache_version = bump_governance_cache_version_mem
2 changes: 1 addition & 1 deletion application/single_app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
EXECUTOR_TYPE = 'thread'
EXECUTOR_MAX_WORKERS = 30
SESSION_TYPE = 'filesystem'
VERSION = "0.242.043"
VERSION = "0.242.044"

SECRET_KEY = os.getenv('SECRET_KEY', 'dev-secret-key-change-in-production')

Expand Down
Loading
Loading