From 22678e38385abcaf6948287eea4e7eb791002fa7 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Tue, 17 Oct 2017 14:23:51 -0400 Subject: [PATCH] Added tx cache. --- errors.txt | 16 ++++++++++++++++ lib/services/transaction/index.js | 19 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 errors.txt diff --git a/errors.txt b/errors.txt new file mode 100644 index 00000000..3f7ba46c --- /dev/null +++ b/errors.txt @@ -0,0 +1,16 @@ +17-10-16T23:06:28.131Z] error: Error: Block hash or address expected +Oct 16 23:06:28 rs-iad-insight-04 bitcored[23072]: at TxController.list (/home/bitcore/.npm-packages/lib/node_modules/bitcore/node_modules/insight-api/lib/transactions.js:321:37) +Oct 16 23:06:28 rs-iad-insight-04 bitcored[23072]: at Layer.handle [as handle_request] (/home/bitcore/.npm-packages/lib/node_modules/bitcore/node_modules/express/lib/router/layer.js:95:5) +Oct 16 23:06:28 rs-iad-insight-04 bitcored[23072]: at next (/home/bitcore/.npm-packages/lib/node_modules/bitcore/node_modules/express/lib/router/route.js:137:13) +Oct 16 23:06:28 rs-iad-insight-04 bitcored[23072]: at /home/bitcore/.npm-packages/lib/node_modules/bitcore/node_modules/insight-api/lib/index.js:73:5 +Oct 16 23:06:28 rs-iad-insight-04 bitcored[23072]: at Layer.handle [as handle_request] (/home/bitcore/.npm-packages/lib/node_modules/bitcore/node_modules/express/lib/router/layer.js:95:5) +Oct 16 23:06:28 rs-iad-insight-04 bitcored[23072]: at next (/home/bitcore/.npm-packages/lib/node_modules/bitcore/node_modules/express/lib/router/route.js:137:13) +Oct 16 23:06:28 rs-iad-insight-04 bitcored[23072]: at Route.dispatch (/home/bitcore/.npm-packages/lib/node_modules/bitcore/node_modules/express/lib/router/route.js:112:3) +Oct 16 23:06:28 rs-iad-insight-04 bitcored[23072]: at Layer.handle [as handle_request] (/home/bitcore/.npm-packages/lib/node_modules/bitcore/node_modules/express/lib/router/layer.js:95:5) +Oct 16 23:06:28 rs-iad-insight-04 bitcored[23072]: at /home/bitcore/.npm-packages/lib/node_modules/bitcore/node_modules/express/lib/router/index.js:281:22 +Oct 16 23:06:28 rs-iad-insight-04 bitcored[23072]: at + + + +17 + diff --git a/lib/services/transaction/index.js b/lib/services/transaction/index.js index 19d0fdec..a6ef3551 100644 --- a/lib/services/transaction/index.js +++ b/lib/services/transaction/index.js @@ -6,6 +6,7 @@ var Encoding = require('./encoding'); var _ = require('lodash'); var async = require('async'); var assert = require('assert'); +var LRU = require('lru-cache'); function TransactionService(options) { BaseService.call(this, options); @@ -24,6 +25,9 @@ function TransactionService(options) { if (this._network === 'regtest') { this._network = 'testnet'; } + + // caches + this._cacheTx = LRU(1000); } inherits(TransactionService, BaseService); @@ -113,12 +117,25 @@ TransactionService.prototype.getTransaction = function(txid, options, callback) callback = options; } + var cacheTx = self._cacheTx.get(txid); + if (cacheTx) { + return callback(null, cacheTx); + } + async.waterfall([ self._getTransaction.bind(self, txid, options), self._getSupplementaryTransactionInfo.bind(self), self._getMempoolTransaction.bind(self), self.setTxMetaInfo.bind(self) - ], callback); + ], function(err, tx) { + if (err) { + return callback(err); + } + if (tx) { + self._cacheTx.set(txid, tx); + } + callback(err, tx); + }); };