Files
rothbard/cache.py
2025-10-31 11:19:49 -07:00

44 lines
1.3 KiB
Python

# In-memory cache for Filevine projects
import threading
import time
from datetime import datetime, timedelta
class ProjectCache:
def __init__(self):
self._cache = {}
self._lock = threading.Lock()
self._last_updated = None
self._is_updating = False
def get_projects(self):
"""Get cached projects if they exist and are not expired (15 minutes)"""
with self._lock:
if not self._cache or not self._last_updated:
return None
# Check if cache is older than 15 minutes
if datetime.now() - self._last_updated > timedelta(minutes=15):
return None
return self._cache.copy()
def set_projects(self, projects):
print(f"Caching new projects: {len(projects)}")
"""Set projects in cache with current timestamp"""
with self._lock:
self._cache = projects.copy() if projects else {}
self._last_updated = datetime.now()
def is_updating(self):
"""Check if cache is currently being updated"""
with self._lock:
return self._is_updating
def set_updating(self, updating):
"""Set the updating status"""
with self._lock:
self._is_updating = updating
# Global cache instance
project_cache = ProjectCache()