Cache expensive action results like AI API calls in Convex with configurable TTL to reduce latency and API costs.
npm install @convex-dev/action-cacheSometimes your app needs to fetch information from a third-party API that is slow or costs money. Caching can help! This is a Convex component that can cache the results of expensive functions and set an optional TTL. Expired entries are cleaned up via a cron job once a day. The cache key is the ActionCache's name (defaults to function name) and the arguments to the action that generates the cache values.
To invalidate the cache, you can set a new name explicitly and/or clear cached values by name.
Action Cache wraps your expensive AI calls with automatic caching based on input parameters. Results are stored with configurable TTL, so identical requests return instantly from cache instead of hitting the AI API again.
Use Action Cache to store OpenAI responses and avoid duplicate API calls for the same inputs. The component automatically handles cache keys and expiration, significantly reducing your AI API usage and costs.
Action Cache stores results from external APIs like payment processors or data services. Configure cache duration based on how frequently the external data changes to balance freshness with performance.
Action Cache uses input parameters as cache keys and respects the TTL you configure. If cached data exists and hasn't expired, it returns immediately. Otherwise, it executes your action and caches the new result for subsequent calls.
Yes, Action Cache provides methods to manually clear specific cache entries or entire cache namespaces. This is useful when you know external data has changed and want to force fresh API calls.
Action Cache only stores successful results. If your action throws an error, the error propagates normally and no cache entry is created, ensuring failed operations don't prevent retries.
Action Cache uses Convex's database for storage, so it's limited by your Convex plan's database size. The component is designed for caching API responses and computed results, not large files or datasets.