Fetches a resource from a server and stores it inside a redis cache and the next time it grabs it from there
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example
test
.gitignore
.travis.yml
LICENSE
README.md
index.js
package.json

README.md

fetchncache build status

Fetches a resource from a server and stores it inside a redis cache and the next time it grabs it from there

var fetchncache = require('../')
  , nock = require('nock');

// This example requires a redis instance to run at REDIS_HOST:6379
var redis = {
    host: process.env.REDIS_HOST || '127.0.0.1'
  , port: 6379 
}
var service = {
  url: 'http://my.service.me'
}

// nocking service
nock(service.url)
  .defaultReplyHeaders({ 'Content-Type': 'application/json' })
  .get('/resource')
  .reply(200, { hello: 'world' }); 

var fnc = fetchncache({ 
    redis: redis
  , service: service
  , expire: 10 * 60 // expire cached values every 10 mins 
})

fnc.fetch('/resource', function (err, res, fromCache) {
  if (err) return console.error(err);
  console.log({ res: res, fromCache: fromCache });

  fnc.fetch('/resource', function (err, res, fromCache) {
    if (err) return console.error(err);
    console.log({ res: res, fromCache: fromCache });

    fnc.stop();
  })
})

Output on first run (assuming cache is empty)

{ res: '{"hello":"world"}', fromCache: undefined }
{ res: '{"hello":"world"}', fromCache: true }

Output on second run (assuming 10mins didn't pass)

{ res: '{"hello":"world"}', fromCache: true }
{ res: '{"hello":"world"}', fromCache: true }

Installation

npm install fetchncache

API

fetchncache(opts) → {Object}

Creates a fetchncache instance.

redis opts

  • opts.redis.host {number=} host at which redis is listening, defaults to 127.0.0.1
  • opts.redis.port {string=} port at which redis is listening, defaults to 6379

service opts

  • opts.service.url {string=} url at which to reach the service
Parameters:
Name Type Argument Description
opts Object <optional>
Properties
Name Type Argument Description
redis Object <optional>

redis options passed straight to redis (@see above)

service Object <optional>

options specifying how to reach the service that provides the data (@see above)

expire number <optional>

the default number of seconds after which to expire a resource from the redis cache (defaults to 15mins)

Source:
Returns:

fetchncache instance

Type
Object

fetchncache::clearCache() → {Object}

Clears the entire redis cache, so use with care. Mainly useful for testing or to ensure that all resources get refreshed.

Source:
Returns:

fetchncache instance

Type
Object

fetchncache::stop(force)

Stops the redis client in order to allow exiting the application. At this point this fetchncache instance becomes unusable and throws errors on any attempts to use it.

Parameters:
Name Type Argument Description
force boolean <optional>

will make it more aggressive about ending the underlying redis client (default: false)

Source:

fetcncache::fetch(uri, opts, cb)

Fetches the resource, probing the redis cache first and falling back to the service. If a transform function is provided (@see opts), that is applied to the result before returning or caching it. When fetched from the service it is added to the redis cached according to the provided options.

Parameters:
Name Type Argument Description
uri string

path to the resource, i.e. /reource1?q=1234

opts Object <optional>

configure caching and transformation behavior for this particular resource

Properties
Name Type Argument Description
expire number <optional>

overrides default expire for this particular resource

transform function <optional>

specify the transform function to be applied, default: function (res} { return res }

cb function

called back with an error or the transformed resource

Source:

generated with docme

License

MIT