In traditional projects, we often use caching to optimize database reads, such as in Java, where we take advantage of Spring's AOP capabilities to increase caching operations before reading and writing databases.
Similar problems still exist in node and MongoDB projects, this article refers to the Mongoose-redis-cache plugin.
Https://github.com/conancat/mongoose-redis-cache
The plugin is not perfect, but the basic idea is simple, initialize a redis client, then rewrite the Exec method of mongoose, set the exec parameter to Redis key, and set the result returned by the database to the corresponding value.
The Redis is read first on each operation.
The code is as follows:
//Generated by Coffeescript 1.5.0varMongooserediscache, Redis, _;redis= Require ("Redis");_ = Require ("Underscore"); Mongooserediscache=function(Mongoose, Options, callback) {varclient, host, pass, Port, redisoptions; if(Options = =NULL) {Options= {}; } Host= Options.host | | ""; Port= Options.port | | ""; Pass= Options.pass | | ""; Redisoptions= Options.options | | {}; Mongoose.redisclient= Client =Redis.createclient (port, host, redisoptions); if(Pass.length > 0) {Client.auth (pass,function(err) {if(callback) {returncallback (ERR); } }); }
//Changed here, the original is Execfind, in my current Mongoose version can not use mongoose. Query.prototype._exec=Mongoose. Query.prototype.exec; Mongoose. Query.prototype.exec=function(callback) {varCB, expires, fields, key, model, query, schemaoptions, self; Self= This; Model= This. Model; Query= This. _conditions; Options= This. _optionsforexec (model); fields= _.clone ( This. _fields); Schemaoptions=model.schema.options; Expires= Schemaoptions.expires | | 60; if(!schemaoptions.rediscache &&Options.lean) {returnMongoose. Query.prototype._exec.apply (self, arguments); } Key= json.stringify (query) + json.stringify (options) +json.stringify (fields); CB=function(err, result) {vardocs; if(err) {returncallback (ERR); } if(!result) { returnMongoose. Query.prototype._exec.call (Self,function(Err, docs) {varstr; if(err) {returncallback (ERR); } STR=json.stringify (Docs); Client.set (key, str); Client.expire (key, expires); returnCallbackNULL, Docs); }); } Else{docs=json.parse (Result); returnCallbackNULL, Docs); } }; Client.get (key, CB); return This; };}; Module.exports= Mongooserediscache;
Write the test case as follows:
varMongoose = require (' Mongoose '));varAsync = require (' async ');varMongooserediscache = require ("Mongoose-redis-cache"); Mongooserediscache (mongoose);varSchema =Mongoose. Schema;mongoose.connect (' Mongodb://localhost/cachetest ');varuser =NewSchema ({username:{type:string, index:true}, password:string}); User.set (' Rediscache ',true);varUsermodel = Mongoose.model (' user ', user);varentity =NewUsermodel ({username:' Fredric ', Password:' Sinny '});varUsers = []; for(vari = 0; i < 1000; i++) {Users.push ({username:' Fredric ' +i, Password:' Sinny ' +i});}functionTestcache () {functionDatainit (ITEM,CB) {varentity =NewUsermodel (item); Entity.save (function(ERR) {CB (ERR); }); } async.mapseries (Users, Datainit,function(err, results) {Console.log (' Datainit finished '); vartimestamp =NewDate (). ValueOf (); varRound = []; for(vari = 0; I < 2000; i++) {Round.push (i); } //Leveraging caching functionTest (ITEM,CB) {query= Usermodel.find ({' username ': ' fredric101 ')}); Query.lean (); Query.exec (function(err, result) {CB (ERR); }); } //do not use cache functionTest_nocache (ITEM,CB) {query= Usermodel.find ({}). setoptions ({nocache:true}); Query.where ("username", "fredric101"); Query.exec (function(err, result) {CB (ERR); }); } async.mapseries (Round, Test_nocache,function(err, results) {Console.log (NewDate (). ValueOf ()-timestamp); }); }); }testcache ();
The test result is still more obvious, on my local notebook (installing Redis + MongoDB), the above test case execution:
1, no cache, no index: 21501ms
2, no cache, indexed: 1966ms
3, has the cache, has the index: 281ms
MongoDB (Mongoose-redis-cache)