Use Redis for cache indexes - enables multi-worker scaling

The cache_manager now uses Redis hashes for the content_index and
ipfs_cids mappings. This allows multiple uvicorn workers to share
state, so files added by one worker are immediately visible to all
others.

- Added redis_client parameter to L1CacheManager
- Index lookups check Redis first, then fall back to in-memory
- Index updates go to both Redis and JSON file (backup)
- Migrates existing JSON indexes to Redis on first load
- Re-enabled workers=4 in uvicorn

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
gilesb
2026-01-09 04:19:00 +00:00
parent 6fc3562d22
commit a0a4c08b9a
2 changed files with 142 additions and 43 deletions

View File

@@ -57,10 +57,7 @@ IPFS_GATEWAY_URL = os.environ.get("IPFS_GATEWAY_URL", "")
CACHE_DIR = Path(os.environ.get("CACHE_DIR", str(Path.home() / ".artdag" / "cache")))
CACHE_DIR.mkdir(parents=True, exist_ok=True)
# Initialize L1 cache manager (no L2 config - determined dynamically from token)
cache_manager = L1CacheManager(cache_dir=CACHE_DIR)
# Redis for persistent run storage
# Redis for persistent run storage and shared cache index (multi-worker support)
REDIS_URL = os.environ.get('REDIS_URL', 'redis://localhost:6379/5')
parsed = urlparse(REDIS_URL)
redis_client = redis.Redis(
@@ -73,6 +70,9 @@ redis_client = redis.Redis(
RUNS_KEY_PREFIX = "artdag:run:"
RECIPES_KEY_PREFIX = "artdag:recipe:"
# Initialize L1 cache manager with Redis for shared state between workers
cache_manager = L1CacheManager(cache_dir=CACHE_DIR, redis_client=redis_client)
def save_run(run: "RunStatus"):
"""Save run to Redis."""
@@ -4014,6 +4014,5 @@ async def ui_run_partial(run_id: str, request: Request):
if __name__ == "__main__":
import uvicorn
# Note: workers disabled because cache_manager uses in-memory state
# that isn't shared between worker processes
uvicorn.run("server:app", host="0.0.0.0", port=8100)
# Workers enabled - cache indexes shared via Redis
uvicorn.run("server:app", host="0.0.0.0", port=8100, workers=4)