cashier

a module for caching static http responses in memory

npm install cashier
4 downloads in the last week
4 downloads in the last month

Cashier

A module for caching static http responses in memory. It's available through npm

npm install cashier

Cashier allows you to cache in memory a result from a function and serve it using http.
It does so by wrapping a function into connect compatible middleware.

Usage

var cashier = require('cashier');
var fs = require('fs');

var serveFiles = cashier(function(url, callback) {
    var filename = '.'+url;

    fs.readFile(filename, function(err, buf) {
        if (err) return callback(err);
        callback(null, {data:buf, watch:[filename]}); // see more options below
    });
});

// let's mount it using express

var express = require('express');
var app = express();

app.use(serveFiles);
app.listen(8080);

If you save the above example as example.js and afterwards run it and visit http://localhost:8080/example.js you should see the file being served.

Cashier is ensuring that we actually only read the file from disk once - any subsequent requests will be served from the cache. Since we pass the watch option Cashier will clear the cache if any of the files in the array changes.

Options

  • data a buffer or a string to be responded (a gzipped version will also be saved)
  • watch a file or array of files to be watched - the cache will clear if they are modified.
  • type the content-type of the response

License

(The MIT License)

Copyright (c) 2012 Mathias Buus Madsen <mathiasbuus@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you