mongoose-redis

1.1.1 • Public • Published

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);
    });
 

Package Sidebar

Install

npm i mongoose-redis

Weekly Downloads

50

Version

1.1.1

License

MIT

Unpacked Size

4.95 kB

Total Files

5

Last publish

Collaborators

  • nasa8x