fastify-lcache

fastify-lcache plugin for Fastify for memorize
data on first request and use it next time until ttl expires
npm i fastify-lcache
Table of Contents
Example
import fastify from "fastify";
import lcache from "fastify-lcache";
const app = fastify();
const address = "0.0.0.0";
const port = 4000;
app.register(lcache, {
ttlInMinutes: 10,
});
app.after(() => {
app.get("/ping", async (req, reply) => {
reply.send("pong");
});
});
app.listen(port, address);
const url = "http://0.0.0.0:4000/ping";
axios.get(url);
axios.get(url);
- ⚠️ IMPORTANT
- Restarting your app resets the cache
API
Options (default)
{
ttlInMinutes?: 5,
disableCache?: false,
statusesToCache?: [200],
methodsToCache?: ['GET'],
excludeRoutes?: [],
includeRoutes?: '*'
}
On fastify instance
app.lcache available inside your app
interface ILightCache {
get<T>(key: string): T;
set<T>(key: string, value: T): void;
has(key: string): boolean;
reset(key?: string): void;
destroy(): void;
}
Route caching
If a route matches any pattern in excludeRoutes
, it will never be cached.
Unspecified Routes
If a route is neither in includeRoutes
nor excludeRoutes
:
When includeRoutes
specifies particular routes, only those specified are cached,
while all other routes are ignored by the cache.
{
includeRoutes: '*',
excludeRoutes: ['/auth*', '/admin*']
}
{
includeRoutes: ['/api/users', '/api/orders']
excludeRoutes: ['/api/secret'],
}
Fastify version compatibility