middleware-cache

    0.0.1 • Public • Published

    NPM version Build Status Coverage Status

    middleware-cache

    $ npm install --save middleware-cache

    Overview

    The middleware cache is a higher order component that stores the results of an express middleware function in a repository so that repetitive long running calls can be made more efficient.

    For examples of repositories you can use, checkout this repo. Node Repositories

    Most likely you will want to use an in-memory cache or a redis cache. Caching in something like mongo will require you to invalidate the cache on your own.

    Options

    • key: string or hash function to determine where to store in db. note that this should be namespaced for the middleware type.
    • selector: function to pick the resulting cacheable data out of the ORIGINAL middleware result.

    Usage

     
    // pass the data store into the cache function
    const cache = require('middleware-cache')(repo);
     
    const middleware = (req, res, next) => {
      res.locals.complete = true;
      setTimeout(next, 500);
    };
     
    const options = {
      key: (req, res) => `orderHistory|${req.user._id}`,
      expire: 1000 // the repository implements this, so for redis it is in seconds
    };
     
    const cachedMiddleware = cache('orderHistory', middleware, options);
     
    app.use(cachedMiddleware);
    app.get('/foo', cachedMiddleware, (req, res) => res.send({}));
     

    Prior Art

    leahciMic/cache-middleware

    Install

    npm i middleware-cache

    DownloadsWeekly Downloads

    44

    Version

    0.0.1

    License

    none

    Last publish

    Collaborators

    • avatar