# cachegoose

> Mongoose caching that actually works.

Latest version **8.0.0** (published 2019-01-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install cachegoose
pnpm add cachegoose
yarn add cachegoose
bun add cachegoose
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 8.0.0 |
| Published | 2019-01-18 |
| First published | 2014-10-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 31 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Bob Lauer |
| Maintainers | boblauer |
| Keywords | cache, mongo, mongodb, mongoose, redis, store, ttl |

## Links

- npm: https://www.npmjs.com/package/cachegoose
- Repository: https://github.com/boblauer/cachegoose
- Issues: https://github.com/boblauer/cachegoose/issues
- npm.io page: https://npm.io/package/cachegoose

## Dependencies (5)

- [sha1](https://npm.io/package/sha1.md) ^1.1.1
- [jsosort](https://npm.io/package/jsosort.md) 0.0.0
- [cacheman](https://npm.io/package/cacheman.md) ^2.2.1
- [cacheman-redis](https://npm.io/package/cacheman-redis.md) ^1.1.2
- [brace-expansion](https://npm.io/package/brace-expansion.md) ^1.1.8

## 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

- 8.0.0 (latest) — 2019-01-18
- 7.1.0 — 2018-09-18
- 7.0.0 — 2018-04-12
- 6.0.1 — 2018-04-06
- 6.0.0 — 2018-04-05
- 5.0.0 — 2018-01-22
- 4.0.1 — 2017-11-12
- 4.0.0 — 2017-11-10
- 3.1.2 — 2017-11-10
- 3.0.2 — 2017-02-07
- 3.0.1 — 2016-08-18
- 3.0.0 — 2016-07-27
- 2.1.7 — 2015-12-22
- 2.1.6 — 2015-04-08
- 2.1.5 — 2015-03-30
- … 15 more at https://npm.io/package/cachegoose/versions

## README

# cachegoose #

#### Mongoose caching that actually works. ####

[![Build Status](https://travis-ci.org/boblauer/cachegoose.svg)](https://travis-ci.org/boblauer/cachegoose)

## About ##

A Mongoose caching module that works exactly how you would expect it to, with the latest version of Mongoose.

```
Important:

If you are using Mongoose 4.x or below, you need to use version 4.1 of this library.
```

## Usage ##

```javascript
var mongoose = require('mongoose');
var cachegoose = require('cachegoose');

cachegoose(mongoose, {
  engine: 'redis',    /* If you don't specify the redis engine,      */
  port: 6379,         /* the query results will be cached in memory. */
  host: 'localhost'
});

Record
  .find({ some_condition: true })
  .cache(30) // The number of seconds to cache the query.  Defaults to 60 seconds.
  .exec(function(err, records) {
    ...
  });

Record
  .aggregate()
  .group({ total: { $sum: '$some_field' } })
  .cache(0) // Explicitly passing in 0 will cache the results indefinitely.
  .exec(function(err, aggResults) {
    ...
  });
```

You can also pass a custom key into the `.cache()` method, which you can then use later to clear the cached content.

```javascript
var mongoose = require('mongoose');
var cachegoose = require('cachegoose');

cachegoose(mongoose, {
  engine: 'redis',
  port: 6379,
  host: 'localhost'
});

var userId = '1234567890';

Children
  .find({ parentId: userId })
  .cache(0, userId + '-children') /* Will create a redis entry          */
  .exec(function(err, records) {  /* with the key '1234567890-children' */
    ...
  });

ChildrenSchema.post('save', function(child) {
  // Clear the parent's cache, since a new child has been added.
  cachegoose.clearCache(child.parentId + '-children');
});
```

Insert `.cache()` into the queries you want to cache, and they will be cached.  Works with `select`, `lean`, `sort`, and anything else that will modify the results of a query.

## Clearing the cache ##

If you want to clear the cache for a specific query, you must specify the cache key yourself:

```js
function getChildrenByParentId(parentId, cb) {
  Children
    .find({ parentId })
    .cache(0, `${parentId}_children`)
    .exec(cb);
}

function clearChildrenByParentIdCache(parentId, cb) {
  cachegoose.clearCache(`${parentId}_children`, cb);
}
```

If you call `cachegoose.clearCache(null, cb)` without passing a cache key as the first parameter, the entire cache will be cleared for all queries.

## Caching populated documents ##

When a document is returned from the cache, cachegoose will [hydrate](http://mongoosejs.com/docs/api.html#model_Model.hydrate) it, which initializes it's virtuals/methods. Hydrating a populated document will discard any populated fields (see [Automattic/mongoose#4727](https://github.com/Automattic/mongoose/issues/4727)). To cache populated documents without losing child documents, you must use `.lean()`, however if you do this you will not be able to use any virtuals/methods (it will be a plain object).

## Test ##
npm test

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