1.0.3 • Published 7 years ago

@kevroadrunner/express-cache v1.0.3

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

@kevroadrunner/express-cache

Build Status JavaScript Style Guide Maintainability Test Coverage Renovate enabled

Caching middleware for express apps.

Be aware that the request and response type must be the same.

Installation

$ npm install @kevroadrunner/express-cache

Usage

Default/Zero Configuration

Express cache with InMemory storage. Caches the response of json responses.

const express = require('express')
const cache = require('@kevroadrunner/express-cache/json')

const app = express()
app.use(cache())

Custom Configuration

Express cache with SQLite storage. Caches the response of all GET requests matching /api/people.

const express = require('express')
const cache = require('@kevroadrunner/express-cache/json')

const app = express()
app.get('/api/people', cache('//sqlite:store.sqlite'))

More storage adapters can be found in the Keyv#usage documentation.

To customize request routes see the Express#req documentation.

Storage

The Store inherits from Keyv.

Further it provides an has(key) method. This method is similar to Map.prototype.has().

Express App Example

const {get} = require('axios')
const express = require('express')

const cache = require('@kevroadrunner/express-cache/json')

express()
  .use(cache())
  .get('/people', async (req, res, next) => {
    try {
      const result = await get('http://swapi.co/api/people')
      res.json(result.data)
    } catch (error) {
      console.error(error)
      next()
    }
  })
  .use((req, res) => res.send('OK'))
  .listen(5000)