Run cache summary
run cache storage for address summary in background when queried result is incomplete
This commit is contained in:
parent
3982807b32
commit
adb81616aa
@ -357,11 +357,11 @@ AddressService.prototype.getAddressSummary = function(address, options, streamer
|
||||
if(tx) {
|
||||
count++;
|
||||
self._aggregateAddressSummaryResult(tx, address, result, options);
|
||||
result.lastItem = tx.txid();
|
||||
}
|
||||
|
||||
if(count >= MAX_TX_QUERY_LIMIT) {//stop quering db when limit reached
|
||||
options.flag_stop = true;
|
||||
result.lastItem = tx.txid();
|
||||
result.incomplete = true;
|
||||
}
|
||||
|
||||
@ -388,8 +388,12 @@ AddressService.prototype.getAddressSummary = function(address, options, streamer
|
||||
callback(null, result);
|
||||
|
||||
//store in cache if needed
|
||||
if(useCache && result.incomplete)
|
||||
this._storeCache(address, result.lastItem, result);
|
||||
if(useCache) {
|
||||
if(result.incomplete) //full summary needs to be calculated in background
|
||||
self._cacheSummaryInBackground(address, result.lastItem, result);
|
||||
else if (result.lastItem != lastCachedTx)
|
||||
self._storeCache(address, result.lastItem, result);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@ -397,6 +401,57 @@ AddressService.prototype.getAddressSummary = function(address, options, streamer
|
||||
|
||||
}
|
||||
|
||||
AddressService.prototype._cacheSummaryInBackground_runningInstance = new Set();
|
||||
AddressService.prototype._cacheSummaryInBackground = function(address, lastTx, result){
|
||||
const self = this;
|
||||
|
||||
if(self._cacheSummaryInBackground_runningInstance.has(address))
|
||||
return;
|
||||
|
||||
self._cacheSummaryInBackground_runningInstance.add(address);
|
||||
|
||||
const cache = {
|
||||
balanceSat: result.balanceSat,
|
||||
totalReceivedSat: result.totalReceivedSat,
|
||||
totalSentSat: result.totalSentSat,
|
||||
txApperances: result.txApperances,
|
||||
unconfirmedBalanceSat: result.unconfirmedBalanceSat,
|
||||
unconfirmedTxApperances: result.unconfirmedTxApperances
|
||||
};
|
||||
const options = { queryMempool: false, after: lastTx, noTxList: true };
|
||||
var lastItem;
|
||||
|
||||
self._streamAddressSummary(address, options, function(err, tx) {
|
||||
|
||||
if(err)
|
||||
return log.error(err);
|
||||
|
||||
if(tx) {
|
||||
self._aggregateAddressSummaryResult(tx, address, cache, options);
|
||||
lastItem = tx.txid();
|
||||
}
|
||||
|
||||
}, function(err) {
|
||||
|
||||
if (err)
|
||||
return log.error(err);
|
||||
|
||||
cache.balanceSat = parseInt(cache.balanceSat.toFixed());
|
||||
cache.totalReceivedSat = parseInt(cache.totalReceivedSat.toFixed());
|
||||
cache.totalSentSat = parseInt(cache.totalSentSat.toFixed());
|
||||
cache.txApperances = parseInt(cache.txApperances.toFixed());
|
||||
cache.unconfirmedBalanceSat = parseInt(cache.unconfirmedBalanceSat.toFixed());
|
||||
cache.unconfirmedTxApperances = parseInt(cache.unconfirmedTxApperances.toFixed());
|
||||
|
||||
if(_.isUndefined(lastItem))
|
||||
self._storeCache(address, lastItem, cache);
|
||||
|
||||
self._cacheSummaryInBackground_runningInstance.delete(address); //remove from running instance
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
AddressService.prototype._storeCache = function(address, lastCacheTx, result, callback) {
|
||||
var key = self._encoding.encodeAddressCacheKey(address);
|
||||
var value = self._encoding.encodeAddressCacheValue(lastCacheTx, result.balanceSat, result.totalReceivedSat, result.totalSentSat, result.txApperances, result.unconfirmedBalanceSat, result.unconfirmedTxApperances)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user