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

bastian

v0.1.0

Published

Handles partial cache misses, generically, using Redis as a cache store

Downloads

203

Readme

Bastian

This tool is useful for situations where you make many requests for known sets of data, and have partial cache misses.

For example, if you would like to access restaurants 1, 2, 3, and you already have restaurants 2, 3, 4 in the cache, you should only read restaurant 1 from disk.

npm install --save bastian

Example Collection Usage

var request = require('request');
var redis = require('redis').createClient();
var Bastian = require('bastian');

var cache = new Bastian(redis);

cache.on('error', function(err) {
  assert.ifError(err);
});

function serviceCall(ids, language, callback) {
  const VERSION = 'v4';
  cache.lookup({
    primary: 'id',
    keyPrefix: 'cuisine-' + language + '-' + VERSION,
    ids: ids,
    expiration: 60 * 60 * 24,
    handler: function(ids, cb) {
      var url = 'http://cuisine.api.opentable.com/' + VERSION + '/cuisines/?lang=' + language + '&ids=[' + ids + ']';

      console.log(`Request: ${url}`);

      request(url, function(err, response, body) {
        if (err) {
          return cb(err);
        }

        if (response.statusCode !== 200) {
          return cb(new Error("Unable to load data from remote server"));
        }

        cb(null, body); // Be sure to resolve an array of objects with id (primary) attributes
      });
    }
  }, callback);
}

serviceCall([1, 2, 3], 'en-US', function(err, data) {
  // Request: http://cuisine.api.opentable.com/v4/cuisines/?lang=en-US&ids=[1,2,3]
  // data contains items 1, 2, 3
});

serviceCall([3, 4, 5], 'en-US', function(err, data) {
  // Request: http://cuisine.api.opentable.com/v4/cuisines/?lang=en-US&ids=[4,5]
  // data contains items 3, 4, 5
});

serviceCall([1, 2, 3, 4, 5], 'en-US', function(err, data) {
  // No URL request is made!
  // data contains items 1, 2, 3, 4, 5
});

serviceCall([1, 2, 3], 'es-MX', function(err, data) {
  // Request: http://cuisine.api.opentable.com/v4/cuisines/?lang=es-MX&ids=[1,2,3]
  // data contains items 1, 2, 3
});

Example Singular Usage

We also expose a method called .get() which is useful for loading singular items.

Note that the id parameter is optional. If present it will be appended to the key in the same manner that lookup() appends (they're compatible). If missing it will not be appended and will not be provided to handler().

var request = require('request');
var redis = require('redis').createClient();
var Bastian = require('bastian');

var cache = new Bastian(redis);

cache.on('error', function(err) {
  assert.ifError(err);
});

function serviceCall(id, language, callback) {
  const VERSION = 'v4';
  cache.get({
    keyPrefix: 'restaurant-' + language + '-' + VERSION,
    id: id,
    expiration: 60 * 60 * 24,
    handler: function(id, cb) {
      var url = 'http://restaurant.api.opentable.com/' + VERSION + '/restaurants/?lang=' + language + '&id=' + ids;

      console.log(`Request: ${url}`);

      request(url, function(err, response, body) {
        if (err) {
          return cb(err);
        }

        if (response.statusCode !== 200) {
          return cb(new Error("Unable to load data from remote server"));
        }

        cb(null, body); // Whatever is returned will be cached
      });
    }
  }, callback);
}

serviceCall(1, 'en-US', function(err, data) {
  // Request: http://restaurant.api.opentable.com/v4/restaurants/?lang=en-US&id=1
  // data contains item 1
});

serviceCall(2, 'en-US', function(err, data) {
  // Request: http://restaurant.api.opentable.com/v4/restaurants/?lang=en-US&id=2
  // data contains item 2
});

serviceCall(1, 'en-US', function(err, data) {
  // No URL request is made!
  // data contains item 1
});

serviceCall(1, 'es-MX', function(err, data) {
  // Request: http://restaurant.api.opentable.com/v4/restaurants/?lang=es-MX&id=1
  // data contains item 1
});

TODO

  • Prevent the same process from making duplicate simultaneous lookups
    • Keep a local array of requested entities
  • Prevent multiple processes from making duplicate simultaneous lookups
    • Keep a list of requested entities in Redis
    • Use Pub/Sub to announce when data has become available