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

mongo-atm

v1.1.2

Published

Provides simple in-memory caching and (optionally) provides connection helper for MongoDB

Downloads

2

Readme

mongo-atm

A simple in-memory cache for node.js with optional mongoDB helper

Installation

npm install mongo-atm

Simple Examples

var Cache = require('mongo-atm');
var myCache = new Cache ({
    ttl: 120, // OPTIONAL: time to live in seconds -- Defaults to 60 seconds.
    limit: 400, // OPTIONAL: maximum objects to keep in cache -- Defaults to 600.
    mongoClient: dbClient // OPTIONAL: mongoDB instance to allow mongo-atm to make calls to mongo. This can also be sent with the getMongo() call.
});

//Simple Caching
myCache.setCache('foo', 'bar');
console.log(myCache.getCache('foo')); //bar

//Custom TTL
myCache.setCache('foo2', 'bar2', 10); //sets ttl to 10 seconds
setTimeout(function(){
    console.log(myCache.getCache('foo2')); //null
},15000);


//Using Callbacks
myCache.setCache('foo3', 'bar3', function(data){
    console.log(data); //bar3
});
myCache.getCache('foo3',function(cachedResponse){
    if(cachedResponse)
        console.log(cachedResponse); //bar3
    else
        console.log('No cache for you!');
});

//If you're worried about latency waiting to re-cache expired items, 
//the getCache callback can also return expired items:
myCache.getCache('foo3',function(cachedResponse, expiredResponse){
    if(cachedResponse)
        console.log(cachedResponse); //bar3
    else if (expiredResponse){
        //now would be a good time to refresh the data and use setCache
        console.log(expiredResponse); //bar3
        myCache.setCache('foo3','newbar');
    } else {
        //not found in cache
        myCache.setCache('foo3','newbar',function(data){
            console.log(data); //newbar
        });
    }
});


//And finally for the fun part!! The getMongo() call automates the above function
//and queries mongoDB in one call:
myCache.getMongo('myCollection',{firstName:"David"},function(data){ //returns an array
    if(data.length > 0)
        console.log(data[0].firstName); //David
    else
        console.log("Nothing found!!");
});

API

Required constructor to create an instance of mongo-atm

var myCache = new Cache(options);
  • options is an optional settings object containing some or all of the following:
    • ttl is a number setting the time to live in seconds for the instance. Defaults to 60.
    • limit is a number setting the maximum number of objects kept in cache. Defaults to 600.
    • mongoClient is a mongoDB instance should you want/need mongo-atm to interact with your mongo collections.

Writes to cache

myCache.setCache(key, value, ttl, callback);
  • key is a string that identifies your cached object. [Required]
  • value is what ever you are wanting to cache. [Required]
  • ttl is a number that sets a custom time to live in seconds for the cached object. [Optional]
  • callback is a callback function that returns the object that was written to cache. [Optional]

Retrieves from cache for a given key

myCache.getCache(key, callback);
  • key is a string that identifies your cached object. [Required]
  • callback is a callback function that returns two values. [Optional]
    • cachedResponse is whatever was found in cache or returns null if cache was expired or missing. [Required]
    • expiredResponse is returned as the second parameter if the requested cached item has expired. [Optional]

Deletes a single item from cache for a given key

myCache.del(key);
  • key is a string that identifies the cached object to be removed. [Required]

Deletes all items from cache

myCache.flush();

mongoDB Helper

myCache.getMongo(collection, searchObj, options, callback);

Queries a provided mongoDB instance based on the criteria provided and automatically utilizes cache for the results. Please reference mongoDB docs for requirements for specific parameters.

  • collection is a string that identifies the mongoDB collection you wish to query. [Required]
  • searchObj is the search criteria for mongoDB in the form {key: value}. [Required]
  • options is an optional settings object containing some or all of the following:
    • mongoClient is your mongoDB instance. It is required if it wasn't declared with the constructor. [Conditionally Required]
    • queryOptions is an object used as the 3rd parameter in mongo find() function and includes options such as limit and skip in the form {limit: size, skip: page} [Optional]
    • sort is the field used to sort by in the form {field: 1} [Optional]
    • limit is a number setting the maximum number of results to return. Defaults to 50. [Optional]
    • projection is the projection fields to include in the results in the form {field: 1} [Optional]
    • callback is a callback function that returns the cached mongoDB results in the form of an array. [Required]