mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-11 05:13:01 +00:00
fix: handle string timestamps in cache manager
This commit is contained in:
@@ -80,39 +80,28 @@ class CacheManager:
|
|||||||
return os.path.join(self.cache_dir, f"{key}.json")
|
return os.path.join(self.cache_dir, f"{key}.json")
|
||||||
|
|
||||||
def get_cached_data(self, key: str, max_age: int = 300) -> Optional[Dict]:
|
def get_cached_data(self, key: str, max_age: int = 300) -> Optional[Dict]:
|
||||||
"""
|
"""Get data from cache if it exists and is not stale."""
|
||||||
Get cached data if it exists and is not too old.
|
if key not in self._memory_cache:
|
||||||
Args:
|
|
||||||
key: Cache key
|
|
||||||
max_age: Maximum age of cache in seconds
|
|
||||||
Returns:
|
|
||||||
Cached data or None if not found/too old
|
|
||||||
"""
|
|
||||||
# Check memory cache first
|
|
||||||
if key in self._memory_cache:
|
|
||||||
data, timestamp = self._memory_cache[key]
|
|
||||||
if time.time() - timestamp <= max_age:
|
|
||||||
return data
|
|
||||||
|
|
||||||
# Check file cache
|
|
||||||
cache_path = self._get_cache_path(key)
|
|
||||||
if not os.path.exists(cache_path):
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
timestamp = self._memory_cache_timestamps.get(key)
|
||||||
# Check file age
|
if timestamp is None:
|
||||||
if time.time() - os.path.getmtime(cache_path) > max_age:
|
return None
|
||||||
|
|
||||||
|
# Convert timestamp to float if it's a string
|
||||||
|
if isinstance(timestamp, str):
|
||||||
|
try:
|
||||||
|
timestamp = float(timestamp)
|
||||||
|
except ValueError:
|
||||||
|
self.logger.error(f"Invalid timestamp format for key {key}: {timestamp}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Load and return cached data
|
if time.time() - timestamp <= max_age:
|
||||||
with self._cache_lock:
|
return self._memory_cache[key]
|
||||||
with open(cache_path, 'r') as f:
|
else:
|
||||||
data = json.load(f)
|
# Data is stale, remove it
|
||||||
# Update memory cache
|
self._memory_cache.pop(key, None)
|
||||||
self._memory_cache[key] = (data, time.time())
|
self._memory_cache_timestamps.pop(key, None)
|
||||||
return data
|
|
||||||
|
|
||||||
except Exception:
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def save_cache(self, key: str, data: Dict) -> None:
|
def save_cache(self, key: str, data: Dict) -> None:
|
||||||
@@ -130,7 +119,8 @@ class CacheManager:
|
|||||||
json.dump(data, f)
|
json.dump(data, f)
|
||||||
|
|
||||||
# Update memory cache
|
# Update memory cache
|
||||||
self._memory_cache[key] = (data, time.time())
|
self._memory_cache[key] = data
|
||||||
|
self._memory_cache_timestamps[key] = time.time()
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
pass # Silently fail if cache save fails
|
pass # Silently fail if cache save fails
|
||||||
|
|||||||
Reference in New Issue
Block a user