Add cache support to your Express application. Supports in-memory and Redis-based caching out of the box.
This module has a peer dependency of
@irrelon/emitter
which is also installed by this command:
npm i @irrelon/cache-express @irrelon/emitter
Add the expressCache()
function before your route handler function.
import express from "express";
import {expressCache, MemoryCache} from "@irrelon/cache-express";
const app = express();
const inMemoryCache = new MemoryCache();
// Apply the caching middleware to a route
app.get(
"/api/data",
expressCache({
cache: inMemoryCache
/*options*/
}),
// Your route handler function
(req, res, next) => {
res.send("hello!");
}
);
import {createClient} from "redis";
import express from "express";
import {expressCache, RedisCache} from "@irrelon/cache-express";
const redisClient = createClient({url: "redis://localhost:6380"});
const app = express();
const redisCache = new RedisCache({client: redisClient});
// Apply the caching middleware to a route
app.get(
"/api/data",
expressCache({
cache: redisCache
/*options*/
}),
// Your route handler function
(req, res, next) => {
res.send("hello!");
}
);
See the ExpressCacheOptions.ts type which describes the options available.
import express from "express";
import expressCache from "cache-express";
const app = express();
// Apply caching middleware with custom options
app.get(
"/api/data",
expressCache({
timeOutMins: 1, // Cache for 1 minute
}),
(req, res) => {
// time consuming api or database calls
let data = { success: true };
res.json(data);
}
);
// Or you can create a middleWare configuration beforehand:
let postsCache = expressCache({
timeOutMins: 1,
});
// Then use it in route.
app.get("/api/posts", postsCache, (req, res) => {
//...
res.send("");
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});
-
Basic Usage:
import express from "express"; import expressCache from "cache-express"; const app = express(); app.get("/api/data", expressCache()); app.listen(3000, () => { console.log("Server is running on port 3000"); });
-
Custom Timeout and Callback:
app.get( "/api/data", expressCache({ timeOutMins: 1, // Cache for 1 minute }) );
This project is licensed under the MIT License.