{"_id":"mongoose-redis-cache","_rev":"32-29646ddc634b08b292d44938dd7fc287","name":"mongoose-redis-cache","description":"Plugin to cache Mongoose MongoDB query results in Redis","dist-tags":{"latest":"0.0.3"},"versions":{"0.0.1":{"name":"mongoose-redis-cache","version":"0.0.1","description":"Plugin to cache Mongoose MongoDB query results in Redis","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"license":"BSD","directories":{"test":"test"},"dependencies":{"redis":"~0.8.2","underscore":"~1.4.4"},"devDependencies":{"mocha":"~1.8.1","async":"~0.2.5","mongoose":"~3.5.7"},"repository":{"type":"git","url":"https://github.com/conancat/mongoose-redis-cache.git"},"keywords":["mongodb","mongoose","redis","cache"],"author":{"name":"Grey Ang"},"readme":"# mongoose-redis-cache\n\nPlugin to cache Mongoose MongoDB query results in Redis. Lean DB queries, at least 300% faster! \n\n## How to use\nFirst, the usual: \n\n    npm install mongoose-redis-cache\n\nThen, \n\n    Setup mongoose connect as usual: \n\n       var mongoose = require(\"mongoose\");\n       var mongooseRedisCache = require(\"mongoose-redis-cache\");\n       mongoose.connect(\"mongodb://localhost/mongoose-redis-test\")\n\n    Create your schemas as usual: \n\n       var ExampleSchema = new Schema(function(){\n          field1: String\n          field2: Number\n          field3: Date\n       });\n     \n    Enable redisCache on the schema! \n       \n       REQUIRED: Enable Redis caching on this schema by specifying\n\n           ExampleSchema.set('redisCache', true)\n\n       OPTIONAL: Change the time for the cache of this schema. Defaults to 60 seconds. \n     \n           ExampleSchema.set('expires', 30)\n\n    Register the schema as usual: \n         \n         Example = mongoose.model('Example', ExampleSchema)\n\n    Setup your mongooseCache options:\n\n        # If you're running this locally, \n        mongooseRedisCache(mongoose)\n\n        # Or if you're running a remote Redis DB\n        mongooseRedisCache(mongoose, {\n           host: \"redisHost\",\n           port: \"redisPort\",\n           pass: \"redisPass\",\n           options: \"redisOptions\"\n         })\n     \n    Make a query as usual:\n         \n        query = Example.find({}) \n        query.where(\"field1\", \"foo\")\n        query.where(\"field2\").gte(30)\n        query.lean()  mongooseRedisCache only works for query.lean() queries!\n        query.exec(function(err, result){\n            Do whatever here! \n        });\n\nCheck out the test example for more information. \n\n## Test results: \n\n    =========================\n    Mongoose-Redis Cache Test\n    =========================\n    Total items in DB: 30000\n    Total number of queries per round: 20\n    Total number of rounds: 30\n\n    Generating 30000 mocks...\n\n    --------------------------------\n    Test query without Redis caching\n    --------------------------------\n    Begin executing queries without caching\n    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․\n\n    Total time for 30 test rounds: 12620ms\n    Average time for each round: 420.67ms\n\n    --------------------------------\n    Test query with Redis caching\n    --------------------------------\n    Begin executing queries with Redis caching\n    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․\n\n    Total time for 30 test rounds: 3618ms\n    Average time for each round: 120.60ms\n    ------------\n    CONCLUSION\n    ------------\n    Caching with Redis makes Mongoose lean queries faster by 9002 ms.\n    That's 348.81% faster!  \n\n\n    End tests. \n    Wiping DB and exiting\n\n## How this works\n\n### What is a Mongoose `lean` query? \n\n[Mongoose](http://mongoosejs.com), the MongoDB ODM for NodeJS has an awesome feature which casts each document \nas a Mongoose [model](http://mongoosejs.com/docs/models.html). This allows awesome features like being able to call `model.save()` or `model.remove()` on each document directly, which makes coding much easier. \n\nAt the same time, Mongoose also casts each value in every document returned from MongoDB to their [schema types](http://mongoosejs.com/docs/guide.html), which makes sure your database value types stays consistent. \n\nHowever, sometimes in situations where we need to READ a lot of data and have no intentions of doing anything to the \ndocument, we can call [query.lean()](http://mongoosejs.com/docs/api.html#query_Query-lean) when constructing \nour queries to remove the step which casts the documents into models. Documents are returned as normal Javascript \nobjects without the Model constructor functions, and values are not casted. This speeds things up considerably. \n\n### Caching with Redis\n\nWhat if we want to speed things up even faster? \n\nIn these situations where we don't need Mongoose model functionalities, we may want to ramp out our reading speed \nhigher by caching data in Redis.\n\nRedis is awesome for caching data. By caching our MongoDB results in Redis, we can get at least 300% increase in \nspeed when reading the data. This is AFTER indexing in MongoDB. \n\nCool for high-volume data reading! \n\n## API\n\n### Setting up\n\n    # If you're running this locally, \n    mongooseRedisCache(mongoose)\n\n    # Or if you're running a remote Redis DB\n    mongooseRedisCache(mongoose, {\n       host: \"redisHost\",\n       port: \"redisPort\",\n       pass: \"redisPass\",\n       options: \"redisOptions\"\n     })\n\n### redisCache: Boolean \n\n    ExampleSchema.set('redisCache', true)\n\nREQUIRED\nCall this function on whatever collection you want to cache. You don't have to use this on every collection, \nright? Pick and choose your collections wisely, you shall. \n\n### expires: Number\n\n    ExampleSchema.set('expires', 30)\n\nOPTIONAL\nSet the expiry time for the Redis key in seconds. Defaults to 60. \n\n\n### query.lean()\n\n    query = Example.find({})\n    query.lean()\n    query.exec(function(err, results){\n      # Your results here #\n    })\n\nREQUIRED\nJust a reminder. Be sure to call this whenever you want the results to be cached! More info\nabout [query.lean()](http://mongoosejs.com/docs/api.html#query_Query-lean) here.\n\nYeah, that's it. What else did you expect? Meh. \n\n## How to Run Test\n\nTry testing this on your machine and let me know how it went for you! \n\nThe usual jazz: \n\n    # Clone em!\n    git clone https://github.com/conancat/mongoose-redis-cache.git\n    \n    # Install those packages\n    cd mongoose-redis-cache\n    npm install\n\n    # If you don't have Mocha installed already\n    npm install mocha -g \n\n    # Yeah, just get in there and get on it\n    cd tests\n    mocha\n\n### How the tests are run\n\n#### Mock data\nWe generate a set number of mock data in the DB (defaults to 30000 items). \nEach item contains a random person's name, some arbitary number as random data, a date, and \nn array for the person's friend.\n\nFor testing purposes, we also called `ensureIndex()` on MongoDB to make sure we index\nthe field we want to query. \n\n#### Execute test rounds\nFor every round we query the database for all the names (defaults to 20 of them),\nand tracks the amount of time required to return the data. Each query returns around 1100 documents per call. \nWe run these same queries with and without Redis caching, for 20 rounds. Then we average out the time \nneeded to return the data. \n\nAll queries are query.lean(), meaning all documents returned are NOT casted as Mongoose models.\nThis gives us fair comparison between Redis caching and direct MongoDB queries. \n\n## These awesome people!\n\nMore resources: \n\n* [mongoose](https://github.com/LearnBoost/mongoose)\n* [node-redis](https://github.com/mranney/node_redis)\n\n## Contact\n\nLet's talk! I'm on [Twitter](https://twitter.com/conancat), [Reddit](http://www.reddit.com/user/conancat), or you can email me at conancat@gmail.com. \n\n","_id":"mongoose-redis-cache@0.0.1","dist":{"shasum":"a52c5059994873d382b30abb5b4e7ba921a309ea","tarball":"https://registry.npmjs.org/mongoose-redis-cache/-/mongoose-redis-cache-0.0.1.tgz","integrity":"sha512-89kXlTlQ2YSFUq80oPmY3ITFFeSv6Toy2M387qsBWDmGmr+/u+C3Nj3b5qq3+JMN+FdGbvoJcYq6L99WYCqK3g==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC7dZWC4bZYhFfP5E5AYBtzlEv6oDOFsgA0d8amhQPs2gIgMPMQ2XCX0Y0bNIEapfbv2k9dws2loYnhAljxDqseuBY="}]},"maintainers":[{"name":"conancat","email":"conancat@gmail.com"}]},"0.0.2":{"name":"mongoose-redis-cache","version":"0.0.2","description":"Plugin to cache Mongoose MongoDB query results in Redis","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"license":"BSD","directories":{"test":"test"},"dependencies":{"redis":"~0.8.2","underscore":"~1.4.4"},"devDependencies":{"mocha":"~1.8.1","async":"~0.2.5","mongoose":"~3.5.7"},"repository":{"type":"git","url":"https://github.com/conancat/mongoose-redis-cache.git"},"keywords":["mongodb","mongoose","redis","cache"],"author":{"name":"Grey Ang"},"readme":"# mongoose-redis-cache\n\nPlugin to cache Mongoose MongoDB query results in Redis. Lean DB queries, at least 300% faster! \n\n## How to use\nFirst, the usual: \n\n    npm install mongoose-redis-cache\n\nThen, \n\n    Setup mongoose connect as usual: \n\n       var mongoose = require(\"mongoose\");\n       var mongooseRedisCache = require(\"mongoose-redis-cache\");\n       mongoose.connect(\"mongodb://localhost/mongoose-redis-test\")\n\n    Create your schemas as usual: \n\n       var ExampleSchema = new Schema(function(){\n          field1: String\n          field2: Number\n          field3: Date\n       });\n     \n    Enable redisCache on the schema! \n       \n       REQUIRED: Enable Redis caching on this schema by specifying\n\n           ExampleSchema.set('redisCache', true)\n\n       OPTIONAL: Change the time for the cache of this schema. Defaults to 60 seconds. \n     \n           ExampleSchema.set('expires', 30)\n\n    Register the schema as usual: \n         \n         Example = mongoose.model('Example', ExampleSchema)\n\n    Setup your mongooseCache options:\n\n        # If you're running this locally, \n        mongooseRedisCache(mongoose)\n\n        # Or if you're running a remote Redis DB\n        mongooseRedisCache(mongoose, {\n           host: \"redisHost\",\n           port: \"redisPort\",\n           pass: \"redisPass\",\n           options: \"redisOptions\"\n         })\n     \n    Make a query as usual:\n         \n        query = Example.find({}) \n        query.where(\"field1\", \"foo\")\n        query.where(\"field2\").gte(30)\n        query.lean()  mongooseRedisCache only works for query.lean() queries!\n        query.exec(function(err, result){\n            Do whatever here! \n        });\n\nCheck out the test example for more information. \n\n## Test results: \n\n    =========================\n    Mongoose-Redis Cache Test\n    =========================\n    Total items in DB: 30000\n    Total number of queries per round: 20\n    Total number of rounds: 30\n\n    Generating 30000 mocks...\n\n    --------------------------------\n    Test query without Redis caching\n    --------------------------------\n    Begin executing queries without caching\n    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․\n\n    Total time for 30 test rounds: 12620ms\n    Average time for each round: 420.67ms\n\n    --------------------------------\n    Test query with Redis caching\n    --------------------------------\n    Begin executing queries with Redis caching\n    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․\n\n    Total time for 30 test rounds: 3618ms\n    Average time for each round: 120.60ms\n    ------------\n    CONCLUSION\n    ------------\n    Caching with Redis makes Mongoose lean queries faster by 9002 ms.\n    That's 348.81% faster!  \n\n\n    End tests. \n    Wiping DB and exiting\n\n## How this works\n\n### What is a Mongoose `lean` query? \n\n[Mongoose](http://mongoosejs.com), the MongoDB ORM for NodeJS has an awesome feature which casts each document \nas a Mongoose [model](http://mongoosejs.com/docs/models.html). This allows awesome features like being able to call `model.save()` or `model.remove()` on each document directly, which makes coding much easier. \n\nAt the same time, Mongoose also casts each value in every document returned from MongoDB to their [schema types](http://mongoosejs.com/docs/guide.html), which makes sure your database value types stays consistent. \n\nHowever, sometimes in situations where we need to READ a lot of data and have no intentions of doing anything to the \ndocument, we can call [query.lean()](http://mongoosejs.com/docs/api.html#query_Query-lean) when constructing \nour queries to remove the step which casts the documents into models. Documents are returned as normal Javascript \nobjects without the Model constructor functions, and values are not casted. This speeds things up considerably. \n\n### Caching with Redis\n\nWhat if we want to speed things up even faster? \n\nIn these situations where we don't need Mongoose model functionalities, we may want to ramp out our reading speed \nhigher by caching data in Redis.\n\nRedis is awesome for caching data. By caching our MongoDB results in Redis, we can get at least 300% increase in \nspeed when reading the data. This is AFTER indexing in MongoDB. \n\nCool for high-volume data reading! \n\n## API\n\n### Setting up\n\n    # If you're running this locally, \n    mongooseRedisCache(mongoose)\n\n    # Or if you're running a remote Redis DB\n    mongooseRedisCache(mongoose, {\n       host: \"redisHost\",\n       port: \"redisPort\",\n       pass: \"redisPass\",\n       options: \"redisOptions\"\n     })\n\n### redisCache: Boolean \n\n    ExampleSchema.set('redisCache', true)\n\nREQUIRED\nCall this function on whatever collection you want to cache. You don't have to use this on every collection, \nright? Pick and choose your collections wisely, you shall. \n\n### expires: Number\n\n    ExampleSchema.set('expires', 30)\n\nOPTIONAL\nSet the expiry time for the Redis key in seconds. Defaults to 60. \n\n\n### query.lean()\n\n    query = Example.find({})\n    query.lean()\n    query.exec(function(err, results){\n      # Your results here #\n    })\n\nREQUIRED\nJust a reminder. Be sure to call this whenever you want the results to be cached! More info\nabout [query.lean()](http://mongoosejs.com/docs/api.html#query_Query-lean) here.\n\nYeah, that's it. What else did you expect? Meh. \n\n## How to Run Test\n\nTry testing this on your machine and let me know how it went for you! \n\nThe usual jazz: \n\n    # Clone em!\n    git clone https://github.com/conancat/mongoose-redis-cache.git\n    \n    # Install those packages\n    cd mongoose-redis-cache\n    npm install\n\n    # If you don't have Mocha installed already\n    npm install mocha -g \n\n    # Yeah, just get in there and get on it\n    cd tests\n    mocha\n\n### How the tests are run\n\n#### Mock data\nWe generate a set number of mock data in the DB (defaults to 30000 items). \nEach item contains a random person's name, some arbitary number as random data, a date, and \nn array for the person's friend.\n\nFor testing purposes, we also called `ensureIndex()` on MongoDB to make sure we index\nthe field we want to query. \n\n#### Execute test rounds\nFor every round we query the database for all the names (defaults to 20 of them),\nand tracks the amount of time required to return the data. Each query returns around 1100 documents per call. \nWe run these same queries with and without Redis caching, for 20 rounds. Then we average out the time \nneeded to return the data. \n\nAll queries are query.lean(), meaning all documents returned are NOT casted as Mongoose models.\nThis gives us fair comparison between Redis caching and direct MongoDB queries. \n\n## These awesome people!\n\n* [mongoose](https://github.com/LearnBoost/mongoose)\n* [node-redis](https://github.com/mranney/node_redis)\n\n## Contact\n\nLet's talk! I'm on [Twitter](https://twitter.com/conancat), [Reddit](http://www.reddit.com/user/conancat), or you can email me at conancat@gmail.com. \n\n","_id":"mongoose-redis-cache@0.0.2","dist":{"shasum":"69ba5308a413c6cd13b37a9f3b7800655559060f","tarball":"https://registry.npmjs.org/mongoose-redis-cache/-/mongoose-redis-cache-0.0.2.tgz","integrity":"sha512-M39EbryiK/AD+LL3W3cLB8P7AI5GjxScZG+KCcwnQLKeKRq3mSgkdkIPyqwHiAk3cHrzcWKcSUbiIpdJx/TFuQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDTgxGGMBsiq0d19WbS8NKgrvstf+kKjyXQN5pnkSOzogIhAJDxaLCW1D6PCOJvj8Z4zkatYNDZVfVbQUpLYB9obgsx"}]},"maintainers":[{"name":"conancat","email":"conancat@gmail.com"}]},"0.0.3":{"name":"mongoose-redis-cache","version":"0.0.3","description":"Plugin to cache Mongoose MongoDB query results in Redis","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"license":"BSD","directories":{"test":"test"},"dependencies":{"redis":"~0.8.2","underscore":"~1.4.4"},"devDependencies":{"mocha":"~1.8.1","async":"~0.2.5","mongoose":"~3.5.7"},"repository":{"type":"git","url":"https://github.com/conancat/mongoose-redis-cache.git"},"keywords":["mongodb","mongoose","redis","cache"],"author":{"name":"Grey Ang"},"readme":"# mongoose-redis-cache\n\nPlugin to cache Mongoose MongoDB query results in Redis. Lean DB queries, at least 300% faster! \n\n## How to use\nFirst, the usual: \n\n    npm install mongoose-redis-cache\n\nThen, \n\n    Setup mongoose connect as usual: \n\n       var mongoose = require(\"mongoose\");\n       var mongooseRedisCache = require(\"mongoose-redis-cache\");\n       mongoose.connect(\"mongodb://localhost/mongoose-redis-test\")\n\n    Create your schemas as usual: \n\n       var ExampleSchema = new Schema(function(){\n          field1: String\n          field2: Number\n          field3: Date\n       });\n     \n    Enable redisCache on the schema! \n       \n       REQUIRED: Enable Redis caching on this schema by specifying\n\n           ExampleSchema.set('redisCache', true)\n\n       OPTIONAL: Change the time for the cache of this schema. Defaults to 60 seconds. \n     \n           ExampleSchema.set('expires', 30)\n\n    Register the schema as usual: \n         \n         Example = mongoose.model('Example', ExampleSchema)\n\n    Setup your mongooseCache options:\n\n        # If you're running this locally, \n        mongooseRedisCache(mongoose)\n\n        # Or if you're running a remote Redis DB\n        mongooseRedisCache(mongoose, {\n           host: \"redisHost\",\n           port: \"redisPort\",\n           pass: \"redisPass\",\n           options: \"redisOptions\"\n         })\n     \n    Make a query as usual:\n         \n        query = Example.find({}) \n        query.where(\"field1\", \"foo\")\n        query.where(\"field2\").gte(30)\n        query.lean()  mongooseRedisCache only works for query.lean() queries!\n        query.exec(function(err, result){\n            Do whatever here! \n        });\n\nCheck out the test example for more information. \n\n## Test results: \n\n    =========================\n    Mongoose-Redis Cache Test\n    =========================\n    Total items in DB: 30000\n    Total number of queries per round: 20\n    Total number of rounds: 30\n\n    Generating 30000 mocks...\n\n    --------------------------------\n    Test query without Redis caching\n    --------------------------------\n    Begin executing queries without caching\n    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․\n\n    Total time for 30 test rounds: 12620ms\n    Average time for each round: 420.67ms\n\n    --------------------------------\n    Test query with Redis caching\n    --------------------------------\n    Begin executing queries with Redis caching\n    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․\n\n    Total time for 30 test rounds: 3618ms\n    Average time for each round: 120.60ms\n    ------------\n    CONCLUSION\n    ------------\n    Caching with Redis makes Mongoose lean queries faster by 9002 ms.\n    That's 348.81% faster!  \n\n\n    End tests. \n    Wiping DB and exiting\n\n## How this works\n\n### What is a Mongoose `lean` query? \n\n[Mongoose](http://mongoosejs.com), the MongoDB ORM for NodeJS has an awesome feature which casts each document \nas a Mongoose [model](http://mongoosejs.com/docs/models.html). This allows awesome features like being able to call `model.save()` or `model.remove()` on each document directly, which makes coding much easier. \n\nAt the same time, Mongoose also casts each value in every document returned from MongoDB to their [schema types](http://mongoosejs.com/docs/guide.html), which makes sure your database value types stays consistent. \n\nHowever, sometimes in situations where we need to READ a lot of data and have no intentions of doing anything to the \ndocument, we can call [query.lean()](http://mongoosejs.com/docs/api.html#query_Query-lean) when constructing \nour queries to remove the step which casts the documents into models. Documents are returned as normal Javascript \nobjects without the Model constructor functions, and values are not casted. This speeds things up considerably. \n\n### Caching with Redis\n\nWhat if we want to speed things up even faster? \n\nIn these situations where we don't need Mongoose model functionalities, we may want to ramp out our reading speed \nhigher by caching data in Redis.\n\nRedis is awesome for caching data. By caching our MongoDB results in Redis, we can get at least 300% increase in \nspeed when reading the data. This is AFTER indexing in MongoDB. \n\nCool for high-volume data reading! \n\n## API\n\n### Setting up\n\n    # If you're running this locally, \n    mongooseRedisCache(mongoose)\n\n    # Or if you're running a remote Redis DB\n    mongooseRedisCache(mongoose, {\n       host: \"redisHost\",\n       port: \"redisPort\",\n       pass: \"redisPass\",\n       options: \"redisOptions\"\n     })\n\n### redisCache: Boolean \n\n    ExampleSchema.set('redisCache', true)\n\nREQUIRED\nCall this function on whatever collection you want to cache. You don't have to use this on every collection, \nright? Pick and choose your collections wisely, you shall. \n\n### expires: Number\n\n    ExampleSchema.set('expires', 30)\n\nOPTIONAL\nSet the expiry time for the Redis key in seconds. Defaults to 60. \n\n\n### query.lean()\n\n    query = Example.find({})\n    query.lean()\n    query.exec(function(err, results){\n      # Your results here #\n    })\n\nREQUIRED\nJust a reminder. Be sure to call this whenever you want the results to be cached! More info\nabout [query.lean()](http://mongoosejs.com/docs/api.html#query_Query-lean) here.\n\nYeah, that's it. What else did you expect? Meh. \n\n## How to Run Test\n\nTry testing this on your machine and let me know how it went for you! \n\nThe usual jazz: \n\n    # Clone em!\n    git clone https://github.com/conancat/mongoose-redis-cache.git\n    \n    # Install those packages\n    cd mongoose-redis-cache\n    npm install\n\n    # If you don't have Mocha installed already\n    npm install mocha -g \n\n    # Yeah, just get in there and get on it\n    cd tests\n    mocha\n\n### How the tests are run\n\n#### Mock data\nWe generate a set number of mock data in the DB (defaults to 30000 items). \nEach item contains a random person's name, some arbitary number as random data, a date, and \nn array for the person's friend.\n\nFor testing purposes, we also called `ensureIndex()` on MongoDB to make sure we index\nthe field we want to query. \n\n#### Execute test rounds\nFor every round we query the database for all the names (defaults to 20 of them),\nand tracks the amount of time required to return the data. Each query returns around 1100 documents per call. \nWe run these same queries with and without Redis caching, for 20 rounds. Then we average out the time \nneeded to return the data. \n\nAll queries are query.lean(), meaning all documents returned are NOT casted as Mongoose models.\nThis gives us fair comparison between Redis caching and direct MongoDB queries. \n\n## These awesome people!\n\n* [mongoose](https://github.com/LearnBoost/mongoose)\n* [node-redis](https://github.com/mranney/node_redis)\n\n## Contact\n\nLet's talk! I'm on [Twitter](https://twitter.com/conancat), [Reddit](http://www.reddit.com/user/conancat), or you can email me at conancat@gmail.com. \n\n","_id":"mongoose-redis-cache@0.0.3","dist":{"shasum":"929c5e7617623159a3ae39c006dead14b9fe2cd2","tarball":"https://registry.npmjs.org/mongoose-redis-cache/-/mongoose-redis-cache-0.0.3.tgz","integrity":"sha512-vokf7K5yisA5l5yBsZf0grLNRn3bsf3gXXejm3pdwjXcKZ/1NwOhIhXOHA48DF5+lkuu8CB2wJ9jsmaJFmwb1g==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCG/WgnLcMmOBkkbUAWR5nH1AsjzEckXQ9MnV1SgQ+I+wIhAJLPgw5cpdg/RBjqlQK9gUJxXwXM+QE9YsecXY+Cl9p/"}]},"maintainers":[{"name":"conancat","email":"conancat@gmail.com"}]}},"readme":"# mongoose-redis-cache\n\nPlugin to cache Mongoose MongoDB query results in Redis. Lean DB queries, at least 300% faster! \n\n## How to use\nFirst, the usual: \n\n    npm install mongoose-redis-cache\n\nThen, \n\n    Setup mongoose connect as usual: \n\n       var mongoose = require(\"mongoose\");\n       var mongooseRedisCache = require(\"mongoose-redis-cache\");\n       mongoose.connect(\"mongodb://localhost/mongoose-redis-test\")\n\n    Create your schemas as usual: \n\n       var ExampleSchema = new Schema(function(){\n          field1: String\n          field2: Number\n          field3: Date\n       });\n     \n    Enable redisCache on the schema! \n       \n       REQUIRED: Enable Redis caching on this schema by specifying\n\n           ExampleSchema.set('redisCache', true)\n\n       OPTIONAL: Change the time for the cache of this schema. Defaults to 60 seconds. \n     \n           ExampleSchema.set('expires', 30)\n\n    Register the schema as usual: \n         \n         Example = mongoose.model('Example', ExampleSchema)\n\n    Setup your mongooseCache options:\n\n        # If you're running this locally, \n        mongooseRedisCache(mongoose)\n\n        # Or if you're running a remote Redis DB\n        mongooseRedisCache(mongoose, {\n           host: \"redisHost\",\n           port: \"redisPort\",\n           pass: \"redisPass\",\n           options: \"redisOptions\"\n         })\n     \n    Make a query as usual:\n         \n        query = Example.find({}) \n        query.where(\"field1\", \"foo\")\n        query.where(\"field2\").gte(30)\n        query.lean()  mongooseRedisCache only works for query.lean() queries!\n        query.exec(function(err, result){\n            Do whatever here! \n        });\n\nCheck out the test example for more information. \n\n## Test results: \n\n    =========================\n    Mongoose-Redis Cache Test\n    =========================\n    Total items in DB: 30000\n    Total number of queries per round: 20\n    Total number of rounds: 30\n\n    Generating 30000 mocks...\n\n    --------------------------------\n    Test query without Redis caching\n    --------------------------------\n    Begin executing queries without caching\n    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․\n\n    Total time for 30 test rounds: 12620ms\n    Average time for each round: 420.67ms\n\n    --------------------------------\n    Test query with Redis caching\n    --------------------------------\n    Begin executing queries with Redis caching\n    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․\n\n    Total time for 30 test rounds: 3618ms\n    Average time for each round: 120.60ms\n    ------------\n    CONCLUSION\n    ------------\n    Caching with Redis makes Mongoose lean queries faster by 9002 ms.\n    That's 348.81% faster!  \n\n\n    End tests. \n    Wiping DB and exiting\n\n## How this works\n\n### What is a Mongoose `lean` query? \n\n[Mongoose](http://mongoosejs.com), the MongoDB ODM for NodeJS has an awesome feature which casts each document \nas a Mongoose [model](http://mongoosejs.com/docs/models.html). This allows awesome features like being able to call `model.save()` or `model.remove()` on each document directly, which makes coding much easier. \n\nAt the same time, Mongoose also casts each value in every document returned from MongoDB to their [schema types](http://mongoosejs.com/docs/guide.html), which makes sure your database value types stays consistent. \n\nHowever, sometimes in situations where we need to READ a lot of data and have no intentions of doing anything to the \ndocument, we can call [query.lean()](http://mongoosejs.com/docs/api.html#query_Query-lean) when constructing \nour queries to remove the step which casts the documents into models. Documents are returned as normal Javascript \nobjects without the Model constructor functions, and values are not casted. This speeds things up considerably. \n\n### Caching with Redis\n\nWhat if we want to speed things up even faster? \n\nIn these situations where we don't need Mongoose model functionalities, we may want to ramp out our reading speed \nhigher by caching data in Redis.\n\nRedis is awesome for caching data. By caching our MongoDB results in Redis, we can get at least 300% increase in \nspeed when reading the data. This is AFTER indexing in MongoDB. \n\nCool for high-volume data reading! \n\n## API\n\n### Setting up\n\n    # If you're running this locally, \n    mongooseRedisCache(mongoose)\n\n    # Or if you're running a remote Redis DB\n    mongooseRedisCache(mongoose, {\n       host: \"redisHost\",\n       port: \"redisPort\",\n       pass: \"redisPass\",\n       options: \"redisOptions\"\n     })\n\n### redisCache: Boolean \n\n    ExampleSchema.set('redisCache', true)\n\nREQUIRED\nCall this function on whatever collection you want to cache. You don't have to use this on every collection, \nright? Pick and choose your collections wisely, you shall. \n\n### expires: Number\n\n    ExampleSchema.set('expires', 30)\n\nOPTIONAL\nSet the expiry time for the Redis key in seconds. Defaults to 60. \n\n\n### query.lean()\n\n    query = Example.find({})\n    query.lean()\n    query.exec(function(err, results){\n      # Your results here #\n    })\n\nREQUIRED\nJust a reminder. Be sure to call this whenever you want the results to be cached! More info\nabout [query.lean()](http://mongoosejs.com/docs/api.html#query_Query-lean) here.\n\nYeah, that's it. What else did you expect? Meh. \n\n## How to Run Test\n\nTry testing this on your machine and let me know how it went for you! \n\nThe usual jazz: \n\n    # Clone em!\n    git clone https://github.com/conancat/mongoose-redis-cache.git\n    \n    # Install those packages\n    cd mongoose-redis-cache\n    npm install\n\n    # If you don't have Mocha installed already\n    npm install mocha -g \n\n    # Yeah, just get in there and get on it\n    cd tests\n    mocha\n\n### How the tests are run\n\n#### Mock data\nWe generate a set number of mock data in the DB (defaults to 30000 items). \nEach item contains a random person's name, some arbitary number as random data, a date, and \nn array for the person's friend.\n\nFor testing purposes, we also called `ensureIndex()` on MongoDB to make sure we index\nthe field we want to query. \n\n#### Execute test rounds\nFor every round we query the database for all the names (defaults to 20 of them),\nand tracks the amount of time required to return the data. Each query returns around 1100 documents per call. \nWe run these same queries with and without Redis caching, for 20 rounds. Then we average out the time \nneeded to return the data. \n\nAll queries are query.lean(), meaning all documents returned are NOT casted as Mongoose models.\nThis gives us fair comparison between Redis caching and direct MongoDB queries. \n\n## These awesome people!\n\nMore resources: \n\n* [mongoose](https://github.com/LearnBoost/mongoose)\n* [node-redis](https://github.com/mranney/node_redis)\n\n## Contact\n\nLet's talk! I'm on [Twitter](https://twitter.com/conancat), [Reddit](http://www.reddit.com/user/conancat), or you can email me at conancat@gmail.com. \n\n","maintainers":[{"name":"conancat","email":"conancat@gmail.com"}],"time":{"modified":"2022-06-20T07:40:19.187Z","created":"2013-02-24T08:05:57.035Z","0.0.1":"2013-02-24T08:06:01.855Z","0.0.2":"2013-02-25T11:11:03.307Z","0.0.3":"2013-02-28T01:30:34.401Z"},"author":{"name":"Grey Ang"},"repository":{"type":"git","url":"https://github.com/conancat/mongoose-redis-cache.git"},"users":{"koslun":true,"claytonsilva":true,"laurentknauss":true,"akarsh":true,"shreyawhiz":true,"peterkingsbury":true}}