mongoose-redis

Mongoose Cache: Integrated Redis caching for Mongoose queries.

Usage no npm install needed!

<script type="module">
  import mongooseRedis from 'https://cdn.skypack.dev/mongoose-redis';
</script>

README

Mongoose Cache: Integrated Redis caching for Mongoose queries.

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

Installation

Install via NPM

npm install mongoose-redis --save

Example


var mongoose = require('mongoose');
var MongooseCache = require('mongoose-redis');

Config mongoose-redis cache


var cache = MongooseCache(mongoose, "redis://127.0.0.1:6379");


var docs = await Post.find({ stt: 1 }).sort({ crt: -1 })
.limit(30)
.cache(120) // cache 120 seconds
.select({ tl: 1, lth:1, views:1, img: 1, slug: 1})

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


app.get('/detail/:id', async (req, res)=> {
    var _id = req.params.id;  
    var info = await Post.findOne({ slug: _id, stt: 1 })
            .cache(120, _id); // custom cache key by id
                
});


Post.findById("5dbfac4d7d3ff31acb86d870").cache(60).then(result => {
        console.log(result);
    });


Post.findOne({ _id: '5dbfac4d7d3ff31acb86d870' }).cache(60).then(result => {
        console.log(result);
    });