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

recachegoose

v10.0.1

Published

Mongoose caching that actually works.

Downloads

23,655

Readme

This is a fork version of cachegoose with following differences :

recachegoose

Mongoose caching that actually works.

NPM

npm version CircleCI Coverage Status Known Vulnerabilities License NPM download/month NPM download total

About

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

Important:
If you are using Mongoose 4.x or below, you have to use original cachegoose and use version <= 4.x of it.

Usage

  • Use In Memory
var mongoose = require('mongoose');
var cachegoose = require('recachegoose');

cachegoose(mongoose, {
  engine: 'memory'
});
  • Use File
var mongoose = require('mongoose');
var cachegoose = require('recachegoose');

cachegoose(mongoose, {
  engine: 'file'
});
  • Use Redis
var mongoose = require('mongoose');
var cachegoose = require('recachegoose');

cachegoose(mongoose, {
  engine: 'redis',
  port: 6379,
  host: 'localhost'
});

// or with redis url connection string
// redis[s]://[[username][:password]@][host][:port][/db-number]
cachegoose(mongoose, {
  engine: 'redis',
  url: 'redis://localhost:6379'
});

// or with redis client with connection string
// backwards compatibility
cachegoose(mongoose, {
  engine: 'redis',
  client: 'redis://localhost:6379'
});

// backwards compatibility
cachegoose(mongoose, {
  engine: 'redis',
  client: require('redis').createClient('redis://localhost:6379')
});
  • Set Cache
Record
  .find({ some_condition: true })
  .cache(30) // The number of seconds to cache the query.  Defaults to 60 seconds.
  .exec(function(err, records) { // You are able to use callback or promise
    ...
  });

Record
  .aggregate()
  .group({ total: { $sum: '$some_field' } })
  .cache(0) // Explicitly passing in 0 will cache the results indefinitely.
  .exec(function(err, aggResults) {
    ...
  });

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

var userId = '1234567890';

Children
  .find({ parentId: userId })
  .cache(0, userId + '-children') /* Will create a redis entry          */
  .exec(function(err, records) {  /* with the key '1234567890-children' */
    ...
  });

ChildrenSchema.post('save', function(child) {
  // Clear the parent's cache, since a new child has been added.
  cachegoose.clearCache(child.parentId + '-children');
});

Insert .cache() into the queries you want to cache, and they will be cached. Works with select, lean, sort, and anything else that will modify the results of a query.

Clearing the cache

If you want to clear the cache for a specific query, you must specify the cache key yourself:

function getChildrenByParentId(parentId, cb) {
  Children
    .find({ parentId })
    .cache(0, `${parentId}_children`)
    .exec(cb);
}

function clearChildrenByParentIdCache(parentId, cb) {
  cachegoose.clearCache(`${parentId}_children`, cb);
}

If you call cachegoose.clearCache(null, cb) without passing a cache key as the first parameter, the entire cache will be cleared for all queries.

Caching populated documents

When a document is returned from the cache, cachegoose will hydrate it, which initializes it's virtuals/methods. Hydrating a populated document will discard any populated fields (see Automattic/mongoose#4727). To cache populated documents without losing child documents, you must use .lean(), however if you do this you will not be able to use any virtuals/methods (it will be a plain object).

Test

For development mode, you have to use minimum nodejs 14

npm test