# @momsfriendlydevco/express-middleware-cache

> Express caching middleware using Memory/MemcacheD/Mongo etc.

Latest version **1.1.14** (published 2022-05-23) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @momsfriendlydevco/express-middleware-cache
pnpm add @momsfriendlydevco/express-middleware-cache
yarn add @momsfriendlydevco/express-middleware-cache
bun add @momsfriendlydevco/express-middleware-cache
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.1.14 |
| Published | 2022-05-23 |
| First published | 2017-11-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 17.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Matt Carter |
| Maintainers | hash-bang, 1watt, eekthecat, melromero, mr-yellow |
| Keywords | expressjs, middleware, cache, caching, backend |

## Links

- npm: https://www.npmjs.com/package/@momsfriendlydevco/express-middleware-cache
- Repository: https://github.com/MomsFriendlyDevCo/express-middleware-cache
- Homepage: https://github.com/MomsFriendlyDevCo/express-middleware-cache#readme
- Issues: https://github.com/MomsFriendlyDevCo/express-middleware-cache/issues
- npm.io page: https://npm.io/package/@momsfriendlydevco/express-middleware-cache

## Dependencies (4)

- [argy](https://npm.io/package/argy.md) ^1.3.1
- [timestring](https://npm.io/package/timestring.md) ^5.0.1
- [async-chainable](https://npm.io/package/async-chainable.md) ^2.5.3
- [@momsfriendlydevco/cache](https://npm.io/package/@momsfriendlydevco/cache.md) ^1.4.4

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.1.14 (latest) — 2022-05-23
- 1.1.13 — 2019-01-24
- 1.1.12 — 2018-10-24
- 1.1.11 — 2018-09-28
- 1.1.10 — 2018-09-24
- 1.1.9 — 2018-09-20
- 1.1.8 — 2018-07-12
- 1.1.7 — 2018-06-28
- 1.1.6 — 2018-06-19
- 1.1.5 — 2018-06-08
- 1.1.4 — 2018-06-05
- 1.1.3 — 2018-05-31
- 1.1.2 — 2018-03-29
- 1.1.1 — 2018-03-18
- 1.1.0 — 2018-03-14
- … 3 more at https://npm.io/package/@momsfriendlydevco/express-middleware-cache/versions

## README

@MomsFriendlyDevCo/express-middleware-cache
===========================================
Express middleware for caching the result of a server action in-memory, MongoDB or MemcacheD.


Features:

* Easy to use syntax - Specify cache times in shorthand durations such as `app.get('/some/api', cache('1h'), (req, res) => ...)`
* Cache invalidation - Provide a tag for your cache setup and quickly invalidate all matching routes if necessary
* Etag support - Allows the client to make a request by the server generated hash and recieve either a code 200 (with data) or a 304 (Not Modified) response without actually making a full request
* Pluggable interface - Not just in-memory storage! Add any FIXME plugin to hold cache contents over server restarts



Simple example
--------------

```javascript
var cache = require('@momsfriendlydevco/route-cache');

// Setup the cache (configure which modules to try etc.)
await util.promisify(ache.setup)();

// This route should cache for about about 30 seconds
app.get('/some/api/hourly', cache('30s'), (req, res) => ...)

// This route should cache for about a minute and a half
app.get('/some/api/hourly', cache('1m30s'), (req, res) => ...)

// This route should cache for an hour
app.get('/some/api/hourly', cache('1h'), (req, res) => ...)
```


Cache invalidation
------------------
When setting up a cache specify `tag` / `tags` and invalidate against them when you want all calls to that route to be refreshed.

```javascript
// Make a route which only gets refreshed every hour

app.get('/some/api', cache('1h', {tag: 'myApi'}), (req, res) => ...)


// Later after some action which requires a cache reset:

cache.invalidate('myApi');
```


API
===

cache.defaults
--------------
Object containing any future caching objects options.

Supported options:

| Option           | Type               | Default                       | Description                                                                                       |
|------------------|--------------------|-------------------------------|---------------------------------------------------------------------------------------------------|
| `duration`       | `string`           | `1h`                          | Default duration to cache for                                                                     |
| `cache`          | `object`           | `{}`                          | Options passed to [@momsfriendlydevco/cache](https://github.com/MomsFriendlyDevCo/generic-cache) to setup a cache instance |
| `hashObject`     | `function`         | See internals                 | Method which returns the hashable object to use as the key in the cache. Defaults to hashing `req.{method,path,query,body}` |
| `tag`            | `string` / `array` | `''`                          | Optional tag or tags to associate with the cache. These can be used to invalidate the cache later. If any member is a function it is called as `(req, res)` and expected to return a string |
| `tags`           | `string` / `array` | `''`                          | Alias of `tag`                                                                                    |
| `etag`           | `boolean`          | `true`                        | Use eTag compatible caching with backend (only refresh when the server eTag doesn't match)        |
| `generateEtag`   | `function`         | See internals                 | Callback-style function used to generate an eTag value. Called as `(cb, hash, settings)`          |
| `subscribe`      | `boolean`          | `true`                        | Subscribe the returned EMC instance to the `emc.events` eventEmitter to react to gloabl events such as calls to `emc.invalidate()` |
| `tagStorePrefix` | `string`           | `"emc-tagstore"`              | Prefix to use when caching tagStore collections                                                   |
| `cacheQuery`     | `function`         | `(req, res, content) => true` | Replacable function used to determine if the request should be cached. Replacing this with `(req, res) => res.statusCode == 200` would only cache 200 codes for example |



cache(duration, options)
------------------------
The basic cache factory. This function returns Express middleware tuned to the duration and options specified.

Options extend `cache.defaults`.


cache.setup(options, [callback])
--------------------------------
Initial setup function. This must be called before the caching system can actually be used.

You can also pass in options that override the defaults.


cache.invalidate(...tags)
-------------------------
Reset the caching of any cache matching the given tag, array of tags or multiple tags.


cache.ready
-----------
Boolean indicating if `cache.setup()` has finished its processing.


cache.events
------------
An `EventEmitter` instance which can be bound to in order to retrieve events.

Events fired:

| Event                         | Called as     | Description                                                                              |
|-------------------------------|---------------|------------------------------------------------------------------------------------------|
| `routeCacheHit`               | `(req)`       | Fired when a route is requested that is handled by route-cache                           |
| `routeCacheHashError`         | `(err, req)`  | Fired when a route hashing system fails                                                  |
| `routeCacheEtag`              | `(req, info)` | The client requested the current hash via the `etag` header and will be served a 304 "Not Modified" response |
| `routeCacheExisting`          | `(req, info)` | Fired when a route is requested, a cached version exists and will be provided instead of recomputing the result |
| `routeCacheFresh`             | `(req, info)` | Fired when a route is requested, a valid cache does not exist and we need to compute the result |
| `routeCacheInvalidate`        | `(tag, hash)` | Fired when a single tag is invalidated                                                   |
| `routeCacheCacher`            | `(driver)`    | Emitted when the upstream Cache has loaded along with the driver ID that was used        |
| `routeCacheInvalidateRequest` | `(...tags)` | Fired by `emc.invalidate()` to all upstream EMC objects to tell them to invalidate the given tags |

The info object contains the following structure:

| Key       | Type      | Description                                                            |
|-----------|-----------|------------------------------------------------------------------------|
| `isFresh` | `boolean` | Whether the cache response was generated for this specific request     |
| `hash`    | `string`  | The internal, unqiue hashing value used to identify this cache session |

---
_Source: https://npm.io/package/@momsfriendlydevco/express-middleware-cache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
