Connect
respondFromCache()
Respond with the provided cached value.
TODO: Assume 200 code, that's iffy.
Source
function respondFromCache(req, res, cacheEntry) {
var status = cacheEntry[0]
, headers = utils.merge({}, cacheEntry[1])
, content = cacheEntry.slice(2);
headers.age = (new Date - new Date(headers.date)) / 1000 || 0;
switch (req.method) {
case 'HEAD':
res.writeHead(status, headers);
res.end();
break;
case 'GET':
if (utils.conditionalGET(req) && fresh(req.headers, headers)) {
headers['content-length'] = 0;
res.writeHead(304, headers);
res.end();
} else {
res.writeHead(status, headers);
function write() {
while (content.length) {
if (false === res.write(content.shift())) {
res.once('drain', write);
return;
}
}
res.end();
}
write();
}
break;
default:
// This should never happen.
res.writeHead(500, '');
res.end();
}
}
mustRevalidate()
Determine whether or not a cached value must be revalidated.
Source
function mustRevalidate(req, cacheEntry) {
var cacheHeaders = cacheEntry[1]
, reqCC = utils.parseCacheControl(req.headers['cache-control'] || '')
, cacheCC = utils.parseCacheControl(cacheHeaders['cache-control'] || '')
, cacheAge = (new Date - new Date(cacheHeaders.date)) / 1000 || 0;
if ( cacheCC['no-cache']
|| cacheCC['must-revalidate']
|| cacheCC['proxy-revalidate']) return true;
if (reqCC['no-cache']) return true;
if (null != reqCC['max-age']) return reqCC['max-age'] < cacheAge;
if (null != cacheCC['max-age']) return cacheCC['max-age'] < cacheAge;
return false;
}
cacheKey()
The key to use in the cache. For now, this is the URL path and query.
'http://example.com?key=value' -> '/?key=value'
Source
function cacheKey(req) {
return utils.parseUrl(req).path;
}
Static cache:
Status: Deprecated. This middleware will be removed in
Connect 3.0. You may be interested in:
Enables a memory cache layer on top of
the
static()
middleware, serving popularstatic files.
By default a maximum of 128 objects are
held in cache, with a max of 256k each,
totalling ~32mb.
A Least-Recently-Used (LRU) cache algo
is implemented through the
Cache
object,simply rotating cache objects as they are
hit. This means that increasingly popular
objects maintain their positions while
others get shoved out of the stack and
garbage collected.
Benchmarks:
Options:
maxObjects
max cache objects [128]maxLength
max cache object length 256kbSource