ocache is a JavaScript/TypeScript library providing robust caching utilities for functions and HTTP handlers. It supports common caching strategies such as time-to-live (TTL), stale-while-revalidate (SWR), and multi-tier caching. Currently at version 0.1.4, it is under active development by the unjs organization, suggesting a focus on modularity and integration within their ecosystem, although it is designed to be standalone. Key differentiators include built-in request deduplication for cached functions, automatic HTTP response caching with `etag` and `last-modified` support, and a flexible multi-tier caching mechanism that allows for layered cache storage. The project appears to follow a rapid release cadence for minor enhancements and performance improvements, typical for libraries in their early 0.x development phase, with type definitions included for TypeScript users.
npm install ocacheVerified import paths — ran on the pinned version, not inferred.
This example demonstrates defining a cached function with TTL and stale-while-revalidate, showing how it deduplicates requests, serves cached data, and can be explicitly invalidated.
Always review the changelog when updating any `0.x` version of ocache to understand potential API or behavioral changes.
If providing a custom `getKey` option, test it thoroughly to ensure it produces the expected cache keys for all relevant inputs. If not provided, the default key generation (based on function name and stringified arguments) should be sufficient for most primitive arguments.
Review the usage of `staleMaxAge` in your caching options. If you previously had custom logic for stale content, consider whether `ocache`'s built-in `staleMaxAge` now covers your needs.
Understand the read/write behavior of multi-tier caching. Ensure all tiers are robust and capable of handling write operations. Monitor performance for write-heavy workloads with many tiers.
Verify that your server environment correctly handles standard Web Fetch API `Request` and `Response` objects. Test caching behavior extensively in your target deployment environment, especially for header-based optimizations like `etag` and `last-modified`.
Ensure that the environment in which `defineCachedHandler` is used (e.g., a serverless function context) provides an event object compatible with standard `Request` objects, specifically `event.req.headers`. You might need to adapt your handler to parse headers from a different property or format.
Check your `getKey` function (if custom) for consistency. Increase `maxAge` for testing. Verify `shouldBypassCache` option to ensure it's not unintentionally skipping the cache. Also, ensure that the cache storage is correctly configured and accessible.
Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) and use `import { defineCachedFunction } from 'ocache';`. If you must use CommonJS, consider transpilation or using a bundler that handles ESM conversion.No dependency data recorded yet.