Cache middleware
The Cache middleware intercepts function calls and caches their return values using a configurable cache store. When the wrapped function is invoked, the middleware derives a cache key from the function's arguments. If the key exists in the cache, the cached value is returned immediately without executing the function. Otherwise, the function runs, its result is stored in the cache, and the result is returned.
Usage
import { withCacheFactory } from "@daiso-tech/core/cache/middlewares";
import { Cache } from "@daiso-tech/core/cache";
import { use } from "@daiso-tech/core/middleware";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/adapter/memory-cache-adapter";
const cache = new Cache({
adapter: new MemoryCacheAdapter(),
});
const withCache = withCacheFactory(cache);
const fetchUser = async (userId: string): Promise<{ name: string }> => {
const response = await fetch(`/api/users/${userId}`);
return response.json();
};
// Wrap with caching
const cachedFetchUser = use(
fetchUser,
withCache({
key: (userId) => `user:${userId}`,
ttl: new TimeSpan("10m"), // Cache for 10 minutes
}),
);
const user = await cachedFetchUser("123"); // Cache miss — fetches and caches
const userAgain = await cachedFetchUser("123"); // Cache hit — returns immediately
info
Here is a complete list of settings for the withCache function.
Further information
For further information refer to @daiso-tech/core/cache API docs.