Registry / database / mongoose-redis-caching

mongoose-redis-caching

JSON →
library1.13.2jsnpmunverified

A Mongoose plugin that adds Redis caching to Mongoose queries. Version 1.13.2 is the latest stable release. It provides a `.cache()` chainable method on query results, with optional TTL in seconds (default 60). Works by wrapping Mongoose's `exec` method and caching query results in Redis. Differs from other caching libraries by its simplicity and direct integration with Mongoose without additional configuration.

npm install mongoose-redis-caching
INSTALL
IMPORT
SIG · MONGOOSE-REDIS-CAC
M
mongoose-redis-caching
databasejavascriptv1.13.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default import
const mongooseRedisCaching = require('mongoose-redis-caching');
import mongooseRedisCaching from 'mongoose-redis-caching';
The package does not export an ES module; use CommonJS require.

Demonstrates requiring the module, initializing it with Mongoose, and using .cache() on a query with a 30-second TTL.

const mongoose = require('mongoose'); const mongooseRedisCaching = require('mongoose-redis-caching'); mongooseRedisCaching(mongoose); const userSchema = new mongoose.Schema({ name: String }); const User = mongoose.model('User', userSchema); async function getUsers() { const users = await User.find().cache(30); console.log(users); } getUsers().catch(console.error);
Debug
Known issues
gotchaPackage is ESM-only from v2 onwards? Current version uses CommonJS only; no ES module provided.
fix
Use require() instead of import. If you must use ES modules, consider wrapping or using dynamic import.
affects: >=1.0.0
gotchaRequires an active Redis connection. If Redis is not available, queries will fail silently or throw errors.
fix
Ensure Redis is running and accessible at the configured URL before executing cached queries.
affects: >=1.0.0
gotchaThe .cache() method must be called on a Mongoose Query object (e.g., after .find() but before await). Calling it on a non-query will throw.
fix
Always chain .cache() on a query, like Model.find().cache().
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: mongooseRedisCaching is not a function
Mistakenly using ES import syntax with a CommonJS-only package.
fix
Replace 'import mongooseRedisCaching from 'mongoose-redis-caching'' with 'const mongooseRedisCaching = require('mongoose-redis-caching')'.
MongooseError: Query was already executed
Calling .cache() after awaiting the query, or using .cache() on an already executed query.
fix
Ensure .cache() is chained before await, like Model.find().cache().
Upgrade
Version history
1.13.2latest on npm
Audit
Dependencies
mongooserequiredPeer dependency; the plugin wraps Mongoose queries.
redisrequiredRequired to connect to a Redis instance for caching.
Agent activity
10 hits · last 30 days
node
8
Resources
mongoose-redis-caching — npm install mongoose-redis-caching · libregistry