#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'
);
Required: true
Root folder for levelup storage. Inside this directory a folder with the name node-http-cache.db
will be created.
Required: true
Any logger can be used here. The only requirement is to have this functions defined: error
, warn
, info
, debug
.
Required: true
Service identifier, this name MUST BE UNIQUE among all services.
Required: true
Use crontab expressions to specify when the snapshot should be updated.
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.
Required: false
Default: 'GMT-0'
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'
)
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"]
Retrieves data saved using the config received as parameter.
Returns a Promise
Required: true
Name used in config when the snapshot was created.
Required: false
Name of the index used to search.
*Required: false
If you specify an indexKey you MUST specify an indexValue.
Once data is retrieved from the filesystem storage.
{
//Name of service retrieved
name: String,
//Data retrieved
data: Object
}
Error retrieving data from the filesystem storage. The returning value is an instance of Error
Once data is updated to the filesystem storage.
{
//Name of service updated
name: String,
//Data retrieved
data: Object
}
Error updating data for service. The returning value is an instance of Error
- Partial Updates
In memory storageIndexes