Registry / database / mongoose-redis

mongoose-redis

JSON →
library1.1.1jsnpmunverified

Mongoose Cache is a lightweight module that adds integrated Redis caching to Mongoose queries for Node.js. Version 1.1.1 is the latest stable release; the package is in maintenance mode with irregular updates. It wraps Mongoose query executors to cache results in Redis using a simple .cache(ttlSeconds, customKey) method. Unlike more complex caching solutions (e.g., cachegoose), it aims for minimal configuration and direct Redis URL connection. It supports Mongoose 5.x+ and works with both callback and async/await patterns. Key differentiator: dead-simple API—just call .cache() on any query chain. No support for cache invalidation by pattern or automatic expiration of stale keys beyond TTL.

npm install mongoose-redis
INSTALL
IMPORT
SIG · MONGOOSE-REDIS
M
mongoose-redis
databasejavascriptv1.1.1
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.

MongooseCache (default)
import MongooseCache from 'mongoose-redis'
const { MongooseCache } = require('mongoose-redis')
Use default import. CommonJS users: const MongooseCache = require('mongoose-redis').
Single function (initializer)
MongooseCache(mongoose, 'redis://127.0.0.1:6379')
new MongooseCache(mongoose, 'redis://127.0.0.1:6379')
MongooseCache is not a constructor; call it as a function to attach caching capabilities to Mongoose queries.
Query.cache()
const docs = await Model.find().cache(120);
Model.find().cache({ ttl: 120 })
cache() accepts a number (TTL in seconds) and an optional custom key string. Passing an object will not work.

Connects to MongoDB and Redis, attaches caching to Mongoose, then runs a cached find query with 60-second TTL.

import mongoose from 'mongoose'; import MongooseCache from 'mongoose-redis'; // Connect to MongoDB await mongoose.connect('mongodb://localhost:27017/test'); // Attach cache MongooseCache(mongoose, process.env.REDIS_URL || 'redis://127.0.0.1:6379'); // Define a schema const PostSchema = new mongoose.Schema({ title: String, content: String }); const Post = mongoose.model('Post', PostSchema); // Use cache in a query const cachedPosts = await Post.find({ status: 'published' }) .sort({ createdAt: -1 }) .limit(10) .cache(60); // cache for 60 seconds console.log(cachedPosts);
Debug
Known issues
gotchaThe package does not automatically clear the cache when documents are updated or deleted. Stale data may be served until TTL expires.
fix
Manually clear cache keys using a custom key pattern and redisClient.del() after write operations.
affects: all
gotchaIf the Redis connection fails silently, queries will still work but will not be cached. No error is thrown by default.
fix
Monitor Redis connection events (connect, error) and set up fallback logging.
affects: all
breakingVersion 1.x only supports Mongoose 5.x and 6.x. Mongoose 7+ may cause unexpected behavior due to query execution engine changes.
fix
Pin Mongoose to version 5.x or 6.x, or switch to a more maintained caching solution.
affects: >=1.0.0
deprecatedThe package uses the `redis` npm package (v3+), which is deprecated in favor of `@redis/client`. This may cause compatibility issues with newer Redis versions.
fix
Consider using `@redis/client` directly and rewriting the caching layer, or migrate to a maintained fork.
affects: >=1.0.0
gotchaCalling .cache() on a query with a lean() or populate() may not produce expected results—cached data might not include all transformations.
fix
Always call .cache() after all other chain methods and before await/exec.
affects: all
Errors
Common errors & fixes
MongooseCache is not a constructor
Using new MongooseCache() instead of calling it as a function.
fix
Call MongooseCache(mongoose, redisUrl) without new.
Cannot read properties of undefined (reading 'cache')
MongooseCache was not initialized before running a query, or the model was created before initialization.
fix
Ensure MongooseCache(mongoose, url) is called after mongoose.connect() and before defining models.
TypeError: this.cache is not a function
The query was not executed with .cache() because the package was not properly attached.
fix
Double-check that MongooseCache was called with the correct mongoose instance and Redis URL.
redis connect ECONNREFUSED 127.0.0.1:6379
Redis server is not running or not accessible at the configured URL.
fix
Start a Redis server (e.g., redis-server) or update the connection URL (set REDIS_URL environment variable).
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies
mongooserequiredPeer dependency – the package caches Mongoose queries and must be used with an existing Mongoose instance.
redisrequiredRuntime dependency – required to connect to Redis for storing/retrieving cache entries.
Agent activity
10 hits · last 30 days
node
8
Resources
mongoose-redis — npm install mongoose-redis · libregistry