Bug fix: cache summary not working properly

This commit is contained in:
sairajzero 2023-04-19 00:45:44 +05:30
parent f8260541ef
commit 4afb0dfaaa

View File

@ -393,8 +393,9 @@ AddressService.prototype.getAddressSummary = function(address, options, streamer
if(useCache) { if(useCache) {
if(result.incomplete) //full summary needs to be calculated in background if(result.incomplete) //full summary needs to be calculated in background
self._cacheSummaryInBackground(address, result.lastItem, result); self._cacheSummaryInBackground(address, result.lastItem, result);
else if (result.lastItem != lastCachedTx) else if (!_.isUndefined(lastCachedTx) && !_.isUndefined(result.lastItem)
self._storeCache(address, result.lastItem, result); && result.lastItem != lastCachedTx && !self._cacheInstance.has(address)) //update cache if needed
self._storeCache(address, result.lastItem, result);
} }
}); });
@ -403,14 +404,14 @@ AddressService.prototype.getAddressSummary = function(address, options, streamer
} }
AddressService.prototype._cacheSummaryInBackground_runningInstance = new Set(); AddressService.prototype._cacheInstance = new Set();
AddressService.prototype._cacheSummaryInBackground = function(address, lastTx, result){ AddressService.prototype._cacheSummaryInBackground = function(address, lastTx, result){
const self = this; const self = this;
if(self._cacheSummaryInBackground_runningInstance.has(address)) if(self._cacheInstance.has(address))
return; return;
self._cacheSummaryInBackground_runningInstance.add(address); self._cacheInstance.add(address);
const cache = { const cache = {
balanceSat: result.balanceSat, balanceSat: result.balanceSat,
@ -444,17 +445,18 @@ AddressService.prototype._cacheSummaryInBackground = function(address, lastTx, r
cache.totalSentSat = parseInt(cache.totalSentSat.toFixed()); cache.totalSentSat = parseInt(cache.totalSentSat.toFixed());
cache.txApperances = parseInt(cache.txApperances.toFixed()); cache.txApperances = parseInt(cache.txApperances.toFixed());
if(_.isUndefined(lastItem)) if(!_.isUndefined(lastItem))
self._storeCache(address, lastItem, cache); self._storeCache(address, lastItem, cache);
self._cacheSummaryInBackground_runningInstance.delete(address); //remove from running instance self._cacheInstance.delete(address); //remove from running instance
}); });
} }
AddressService.prototype._storeCache = function(address, lastCacheTx, result, callback) { AddressService.prototype._storeCache = function(address, lastCacheTx, result, callback) {
var key = self._encoding.encodeAddressCacheKey(address); const self = this;
var key = self._encoding.encodeAddressCacheKey(address);
var value = self._encoding.encodeAddressCacheValue(lastCacheTx, result.balanceSat, result.totalReceivedSat, result.totalSentSat, result.txApperances) var value = self._encoding.encodeAddressCacheValue(lastCacheTx, result.balanceSat, result.totalReceivedSat, result.totalSentSat, result.txApperances)
if(!_.isFunction(callback)) //if callback is not passed, call a empty function if(!_.isFunction(callback)) //if callback is not passed, call a empty function
@ -464,11 +466,13 @@ AddressService.prototype._storeCache = function(address, lastCacheTx, result, ca
} }
AddressService.prototype._loadCache = function(address, result, useCache, next) { AddressService.prototype._loadCache = function(address, result, useCache, next) {
const self = this;
if(!useCache) //skip if useCache is false (cases like 'after' parameter is used by client) if(!useCache) //skip if useCache is false (cases like 'after' parameter is used by client)
next(); next();
self._db.get(self._encoding.encodeAddressCacheKey(address), function(err, value) { var key = self._encoding.encodeAddressCacheKey(address);
self._db.get(key, function(err, value) {
if (err) { if (err) {
return next(err); return next(err);