diff --git a/lib/services/address/index.js b/lib/services/address/index.js index 328e0d9d..fb050397 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -393,8 +393,9 @@ AddressService.prototype.getAddressSummary = function(address, options, streamer 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); + else if (!_.isUndefined(lastCachedTx) && !_.isUndefined(result.lastItem) + && 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){ const self = this; - if(self._cacheSummaryInBackground_runningInstance.has(address)) + if(self._cacheInstance.has(address)) return; - self._cacheSummaryInBackground_runningInstance.add(address); + self._cacheInstance.add(address); const cache = { balanceSat: result.balanceSat, @@ -444,17 +445,18 @@ AddressService.prototype._cacheSummaryInBackground = function(address, lastTx, r cache.totalSentSat = parseInt(cache.totalSentSat.toFixed()); cache.txApperances = parseInt(cache.txApperances.toFixed()); - if(_.isUndefined(lastItem)) + if(!_.isUndefined(lastItem)) 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) { - 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) 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) { + const self = this; if(!useCache) //skip if useCache is false (cases like 'after' parameter is used by client) 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) { return next(err);