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
#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~~