@nebulaaa/lru 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lru.cjs +25 -25
- package/dist/lru.d.ts +4 -4
- package/dist/lru.min.cjs +1 -1
- package/dist/lru.min.mjs +1 -1
- package/dist/lru.mjs +25 -25
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/dist/lru.cjs
CHANGED
|
@@ -27,7 +27,7 @@ class LRU {
|
|
|
27
27
|
}
|
|
28
28
|
// 移动到头部(标记为最近使用)
|
|
29
29
|
this.moveToHead(node);
|
|
30
|
-
return node.
|
|
30
|
+
return node.Value;
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
33
|
* 读取缓存值,但不提升新鲜度(不改变淘汰顺序)
|
|
@@ -35,7 +35,7 @@ class LRU {
|
|
|
35
35
|
*/
|
|
36
36
|
Peek(key) {
|
|
37
37
|
const node = this._cache.get(key);
|
|
38
|
-
return node ? node.
|
|
38
|
+
return node ? node.Value : null;
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
41
|
* 读取缓存值,未命中时用 creator 计算并写入缓存(写入视为最近使用)
|
|
@@ -45,7 +45,7 @@ class LRU {
|
|
|
45
45
|
const node = this._cache.get(key);
|
|
46
46
|
if (node) {
|
|
47
47
|
this.moveToHead(node);
|
|
48
|
-
return node.
|
|
48
|
+
return node.Value;
|
|
49
49
|
}
|
|
50
50
|
const value = creator(key);
|
|
51
51
|
this.Set(key, value);
|
|
@@ -58,16 +58,16 @@ class LRU {
|
|
|
58
58
|
const existing = this._cache.get(key);
|
|
59
59
|
if (existing) {
|
|
60
60
|
// 更新现有节点
|
|
61
|
-
existing.
|
|
61
|
+
existing.Value = value;
|
|
62
62
|
this.moveToHead(existing);
|
|
63
63
|
return;
|
|
64
64
|
}
|
|
65
65
|
// 创建新节点
|
|
66
66
|
const node = {
|
|
67
|
-
key,
|
|
68
|
-
value,
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
Key: key,
|
|
68
|
+
Value: value,
|
|
69
|
+
Prev: null,
|
|
70
|
+
Next: null,
|
|
71
71
|
};
|
|
72
72
|
// 添加到缓存
|
|
73
73
|
this._cache.set(key, node);
|
|
@@ -119,10 +119,10 @@ class LRU {
|
|
|
119
119
|
* 添加节点到头部
|
|
120
120
|
*/
|
|
121
121
|
addToHead(node) {
|
|
122
|
-
node.
|
|
123
|
-
node.
|
|
122
|
+
node.Prev = null;
|
|
123
|
+
node.Next = this._head;
|
|
124
124
|
if (this._head) {
|
|
125
|
-
this._head.
|
|
125
|
+
this._head.Prev = node;
|
|
126
126
|
}
|
|
127
127
|
this._head = node;
|
|
128
128
|
if (!this._tail) {
|
|
@@ -142,17 +142,17 @@ class LRU {
|
|
|
142
142
|
* 移除节点
|
|
143
143
|
*/
|
|
144
144
|
removeNode(node) {
|
|
145
|
-
if (node.
|
|
146
|
-
node.
|
|
145
|
+
if (node.Prev) {
|
|
146
|
+
node.Prev.Next = node.Next;
|
|
147
147
|
}
|
|
148
148
|
else {
|
|
149
|
-
this._head = node.
|
|
149
|
+
this._head = node.Next;
|
|
150
150
|
}
|
|
151
|
-
if (node.
|
|
152
|
-
node.
|
|
151
|
+
if (node.Next) {
|
|
152
|
+
node.Next.Prev = node.Prev;
|
|
153
153
|
}
|
|
154
154
|
else {
|
|
155
|
-
this._tail = node.
|
|
155
|
+
this._tail = node.Prev;
|
|
156
156
|
}
|
|
157
157
|
}
|
|
158
158
|
/**
|
|
@@ -162,7 +162,7 @@ class LRU {
|
|
|
162
162
|
if (!this._tail) {
|
|
163
163
|
return;
|
|
164
164
|
}
|
|
165
|
-
this._cache.delete(this._tail.
|
|
165
|
+
this._cache.delete(this._tail.Key);
|
|
166
166
|
this.removeNode(this._tail);
|
|
167
167
|
}
|
|
168
168
|
/**
|
|
@@ -172,8 +172,8 @@ class LRU {
|
|
|
172
172
|
const result = [];
|
|
173
173
|
let node = this._head;
|
|
174
174
|
while (node) {
|
|
175
|
-
result.push(node.
|
|
176
|
-
node = node.
|
|
175
|
+
result.push(node.Key);
|
|
176
|
+
node = node.Next;
|
|
177
177
|
}
|
|
178
178
|
return result;
|
|
179
179
|
}
|
|
@@ -184,8 +184,8 @@ class LRU {
|
|
|
184
184
|
const result = [];
|
|
185
185
|
let node = this._head;
|
|
186
186
|
while (node) {
|
|
187
|
-
result.push(node.
|
|
188
|
-
node = node.
|
|
187
|
+
result.push(node.Value);
|
|
188
|
+
node = node.Next;
|
|
189
189
|
}
|
|
190
190
|
return result;
|
|
191
191
|
}
|
|
@@ -199,12 +199,12 @@ class LRU {
|
|
|
199
199
|
let node = this._head;
|
|
200
200
|
while (node) {
|
|
201
201
|
snapshot.push(node);
|
|
202
|
-
node = node.
|
|
202
|
+
node = node.Next;
|
|
203
203
|
}
|
|
204
204
|
for (const item of snapshot) {
|
|
205
205
|
// 节点身份校验:Set 更新已有键不会更换节点,仍会回调;Delete 后重设的同键新节点则跳过
|
|
206
|
-
if (this._cache.get(item.
|
|
207
|
-
callback(item.
|
|
206
|
+
if (this._cache.get(item.Key) === item) {
|
|
207
|
+
callback(item.Value, item.Key);
|
|
208
208
|
}
|
|
209
209
|
}
|
|
210
210
|
}
|
package/dist/lru.d.ts
CHANGED
package/dist/lru.min.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";exports.LRU=class{constructor(e){if(!Number.isInteger(e)||e<=0)throw new Error("LRU capacity must be an integer greater than 0");this._capacity=e,this._cache=new Map,this._head=null,this._tail=null}Get(e){const t=this._cache.get(e);return t?(this.moveToHead(t),t.
|
|
1
|
+
"use strict";exports.LRU=class{constructor(e){if(!Number.isInteger(e)||e<=0)throw new Error("LRU capacity must be an integer greater than 0");this._capacity=e,this._cache=new Map,this._head=null,this._tail=null}Get(e){const t=this._cache.get(e);return t?(this.moveToHead(t),t.Value):null}Peek(e){const t=this._cache.get(e);return t?t.Value:null}GetOrSet(e,t){const h=this._cache.get(e);if(h)return this.moveToHead(h),h.Value;const s=t(e);return this.Set(e,s),s}Set(e,t){const h=this._cache.get(e);if(h)return h.Value=t,void this.moveToHead(h);const s={Key:e,Value:t,Prev:null,Next:null};this._cache.set(e,s),this.addToHead(s),this._cache.size>this._capacity&&this.removeTail()}Has(e){return this._cache.has(e)}Delete(e){const t=this._cache.get(e);return!!t&&(this._cache.delete(e),this.removeNode(t),!0)}Clear(){this._cache.clear(),this._head=null,this._tail=null}get Size(){return this._cache.size}get Capacity(){return this._capacity}addToHead(e){e.Prev=null,e.Next=this._head,this._head&&(this._head.Prev=e),this._head=e,this._tail||(this._tail=e)}moveToHead(e){this.removeNode(e),this.addToHead(e)}removeNode(e){e.Prev?e.Prev.Next=e.Next:this._head=e.Next,e.Next?e.Next.Prev=e.Prev:this._tail=e.Prev}removeTail(){this._tail&&(this._cache.delete(this._tail.Key),this.removeNode(this._tail))}Keys(){const e=[];let t=this._head;for(;t;)e.push(t.Key),t=t.Next;return e}Values(){const e=[];let t=this._head;for(;t;)e.push(t.Value),t=t.Next;return e}Each(e){const t=[];let h=this._head;for(;h;)t.push(h),h=h.Next;for(const h of t)this._cache.get(h.Key)===h&&e(h.Value,h.Key)}};
|
package/dist/lru.min.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class e{constructor(e){if(!Number.isInteger(e)||e<=0)throw new Error("LRU capacity must be an integer greater than 0");this._capacity=e,this._cache=new Map,this._head=null,this._tail=null}Get(e){const t=this._cache.get(e);return t?(this.moveToHead(t),t.
|
|
1
|
+
class e{constructor(e){if(!Number.isInteger(e)||e<=0)throw new Error("LRU capacity must be an integer greater than 0");this._capacity=e,this._cache=new Map,this._head=null,this._tail=null}Get(e){const t=this._cache.get(e);return t?(this.moveToHead(t),t.Value):null}Peek(e){const t=this._cache.get(e);return t?t.Value:null}GetOrSet(e,t){const h=this._cache.get(e);if(h)return this.moveToHead(h),h.Value;const a=t(e);return this.Set(e,a),a}Set(e,t){const h=this._cache.get(e);if(h)return h.Value=t,void this.moveToHead(h);const a={Key:e,Value:t,Prev:null,Next:null};this._cache.set(e,a),this.addToHead(a),this._cache.size>this._capacity&&this.removeTail()}Has(e){return this._cache.has(e)}Delete(e){const t=this._cache.get(e);return!!t&&(this._cache.delete(e),this.removeNode(t),!0)}Clear(){this._cache.clear(),this._head=null,this._tail=null}get Size(){return this._cache.size}get Capacity(){return this._capacity}addToHead(e){e.Prev=null,e.Next=this._head,this._head&&(this._head.Prev=e),this._head=e,this._tail||(this._tail=e)}moveToHead(e){this.removeNode(e),this.addToHead(e)}removeNode(e){e.Prev?e.Prev.Next=e.Next:this._head=e.Next,e.Next?e.Next.Prev=e.Prev:this._tail=e.Prev}removeTail(){this._tail&&(this._cache.delete(this._tail.Key),this.removeNode(this._tail))}Keys(){const e=[];let t=this._head;for(;t;)e.push(t.Key),t=t.Next;return e}Values(){const e=[];let t=this._head;for(;t;)e.push(t.Value),t=t.Next;return e}Each(e){const t=[];let h=this._head;for(;h;)t.push(h),h=h.Next;for(const h of t)this._cache.get(h.Key)===h&&e(h.Value,h.Key)}}export{e as LRU};
|
package/dist/lru.mjs
CHANGED
|
@@ -25,7 +25,7 @@ class LRU {
|
|
|
25
25
|
}
|
|
26
26
|
// 移动到头部(标记为最近使用)
|
|
27
27
|
this.moveToHead(node);
|
|
28
|
-
return node.
|
|
28
|
+
return node.Value;
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
31
|
* 读取缓存值,但不提升新鲜度(不改变淘汰顺序)
|
|
@@ -33,7 +33,7 @@ class LRU {
|
|
|
33
33
|
*/
|
|
34
34
|
Peek(key) {
|
|
35
35
|
const node = this._cache.get(key);
|
|
36
|
-
return node ? node.
|
|
36
|
+
return node ? node.Value : null;
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
39
|
* 读取缓存值,未命中时用 creator 计算并写入缓存(写入视为最近使用)
|
|
@@ -43,7 +43,7 @@ class LRU {
|
|
|
43
43
|
const node = this._cache.get(key);
|
|
44
44
|
if (node) {
|
|
45
45
|
this.moveToHead(node);
|
|
46
|
-
return node.
|
|
46
|
+
return node.Value;
|
|
47
47
|
}
|
|
48
48
|
const value = creator(key);
|
|
49
49
|
this.Set(key, value);
|
|
@@ -56,16 +56,16 @@ class LRU {
|
|
|
56
56
|
const existing = this._cache.get(key);
|
|
57
57
|
if (existing) {
|
|
58
58
|
// 更新现有节点
|
|
59
|
-
existing.
|
|
59
|
+
existing.Value = value;
|
|
60
60
|
this.moveToHead(existing);
|
|
61
61
|
return;
|
|
62
62
|
}
|
|
63
63
|
// 创建新节点
|
|
64
64
|
const node = {
|
|
65
|
-
key,
|
|
66
|
-
value,
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
Key: key,
|
|
66
|
+
Value: value,
|
|
67
|
+
Prev: null,
|
|
68
|
+
Next: null,
|
|
69
69
|
};
|
|
70
70
|
// 添加到缓存
|
|
71
71
|
this._cache.set(key, node);
|
|
@@ -117,10 +117,10 @@ class LRU {
|
|
|
117
117
|
* 添加节点到头部
|
|
118
118
|
*/
|
|
119
119
|
addToHead(node) {
|
|
120
|
-
node.
|
|
121
|
-
node.
|
|
120
|
+
node.Prev = null;
|
|
121
|
+
node.Next = this._head;
|
|
122
122
|
if (this._head) {
|
|
123
|
-
this._head.
|
|
123
|
+
this._head.Prev = node;
|
|
124
124
|
}
|
|
125
125
|
this._head = node;
|
|
126
126
|
if (!this._tail) {
|
|
@@ -140,17 +140,17 @@ class LRU {
|
|
|
140
140
|
* 移除节点
|
|
141
141
|
*/
|
|
142
142
|
removeNode(node) {
|
|
143
|
-
if (node.
|
|
144
|
-
node.
|
|
143
|
+
if (node.Prev) {
|
|
144
|
+
node.Prev.Next = node.Next;
|
|
145
145
|
}
|
|
146
146
|
else {
|
|
147
|
-
this._head = node.
|
|
147
|
+
this._head = node.Next;
|
|
148
148
|
}
|
|
149
|
-
if (node.
|
|
150
|
-
node.
|
|
149
|
+
if (node.Next) {
|
|
150
|
+
node.Next.Prev = node.Prev;
|
|
151
151
|
}
|
|
152
152
|
else {
|
|
153
|
-
this._tail = node.
|
|
153
|
+
this._tail = node.Prev;
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
156
|
/**
|
|
@@ -160,7 +160,7 @@ class LRU {
|
|
|
160
160
|
if (!this._tail) {
|
|
161
161
|
return;
|
|
162
162
|
}
|
|
163
|
-
this._cache.delete(this._tail.
|
|
163
|
+
this._cache.delete(this._tail.Key);
|
|
164
164
|
this.removeNode(this._tail);
|
|
165
165
|
}
|
|
166
166
|
/**
|
|
@@ -170,8 +170,8 @@ class LRU {
|
|
|
170
170
|
const result = [];
|
|
171
171
|
let node = this._head;
|
|
172
172
|
while (node) {
|
|
173
|
-
result.push(node.
|
|
174
|
-
node = node.
|
|
173
|
+
result.push(node.Key);
|
|
174
|
+
node = node.Next;
|
|
175
175
|
}
|
|
176
176
|
return result;
|
|
177
177
|
}
|
|
@@ -182,8 +182,8 @@ class LRU {
|
|
|
182
182
|
const result = [];
|
|
183
183
|
let node = this._head;
|
|
184
184
|
while (node) {
|
|
185
|
-
result.push(node.
|
|
186
|
-
node = node.
|
|
185
|
+
result.push(node.Value);
|
|
186
|
+
node = node.Next;
|
|
187
187
|
}
|
|
188
188
|
return result;
|
|
189
189
|
}
|
|
@@ -197,12 +197,12 @@ class LRU {
|
|
|
197
197
|
let node = this._head;
|
|
198
198
|
while (node) {
|
|
199
199
|
snapshot.push(node);
|
|
200
|
-
node = node.
|
|
200
|
+
node = node.Next;
|
|
201
201
|
}
|
|
202
202
|
for (const item of snapshot) {
|
|
203
203
|
// 节点身份校验:Set 更新已有键不会更换节点,仍会回调;Delete 后重设的同键新节点则跳过
|
|
204
|
-
if (this._cache.get(item.
|
|
205
|
-
callback(item.
|
|
204
|
+
if (this._cache.get(item.Key) === item) {
|
|
205
|
+
callback(item.Value, item.Key);
|
|
206
206
|
}
|
|
207
207
|
}
|
|
208
208
|
}
|
package/dist/package.json
CHANGED