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

node-http-cache

v2.3.3

Published

This module uses a simple filesystem storage to persist http responses. Then you can retrieve those responses from disk instead of an http connection.

Downloads

10

Readme

Build Status Build Status NPM version

Gitter

NPM

#HTTP Cache

This module uses a simple filesystem storage (levelup) to persist http responses. Storage is updated using cron expressions (see crontab manpage for more detail on how to build these expressions).


##Usage

var cacheFactory = require('node-http-cache');

// (...)

var config = {
  //Any logger with the following defined functions: error, warn, info, debug.
	logger: require('winston'),
	//Folder where the storage will be created.
	location: '/tmp',
	//List of services
	services:[{
		//Update every day at 00:00
		cronExpression: '0 0 * * *',
		name: 'cities',
		timezone: 'America/Buenos_Aires',
		httpOptions:{
			url: 'http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demoapp',
			headers: {
				'accept':'application/json'
			}
		},
		indexes: ['countrycode']
	}]
};

// (...)

var cache = cacheFactory(config);

// (...)

// Retrieves all cities
var allCities = cache.get(
  {
    name: 'cities'
  }
);
var onlyMXCities = cache.get(
  {
	  name: 'cities', 
	  indexKey: 'countrycode',
	  indexValue: 'MX'
); 

Configuration

location

Required: true

Root folder for levelup storage. Inside this directory a folder with the name node-http-cache.db will be created.

logger

Required: true

Any logger can be used here. The only requirement is to have this functions defined: error, warn, info, debug.

services.name

Required: true

Service identifier, this name MUST BE UNIQUE among all services.

services.cronExpression

Required: true

Use crontab expressions to specify when the snapshot should be updated.

services.httpOptions

Required: true

Node HTTP Cache uses HTTP Module internally to make the requests. You can set any option specified in its docs. Only service.httpOptions.url is required.

services.timezone

Required: false

Default: 'GMT-0'

services.itemsPath

Required: false

Path to specify where is the array of objects to store. For example, if the response of the service is: {items:[]}, then itemsPath: 'items'. To specify nested elements, you can use dot notation (i.e.: itemsPath: 'root.items')

services.indexes

Required: false

Array of fields to be indexed. For example, if the response of the service is [{ "user": "barney", "age": 36, "active": true},{ "user": "fred", "age": 40, "active": false }], then you can create an index by user using indexes: ["user"]

Retrieve data

get(config)

Retrieves data saved using the config received as parameter.

Returns a Promise

config.name

Required: true

Name used in config when the snapshot was created.

config.indexKey

Required: false

Name of the index used to search.

config.indexValue

*Required: false

If you specify an indexKey you MUST specify an indexValue.

Events

getData

Once data is retrieved from the filesystem storage.

{
  //Name of service retrieved
  name: String,
  //Data retrieved
  data: Object
}

getError

Error retrieving data from the filesystem storage. The returning value is an instance of Error

updateData

Once data is updated to the filesystem storage.

{
  //Name of service updated
  name: String,
  //Data retrieved
  data: Object
}

updateError

Error updating data for service. The returning value is an instance of Error

TO DO

  • Partial Updates
  • ~~In memory storage~~
  • ~~Indexes~~