npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

monc

v0.1.2

Published

The ultimate MongoDB cache module.

Downloads

37

Readme

Monc - the ultimate MongoDB cache module

Installation

Monc is quite easy to install and configure.

Using NPM

npm install monc --save

Getting started

Monc should be configured to work along with Mongoose.

var monc = require('monc'),
    mongoose = require('mongoose'),
    options  = {};

//Configure monc
monc.install(mongoose, options);

Monc is all set and ready to work.

Options

  • max The maximum size of the cache, checked by applying the length function to all values in the cache. Not setting this is kind of silly, since that's the whole purpose of this lib, but it defaults to Infinity.
  • maxAge Maximum age in ms. Items are not pro-actively pruned out as they age, but if you try to get an item that is too old, it'll drop it and return undefined instead of giving it to you.
  • setFlag : boolean. This option allows Monc module to attach a comingFromCache property on each document retrieved from the cache
  • canLog: boolean. This option allows Monc module to log information values on the console.
  • modelValues: boolean . This option allows Monc module to keep a record with each cache object saved per Model.

Monc API

Monc attaches several useful methods both on Models and Query operations

Query methods

.cache(binKey) This chainable method stores or retrieves a set of documents. The binKey is optional.

var test = mongoose.model('testModel');
test.find({}).lean().cache().exec(function(err, docs) {
  //docs are saved into the cache  
});

test.find({}).lean().cache().exec(function(err, docs) {
  //docs mapped to binkey:  "helloThere" are retrieved from Cache
});

test.find({}).lean().exec(function(err, docs) {
  //docs are retrieved from the db
});

test.find({}).lean().cache("helloCache").exec(function(err, docs) {
  //docs are saved into the cache, mapped to "helloCache"
});

.clean(binKey) Removes the stored objects. The binKey is optional.

  var test = mongoose.model('testModel');
  test.find({}).cleanAll().cache("helloCache").exec(function(err, docs) {
    //Firsty the cache is reset
    //afterwards docs are saved into the cache, mapped to "helloCache"
  });

.cleanAll() Resets the whole cache.

  var test = mongoose.model('testModel');
  test.find({}).cleanAll().cache("helloCache").exec(function(err, docs) {
    //Firsty the cache is reset
    //afterwards docs are saved into the cache, mapped to "helloCache"
  });

.cleanMultiple([binkeys]) Resets all the associated values maped

  var test = mongoose.model('testModel');
  test.find({}).cleanMultiple(["binkey1","binkey2"]).cache("helloCache").exec(function(err, docs) {
    //Firsty binkey1 and binkey2 are removed from the cache
    //afterwards docs are saved into the cache, mapped to "helloCache"
  });

Model methods

Since fetching Mongoose queries could be hard and inefficient Monc attaches useful methods on the Models as well.

.cleanAll() Resets the whole cache.

  var test = mongoose.model('testModel');
  test.cleanAll();

.cleanMultiple([binkeys]) Resets all the associated values maped

  var test = mongoose.model('testModel');
  test.cleanMultiple(["binkey1","binkey2"]);
    //Firsty binkey1 and binkey2 are removed from the cache
    //afterwards docs are saved into the cache, mapped to "helloCache"

.allValues() Grab all the values stored into the cache.

  var test = mongoose.model('testModel');
  test.allValues().map(function(v,i){
    console.log("stored", v)
  });

.dump() Grab all the values and matched keys stored into the cache.

  var test = mongoose.model('testModel');
  test.dump().map(function(v,i){
   console.log("key", v["k"]);
   console.log("value", v["v"]);
  })

.getDir(key) Directly grab the associated stored object.

  var test = mongoose.model('testModel');
  test.setDir("key",[{"hello" : "world"}]);
  test.getDir("key").map(function(v,i){
   console.log("key", v["k"]);
   console.log("value", v["v"]);
  })

.setDir(key,val) Directly store an object to cached.

  var test = mongoose.model('testModel');
  test.setDir("key",{"hello" : "world"});

  test.dump().map(function(v,i){
   console.log("key", v["k"]); // key
   console.log("value", v["v"]); //{"hello" : "world"}
  })

.getModelBinKey() Each Model has a unique identifier, using .getModelBinKey() method you may retrieve it .

  var test = mongoose.model('testModel');
  console.log(test.getModelBinKey())

.assoc() One of the best methods provided. Each Query store into the cache is associated with the parent Model. Using assoc() you can retrieve the active keys that are mapped with the given model . This method requires the modelValues set to true.

  var test = mongoose.model('testModel');
  test.find({}).cache("helloCache").exec(function(err, docs) {});
  test.find({}).cache("helloCache1").exec(function(err, docs) {});

  test.assoc().map(function(v,i){
   console.log(v); // ["helloCache", "helloCache1"]
   //get the value !!

   console.log(test.getDir(v));
  })

.assocObj() Retrieve the associated stored objects and keys mapped to the parent Model. This method requires the modelValues set to true.

  var test = mongoose.model('testModel');
  test.find({}).cache("helloCache").exec(function(err, docs) {});
  test.find({}).cache("helloCache1").exec(function(err, docs) {});

  test.assocObj().map(function(v,i){
   console.log(v); //keys and objects
  })

.cleanAssoc() Removes all the associated stored objects and keys mapped to the parent Model. This method requires the modelValues set to true.


  var test = mongoose.model('testModel');
  test.cleanAssoc();

Running tests

Tests are powered by Mocha and Chai There are a bunch of them provided. Run them by.

  npm install
  npm test

There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton