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 🙏

© 2025 – Pkg Stats / Ryan Hefner

cachebox

v0.0.2

Published

Mongo-driven query cache that supports geospatial lookups

Readme

CacheBox.js

CacheBox is a simple way to cache objects (e.g., HTTP request results). Basically, you setup the CacheBox, then you attempt to make a withdrawal by passing in the params of the query. If a record exists, it is returned and you use that payload. If it's new, then you continue to do your computation and "deposit" the result for next time. Geospatial support is provided so you can find results that are near a previous result (this is good for location-based searches where a slight deviation in location doesn't have much effect on the results).


Setup

Create Connection

CacheBox has two initialization parameters:

  • dburi - a path to a mongodb (e.g., mongodb://user:pass@localhost:port/databaseName)
  • config - options for setup

Config Options

  • timeToExpire {Number} - milliseconds until a cached object is purged, default: 1 day
  • auto_reconnect {Boolean} - if should autoreconnect to mongodb, default: true
  • maxDist {Number} - maxDist to pull from cache if using geospatial
  • distUnit {String} - 'm' or 'ft' as unit for geospatial
  • collectionName {String} - name of collection for cache, default: cachebox

Create Cachebox


var mongodbUri = 'mongodb://user:pass@localhost:port/databaseName';

var cbConfig = {
		  'timeToExpire' : 24 * 60 * 60 * 1000
		, 'auto_reconnect': true
		, 'maxDist' : 50
		, 'distUnit': 'ft'
		, 'collectionName': 'cachebox'
	};
	
var cachebox = new (require('cachebox')).CacheBox(mongodbUri, cbConfig);

Use it!

Attempt to make a withdraw. If no results, execute request/computation/etc. Then, deposit it for next time.


var params = { 'distance': 1000, 'query': 'tacos' };
params.lonlat = [-73.983049, 40.75532];

// Try to withdraw from cache
cachebox.withdraw(params, function(err, results) {
	if (err){ throw new Error(err); }
	else { 
		if (results) { callback(null, results); }
		else { 
		
			// Nothing in cache, so...
			// Do something complicated, like call a remote API
			// ... -> Gives us 'data'
			
			// Deposit the result so we don't need to waste time again
			// Here we return the data after making the deposit
			// But really you could return the data before making the deposit
			cachebox.deposit(params, data, function(err) {
				if (err) { throw new Error(err); }
				else { callback(null, data); }
			});	
			
		} 
	}
});


					

Geospatial Hotness!

Note that for geospatial to work, you must have maxDist and distUnit defined in your CacheBox config!

When data is geospatial, sometimes we don't need to re-execute a query if the location change is small. CacheBox (and mongo) make this easy. Just define maxDist and distUnit, then pass 'lonlat' as one of your params.

Note: lonlat must be an array with 2 numbers, and in the format [ longitude, latitude ]. While most people write it as latitude/longitude, Mongo follows the lon/lat spec.

Now when you make a withdraw you'll get results within "maxDist" that match the rest of the params. Hot!