mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-08-06 11:18:06 +00:00
api_v3.py's font upload/delete handlers import delete_cached from web_interface.cache, but the function was never defined. The surrounding except ImportError silently swallowed the failure, so the fonts_catalog cache entry survived uploads/deletes and newly uploaded fonts did not appear until the TTL expired or the service restarted. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SXb4mKcAkVaxkeTb3YnAdr
49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
"""
|
|
Simple in-memory cache for expensive operations.
|
|
Separated from app.py to avoid circular import issues.
|
|
"""
|
|
import time
|
|
from typing import Any, Optional
|
|
|
|
|
|
# Simple in-memory cache for expensive operations
|
|
_cache = {}
|
|
_cache_timestamps = {}
|
|
|
|
|
|
def get_cached(key: str, ttl_seconds: int = 60) -> Optional[Any]:
|
|
"""Get value from cache if not expired."""
|
|
if key in _cache:
|
|
if time.time() - _cache_timestamps[key] < ttl_seconds:
|
|
return _cache[key]
|
|
else:
|
|
# Expired, remove
|
|
del _cache[key]
|
|
del _cache_timestamps[key]
|
|
return None
|
|
|
|
|
|
def set_cached(key: str, value: Any, ttl_seconds: int = 60) -> None:
|
|
"""Set value in cache with TTL."""
|
|
_cache[key] = value
|
|
_cache_timestamps[key] = time.time()
|
|
|
|
|
|
def delete_cached(key: str) -> None:
|
|
"""Remove a single key from the cache if present."""
|
|
_cache.pop(key, None)
|
|
_cache_timestamps.pop(key, None)
|
|
|
|
|
|
def invalidate_cache(pattern: Optional[str] = None) -> None:
|
|
"""Invalidate cache entries matching pattern, or all if pattern is None."""
|
|
if pattern is None:
|
|
_cache.clear()
|
|
_cache_timestamps.clear()
|
|
else:
|
|
keys_to_remove = [k for k in _cache.keys() if pattern in k]
|
|
for key in keys_to_remove:
|
|
del _cache[key]
|
|
del _cache_timestamps[key]
|
|
|