npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redis_mongoose

v1.0.2

Published

Solution for easy caching Mongoose queries with Redis

Downloads

7

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!