@zcorky/lru
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -7,3 +7,9 @@ export interface Option { | ||
set(key: K, value: T, option?: Option): void; | ||
hits(): Hits; | ||
hasKey(key: K): boolean; | ||
} | ||
export interface Hits { | ||
count: number; | ||
rate: number; | ||
} | ||
export interface Item<T> { | ||
@@ -18,2 +24,4 @@ expired?: number; | ||
private _cache; | ||
private _hitCount; | ||
private _missCount; | ||
constructor(limit?: number); | ||
@@ -24,3 +32,8 @@ static create<K, T>(limit: number): lru<K, T>; | ||
private _updateCache; | ||
hasKey(key: K): boolean; | ||
hits(): { | ||
count: number; | ||
rate: number; | ||
}; | ||
} | ||
export default lru; |
@@ -13,2 +13,4 @@ "use strict"; | ||
this._cache = new Map(); | ||
this._hitCount = 0; | ||
this._missCount = 0; | ||
} | ||
@@ -23,2 +25,3 @@ lru.create = function (limit) { | ||
if (item.expired && getNow() > item.expired) { | ||
this._missCount++; | ||
item.expired = 0; | ||
@@ -28,2 +31,3 @@ item.value = undefined; | ||
else { | ||
this._hitCount++; | ||
if (maxAge !== undefined) { | ||
@@ -39,2 +43,3 @@ var expired = maxAge ? getNow() + maxAge : 0; | ||
if (item.expired && getNow() > item.expired) { | ||
this._missCount++; | ||
item.expired = 0; | ||
@@ -44,2 +49,3 @@ item.value = undefined; | ||
else { | ||
this._hitCount++; | ||
this._updateCache(key, item); | ||
@@ -53,2 +59,3 @@ if (maxAge !== undefined) { | ||
} | ||
this._missCount++; | ||
}; | ||
@@ -77,2 +84,11 @@ lru.prototype.set = function (key, value, option) { | ||
}; | ||
lru.prototype.hasKey = function (key) { | ||
return this.cache.has(key) || this._cache.has(key); | ||
}; | ||
lru.prototype.hits = function () { | ||
return { | ||
count: this._hitCount, | ||
rate: this._hitCount / ((this._hitCount + this._missCount) || 1), | ||
}; | ||
}; | ||
return lru; | ||
@@ -79,0 +95,0 @@ }()); |
{ | ||
"name": "@zcorky/lru", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A simple lru", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
6331
126