http-cache

An extensible caching interface for HTTP traffic.

http-cache

npm install http-cache

https://npmjs.org/package/http-cache

Using Connect or Express?

var
    connect = require("connect"),
    http = require("http"),
    HttpCache = require("http-cache")
;

var app = connect()
    .use(new HttpCache({ }))
    .use(function(req, res) {
        res.end("Cache this response! Time=" + new Date().getTime());
    });

http.createServer(app).listen(8392);

Real coders use no middleware? We've got you covered...

var
    http = require("http"),
    HttpCache = require("http-cache")
;

var httpcache = new HttpCache({ });
http.createServer(function(req, res) {
    httpcache(req, res, function() {
        res.end("Cache this response! Time=" + new Date().getTime());
    });
}).listen(8392);

Both synchronous and asynchronous rules may be provided:

httpcache({
    rules: function(req, res) {
        // do not cache users folder
        return (/\/users\//i.test(req.url) === false);
    }
});

Async rules leverage cb instead of returning...

httpcache({
    rules: function(req, res, cb) {
        setTimeout(function() {
            // do not cache users folder
            cb(null, /\/users\//i.test(req.url) === false);
        }, 100);
    }
});

Multiple rules may be provided as well... (will be processed in parallel)

httpcache({
    rules: [ rule1, rule2, rule3 ]
});
npm test

Now you can view coverage using any browser here:

coverage/lcov-report/index.html

MIT

  • TTL Support
  • Purge Support