bitcore-lib-zcash/lib/hdkeycache.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2014-11-28 07:24:32 -08:00
'use strict';
module.exports = {
2014-11-30 14:08:21 -08:00
_cache: {},
_count: 0,
_eraseIndex: 0,
_usedList: {},
_usedIndex: {},
_CACHE_SIZE: 5000,
2014-11-28 07:24:32 -08:00
get: function(xkey, number, hardened) {
2014-11-30 14:08:21 -08:00
hardened = !!hardened;
2014-11-28 07:24:32 -08:00
var key = xkey + '/' + number + '/' + hardened;
2014-11-30 14:08:21 -08:00
if (this._cache[key]) {
this._cacheHit(key);
return this._cache[key];
2014-11-28 07:24:32 -08:00
}
},
set: function(xkey, number, hardened, derived) {
2014-11-30 14:08:21 -08:00
hardened = !!hardened;
2014-11-28 07:24:32 -08:00
var key = xkey + '/' + number + '/' + hardened;
2014-11-30 14:08:21 -08:00
this._cache[key] = derived;
this._cacheHit(key);
},
_cacheHit: function(key) {
if (this._usedIndex[key]) {
delete this._usedList[this._usedIndex[key]];
}
this._usedList[this._count] = key;
this._usedIndex[key] = this._count;
this._count++;
this._cacheRemove();
},
_cacheRemove: function() {
while (this._eraseIndex < this._count - this._CACHE_SIZE) {
if (this._usedList[this._eraseIndex]) {
var removeKey = this._usedList[this._eraseIndex];
delete this._usedIndex[removeKey];
delete this._cache[removeKey];
}
delete this._usedList[this._eraseIndex];
this._eraseIndex++;
}
2014-11-28 07:24:32 -08:00
}
};