Skip to content

Commit

Permalink
Merge pull request #496 from mikeal/cache2-pull
Browse files Browse the repository at this point in the history
Distributable cache for files
  • Loading branch information
Eran Hammer committed Feb 10, 2013
2 parents 0c91eca + 8e2acb8 commit ad51166
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
23 changes: 20 additions & 3 deletions lib/response/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ var Mime = require('mime');
var Err = require('../error');
var Utils = require('../utils');
var Stream = require('./stream');
var LRU = require('lru-cache');
var Crypto = require('crypto');


// Declare internals

var internals = {};

internals.fileEtags = LRU();

// File response (Base -> Generic -> Stream -> File)

Expand Down Expand Up @@ -49,8 +51,23 @@ internals.File.prototype._prepare = function (request, callback) {
Stream.call(self, stream);
self._headers['Content-Type'] = Mime.lookup(self._filePath) || 'application/octet-stream';
self._headers['Content-Length'] = stat.size;
self._headers['Last-Modified'] = new Date(stat.mtime).toUTCString();
self._headers.etag = JSON.stringify([stat.ino, stat.size, Date.parse(stat.mtime)].join('-'));

// Use stat info for an LRU cache key.
var cachekey = [self._filePath, stat.ino, stat.size, Date.parse(stat.mtime)].join('-');

// The etag must hash the file contents in order to be consistent between nodes.
if (internals.fileEtags.has(cachekey)) {
self._headers.etag = JSON.stringify(internals.fileEtags.get(cachekey));
} else {
var hash = Crypto.createHash('md5');
stream.on('data', function (chunk) {
hash.update(chunk);
})
stream.on('end', function () {
internals.fileEtags.set(cachekey, hash.digest("hex"));
})
}

self._headers['Content-Disposition'] = 'inline; filename=' + encodeURIComponent(fileName);

return Stream.prototype._prepare.call(self, request, callback);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"catbox": "0.1.x",
"cryptiles": "0.0.x",
"iron": "0.1.x",
"semver": "1.1.0"
"semver": "1.1.0",
"lru-cache": "~2.2.2"
},
"devDependencies": {
"mocha": "1.x.x",
Expand Down
17 changes: 0 additions & 17 deletions test/integration/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,23 +546,6 @@ describe('Response', function () {
});
});

it('returns a 304 when the request has a future modifed-since', function (done) {

server.start(function () {

var date = new Date(Date.now());
var headers = {
'if-modified-since': new Date(date.setFullYear(date.getFullYear() + 1)).toUTCString()
};

Request.get({ url: 'http://localhost:17082/file', headers: headers }, function (err, res) {

expect(res.statusCode).to.equal(304);
done();
});
});
});

it('returns a gzipped file in the response when the request accepts gzip', function (done) {

server.start(function () {
Expand Down

0 comments on commit ad51166

Please sign in to comment.