Skip to content

efeiefei/mongoose-with-redis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project borrowed from mongoose-redis-cache. The difference is that this project using javascript alternative coffeescript, support custom redis client, and change some internal logics and using methods.

Installation

`npm install mongoose-with-redis

Example Usage

var mongoose = require('mongoose');
var redis = require('ioredis');
var MongooseRedis = require('mongoose-with-redis');
// custom schema and model as usual
var userSchama = new mongoose.Schema({
  name: String,
  age: Number,
});
User = mongoose.model('User', userSchama);

var redisClient = redis.createClient();
mongoose.connect('mongodb://localhost/mongoose-redis-test');

// The follwing is default config if you pass no cacheOptions.
var cacheOptions = {
  cache: true,
  expires: 60,
  prefix: 'RedisCache'
};
MongooseRedis(mongoose, redisClient, cacheOptions);

// Works with lean() and exec(), the same query will use redis cache next time.
var user = yield User.findOne({name: 'xxx'}).lean().exec();
// It won't work when lack lean() or exec().
var user = yield User.findOne({name: 'xxx'}).exec();
// We will also set cacheOption in each query statement
// It won't affect other queries.
var user = yield User.findOne({name: 'xxx'})
    .setOptions({cacheOptions: {cache: false}})
    .lean().exec();

About

Cache your Mongoose MongoDB query results with Redis

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published