@irrelon/cache-express
TypeScript icon, indicating that this package has built-in type declarations

5.0.0 • Public • Published

Express Caching Middleware

Add cache support to your Express application. Supports in-memory and Redis-based caching out of the box.

Install

This module has a peer dependency of @irrelon/emitter which is also installed by this command:

npm i @irrelon/cache-express @irrelon/emitter

Usage

Add the expressCache() function before your route handler function.

In Memory Cache

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!");
	}
);

Redis Cache

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!");
	}
);

Options

See the ExpressCacheOptions.ts type which describes the options available.

Example Usage

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");
});

Examples

  1. 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");
    });
  2. Custom Timeout and Callback:

    app.get(
    	"/api/data",
    	expressCache({
    		timeOutMins: 1, // Cache for 1 minute
    	})
    );

License

This project is licensed under the MIT License.

Package Sidebar

Install

npm i @irrelon/cache-express

Weekly Downloads

40

Version

5.0.0

License

MIT

Unpacked Size

51.3 kB

Total Files

8

Last publish

Collaborators

  • coolbloke1324