blocks: add configurable option for caches

This commit is contained in:
Braydon Fuller 2016-05-09 17:54:57 -04:00
parent b446f6733c
commit 4e54d195b9
3 changed files with 20 additions and 9 deletions

View File

@ -8,13 +8,13 @@ var BN = bitcore.crypto.BN;
var LRU = require('lru-cache');
function BlockController(node) {
function BlockController(options) {
var self = this;
this.node = node;
this.node = options.node;
this.blockSummaryCache = LRU(1000000);
this.blockSummaryCache = LRU(options.blockSummaryCacheSize || BlockController.DEFAULT_BLOCKSUMMARY_CACHE_SIZE);
this.blockCacheConfirmations = 6;
this.blockCache = LRU(1000);
this.blockCache = LRU(options.blockCacheSize || BlockController.DEFAULT_BLOCK_CACHE_SIZE);
this.poolStrings = {};
pools.forEach(function(pool) {
@ -29,6 +29,9 @@ function BlockController(node) {
var BLOCK_LIMIT = 200;
BlockController.DEFAULT_BLOCKSUMMARY_CACHE_SIZE = 1000000;
BlockController.DEFAULT_BLOCK_CACHE_SIZE = 1000;
/**
* Find block by hash ...
*/

View File

@ -46,6 +46,9 @@ var InsightAPI = function(options) {
this.cacheShortSeconds = options.cacheShortSeconds;
this.cacheLongSeconds = options.cacheLongSeconds;
this.blockSummaryCacheSize = options.blockSummaryCacheSize || BlockController.DEFAULT_BLOCKSUMMARY_CACHE_SIZE;
this.blockCacheSize = options.blockCacheSize || BlockController.DEFAULT_BLOCK_CACHE_SIZE;
if (!_.isUndefined(options.routePrefix)) {
this.routePrefix = options.routePrefix;
} else {
@ -131,7 +134,12 @@ InsightAPI.prototype.setupRoutes = function(app) {
});
//Block routes
var blocks = new BlockController(this.node);
var blockOptions = {
node: this.node,
blockSummaryCacheSize: this.blockSummaryCacheSize,
blockCacheSize: this.blockCacheSize
};
var blocks = new BlockController(blockOptions);
app.get('/blocks', this.cacheShort(), blocks.list.bind(blocks));

View File

@ -83,7 +83,7 @@ describe('Blocks', function() {
};
it('block data should be correct', function(done) {
var controller = new BlockController(node);
var controller = new BlockController({node: node});
var req = {};
var res = {};
var next = function() {
@ -110,7 +110,7 @@ describe('Blocks', function() {
}
}
};
var controller = new BlockController(node);
var controller = new BlockController({node: node});
var req = {};
var res = {};
var next = function() {
@ -187,7 +187,7 @@ describe('Blocks', function() {
};
it('should have correct data', function(done) {
var blocks = new BlockController(node);
var blocks = new BlockController({node: node});
var req = {
query: {
@ -219,7 +219,7 @@ describe('Blocks', function() {
};
it('should have correct data', function(done) {
var blocks = new BlockController(node);
var blocks = new BlockController({node: node});
var insight = {
'blockHash': '0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7'