# redis_mongoose

> Solution for easy caching Mongoose queries with Redis

Latest version **1.0.2** (published 2020-05-26) · MIT license · 0 weekly downloads

## Install

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

## 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 | 1.0.2 |
| Published | 2020-05-26 |
| First published | 2020-05-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 7.1 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| Author | Fedor Sorokin |
| Maintainers | fedorpro |
| Keywords | redis, cache, cached, caching, performance, mongodb cache, mongodb, redis mongoose, redis mongodb, redis mongo, mongoose cache, mongodb cache, mongoose redis, mongoose, redis cache, mongo cache, node cache, nosql |

## Links

- npm: https://www.npmjs.com/package/redis_mongoose
- npm.io page: https://npm.io/package/redis_mongoose

## Dependencies (2)

- [redis](https://npm.io/package/redis.md) ^3.0.2
- [mongoose](https://npm.io/package/mongoose.md) ^5.9.13

## 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.0.2 (latest) — 2020-05-26
- 1.0.1 — 2020-05-26
- 1.0.0 — 2020-05-25

## README

# What's this
This is pretty simple tool that helps you with caching mongoose queries to mongoDB.

It exsposes
```
.cache(options?)
```
method for all mongoose queries

and also adds separate method for clearing cache
```
clearCache(cacheGroup?)
```

# Usage

## Install
```
npm i --save redis_mongoose
```

## Initialize

```
const redisMongoose = require('redis_mongoose');
```
In your main file (usualy src/index.js)
Usually you already have instance of Mongoose imported in this file, if not, add it with
```
const mongoose = require('mongoose');
```

Now execute init function, passing in Mongoose instance and optionally Redis adress, by default local redis runs on 'redis://127.0.0.1:6379', this value will be used by default.

```
redisMongoose.init(mongoose);
```
Thats all. Now you can just add
```
.cache(options?)
```
to your queries and the app will do its job.

### Example
I have some api, it assepts user.id, looks in database for that user's blogs and returns the result.
Insted of punching DB with the exact same request I can do just one request to DB, cache it and then return cached value for every same request.

```
app.get('/api/blogs', requireLogin, async (req, res) => {
        const blogs = await Blog.find({ _user: req.user.id }).cache();

        res.send(blogs);
    });
```

In this case the cache will be stored forever, so in order to keep things working it is crucual to clean cache manualy in some cases. In this case I must clear chashe when new blog is posted, so I will do it in respected api controller.
```
const { clearCache } = require('redis_mongoose');
app.post('/api/blogs', async (req, res) => {
        <!-- some work on putting stuff to db, validatind etc -->
        clearCache();
        <!-- clear cach afterwards -->
    });

```

# Options
There are some options to help you fine-tune your caching strategy

```
.cache({
    customKey?,
    cacheGroup?,
    ttl?,
  })
```
*cacheKey* is key for field, can be empty so uniq hash from queryAnd CollectionName will be made

                  *REDIS STORE*
              |__________|__________|
              |__________|__________|
              |_cacheKey_|_{result}_|
              |__________|__________|

*cacheGroupKey* allows you to group fieds

                  *REDIS STORE*

              |__default__|
                          |___....___|___....___|
                          |___....___|___....___|
                          |_cacheKey_|_{result}_|
                          |___....___|___....___|
              |___user1___|
                          |___....___|___....___|
                          |__blogs___|_{result}_|
                          |__tweets__|_{result}_|
                          |___x40a___|_{result}_|

*ttl* is how long in seconds cache for group will live, by default it will live forever

                  *REDIS STORE*
                    (ttl=60)

              |__default__| <-- will be deleted after 60 seconds
                          |___....___|___....___|
                          |___....___|___....___|
                          |_cacheKey_|_{result}_|
                          |___....___|___....___|

# STAR and Contribute!
Visit my github, give me a star!
<https://github.com/fritzlolpro/redis_mongoose>

Also if u have any questions or proporsals -- you know what to do!

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