Avoid potential crash on cache

This commit is contained in:
Thaddee Tyl 2014-10-31 23:50:27 +01:00
parent 9fd6335db8
commit ef7c63ac95
2 changed files with 11 additions and 9 deletions

View File

@ -63,8 +63,8 @@ Cache.prototype = {
return Math.max(0, (this.order.length - this.size));
} else if (this.type === typeEnum.heap) {
if (getHeapSize() >= this.size) {
// Remove a quarter of them.
return (this.order.length >> 2);
// Remove half of them.
return (this.order.length >> 1);
} else { return 0; }
} else {
console.error("Unknown heuristic for LRU cache.");

View File

@ -147,8 +147,8 @@ var minAccuracy = 0.75;
// = 1 - max(1, df) / rf
var freqRatioMax = 1 - minAccuracy;
// Request cache size of 500MB heap limit.
var requestCache = new LruCache(500000000, 'heap');
// Request cache size of 400MB heap limit.
var requestCache = new LruCache(400000000, 'heap');
// Deep error handling for vendor hooks.
var vendorDomain = domain.create();
@ -219,11 +219,13 @@ function cache(f) {
options = uri;
}
return request(options, function(err, res, json) {
var cacheControl = res.headers['cache-control'];
if (cacheControl != null) {
var age = cacheControl.match(/max-age=([0-9]+)/);
if ((age != null) && (+age[1] === +age[1])) {
cacheInterval = +age[1] * 1000;
if (res != null && res.headers != null) {
var cacheControl = res.headers['cache-control'];
if (cacheControl != null) {
var age = cacheControl.match(/max-age=([0-9]+)/);
if ((age != null) && (+age[1] === +age[1])) {
cacheInterval = +age[1] * 1000;
}
}
}
callback(err, res, json);