Giter Club home page Giter Club logo

mongoose-cachebox's Introduction

mongoose cachebox

Build Status NPM version

Caching mongoose queries easier with cacheman that supports in-memory, and Redis engines.

Installation

$ npm install mongoose-cachebox

Usage

var mongoose = require('mongoose');

var options = {
  cache: true, // start caching
  ttl: 30 // 30 seconds
};

// adding mongoose cachebox
mongooseCachebox(mongoose, options);

Then later any find query will be cached for 30 seconds.

You can also enable caching programatically by using the cache method directly from the query instance:

var Person = mongoose.model('Person');

Person
  .find({ active: true })
  .cache('50s') // cache for 50 seconds
  .exec(function (err, docs) {
    if (err) throw error;

    console.log(docs.ttl); // time left for expiration in ms
    console.log(docs.stored); // timestamp this query was cached
    console.log(docs);
});

API

This plugin will add two more methods to a mongoose query instance cache and ttl.

query.cache([cached], [ttl])

Both parameters cache and ttl are optional, the first one is for enable caching the second is for specifying the cache expiration (time to live).

For start caching just call the cache method:

Person
  .find({ active: true })
  .cache() // will enable caching with 60 seconds ttl
  .exec(function (err, docs) {
    /* .... */
});

The above is equivalent to this:

Person
  .find({ active: true })
  .cache(true) // start caching with 60 seconds ttl
  .exec(function (err, docs) {
    /* .... */
});

You can specify the ttl (time to live) value directly:

Person
  .find({ active: true })
  .cache(10) // cache for 10 seconds
  .exec(function (err, docs) {
    /* .... */
});

The above is equivalent to this:

Person
  .find({ active: true })
  .cache(true, 10) // enable caching with 10 seconds ttl
  .exec(function (err, docs) {
    /* .... */
});

And to disable caching for specific query just pass false:

Person
  .find({ active: true })
  .cache(false) // stop caching this query
  .exec(function (err, docs) {
    /* .... */
});

query.ttl(ttl)

By default the ttl value is 60000 (60 seconds) but you can use the ttl method to specify a different value:

Person
  .find({ active: true })
  .cache() // cache query
  .ttl(10) // caching for 10 seconds
  .exec(function (err, docs) {
    /* .... */
});

Redis

By default mongoose-cachebox will use the memory engine to cache queries but it can cache queries using Redis by specifying redis engine when initializing the plugin:

var mongoose = require('mongoose');

var options = {
  engine: 'redis',
  host: '127.0.0.1',
  port: '6379',
  password: 'secret'
};

// adding mongoose cachebox
mongooseCachebox(mongoose, options);

This module use cacheman for the caching magic, so check out the project for more details and options.

Run tests

$ make test

License

(The MIT License)

Copyright (c) 2013 Jonathan Brumley <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

mongoose-cachebox's People

Contributors

calebtomlinson avatar cayasso avatar ryanseys avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mongoose-cachebox's Issues

Redis only

Hi,

Maybe it's just me but doesn't it seem obvious to add storing the cache in MongoDB also?

Thanks.

What about findOne or Model.findById?

From the looks of it, this only covers find() queries. Could this be expanded to also cover findOne() and the Model.prototype.findById functions? That was repeat requests for the same data can be cached as well.

Does it autocache without specifying .cache?

I've enabled cache by default:

var options = {
  cache: true, // start caching
  ttl: 30 // 30 seconds
};

// adding mongoose cachebox
mongooseCachebox(mongoose, options);

Do I still need to specify .cache(), like in this query:

Person
  .find({ active: true })
  .exec(function (err, docs) {
    /* .... */
});

will this be a cached query?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.