http-cache

An extensible caching interface for HTTP traffic.

Usage no npm install needed!

<script type="module">
  import httpCache from 'https://cdn.skypack.dev/http-cache';
</script>

README

http-cache

Build Status NPM version Dependency Status

Install

npm install http-cache

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

What is it?

Getting Started with Connect/Express

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);

Getting Started with HTTP

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);

Custom Rules

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 ]
});

Tests & Code Coverage

npm test

Now you can view coverage using any browser here:

coverage/lcov-report/index.html

License

MIT

TODO

  • TTL Support
  • Purge Support