0.0.4 • Published 4 years ago

express-route-cache v0.0.4

Weekly downloads
7
License
MIT
Repository
github
Last release
4 years ago

Express Route Cache

Current latest version Downloads per week Libraries.io dependency status for latest release GitHub issues

Express middleware that caches your routes. This project is still under development and not stable, updates might contain breaking changes. Currently only works on methods res.send and res.json.

Installation

  • npm install express-route-cache
  • yarn add express-route-cache

Quick setup

import express from "express";
import ExpressRouteCache from "express-route-cache";

const app = express();

const defaultTtl = 60 * 5;
const erc = new ExpressRouteCache(defaultTtl);

app.get("/default-ttl", erc.cache(), (req, res) => {
  res.send("This route is now cached for 5 minutes");
});

app.get("/specified-ttl", erc.cache(30), (req, res) => {
  res.send("This route is now cached for 30 seconds");
});

API description

class ExpressRouteCache {
  // Default TTL is 60 seconds
  constructor(defaultTtlSeconds: number);

  // Primary function used for caching routes.
  // TTL is set to defaultTtlSeconds if not specified.
  // req.originalUrl (path + query params) is used as the cache key
  cache(ttlSeconds: number): NodeMiddlewareFn;

  // Use to see if cache contains cachekey
  async has(cacheKey: string): Promise<Boolean>;

  // Use to delete a certain cachekey
  async del(cacheKey: string): Promise<Boolean>;

  // Use to flush entire cache
  async flush(): Promise<void>;
}