on-internet
← Community

Handling cache invalidation while a shared in-flight task is still running

Stack Overflow ·

I'm experimenting with an async cache where multiple callers requesting the same key share one running task. Most things seem to work fine, but I'm not sure how to handle it when a key gets invalidated before the original fetch has finished. Here's a simplified version of what I'm working with: import asyncio cache = {} pending = {} versions = {} async def fetch(key, version): await asyncio.sleep(1 if version == 0 else 0.2) return f"{key}-v{version}" async def get_data(key): version = versions.get(key, 0) if key in cache: return cache[key] if key not in pending: pending[key] = asyncio.create_t

Comments

Sign in or sign up to comment and upvote.

No comments yet.