Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
cachegoose
Advanced tools
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 need to use version 4.1 of this library.
var mongoose = require('mongoose');
var cachegoose = require('cachegoose');
cachegoose(mongoose, {
engine: 'redis', /* If you don't specify the redis engine, */
port: 6379, /* the query results will be cached in memory. */
host: 'localhost'
});
Record
.find({ some_condition: true })
.cache(30) // The number of seconds to cache the query. Defaults to 60 seconds.
.exec(function(err, records) {
...
});
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 mongoose = require('mongoose');
var cachegoose = require('cachegoose');
cachegoose(mongoose, {
engine: 'redis',
port: 6379,
host: 'localhost'
});
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.
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.
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).
npm test
FAQs
Mongoose caching that actually works.
The npm package cachegoose receives a total of 1,830 weekly downloads. As such, cachegoose popularity was classified as popular.
We found that cachegoose demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.