Skip to content

Commit bcfb377

Browse files
committed
LRUResize method added to resize the LRU size.
1 parent 023a952 commit bcfb377

File tree

7 files changed

+214
-5
lines changed

7 files changed

+214
-5
lines changed

dist/js/dyCache.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dycachejs",
3-
"version": "1.6.3",
3+
"version": "1.6.4",
44
"description": "Cache data using JavaScript in the browser.",
55
"main": "index.html",
66
"scripts": {

src/js/dyCache.forTest.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,48 @@ var dyCache = /** @class */ (function () {
865865
dyCache.prototype.LRUExists = function (name) {
866866
return this.exists(name);
867867
};
868+
/**
869+
* This will resize the LRU referred by 'name' in the cache.
870+
*
871+
* If new 'size' is less than current size then least recently used
872+
* key(s) and key-value pair(s) is/are removed from the data set and
873+
* the queue.
874+
*
875+
* If 'size' is not mentioned then LRU size is set to 3.
876+
*
877+
* Returns true on success, false otherwise.
878+
*
879+
* @param {string} name
880+
* @param {number} size
881+
* @returns {boolean}
882+
* @constructor
883+
*/
884+
dyCache.prototype.LRUResize = function (name, size) {
885+
var _this = this;
886+
if (size === void 0) { size = 3; }
887+
// return false if LRU referred by 'name'
888+
// does not exists
889+
if (!this.exists(name)) {
890+
return false;
891+
}
892+
// if new 'size' is greater than or equal to current size
893+
// then update size and return true
894+
if (size >= this._cache[name]._size) {
895+
this._cache[name]._size = size;
896+
return true;
897+
}
898+
else {
899+
// get least recently used key(s) from the queue
900+
var leastRecentlyUsedKeys = this._cache[name]._queue.splice(size);
901+
// remove least recently used key-value pair(s) from the data set
902+
leastRecentlyUsedKeys.forEach(function (key) {
903+
delete _this._cache[name]._data[key];
904+
});
905+
// update LRU object size
906+
this._cache[name]._size = size;
907+
}
908+
return true;
909+
};
868910
return dyCache;
869911
}());
870912

0 commit comments

Comments
 (0)