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).
npm install cachebox
CacheBox has two initialization parameters:
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 ;
Attempt to make a withdraw. If no results, execute request/computation/etc. Then, deposit it for next time.
var params = 'distance': 1000 'query': 'tacos' ;paramslonlat = -73983049 4075532;// Try to withdraw from cachecachebox;
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!