goorecache

Mongoose Cache: Integrated Redis caching for Mongoose queries.

Usage no npm install needed!

<script type="module">
  import goorecache from 'https://cdn.skypack.dev/goorecache';
</script>

README

Goorecache

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 goorecache --save

Example


const mongoose = require('mongoose');
const Goorecache = require('goorecache');

Config mongoose-redis cache


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


var docs = await Post.find({ stt: 1 }).sort({ crt: -1 })
.limit(30)
.cache(120) // cache 120 seconds or 0 for keep it forever
.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);
    });