fast-ttl-cache 0.0.6 → 0.0.7
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/index.js +38 -39
- package/dist/index.mjs +38 -39
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -19,10 +19,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
// src/index.mjs
|
|
20
20
|
var index_exports = {};
|
|
21
21
|
__export(index_exports, {
|
|
22
|
-
default: () =>
|
|
22
|
+
default: () => FastTTLCache
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(index_exports);
|
|
25
|
-
var
|
|
25
|
+
var FastTTLCache = class {
|
|
26
26
|
/**
|
|
27
27
|
* 构造函数
|
|
28
28
|
* @param options 配置选项,包含ttl(过期时间)和capacity(容量)
|
|
@@ -64,7 +64,7 @@ var TTLCache = class {
|
|
|
64
64
|
const item = this.store.get(key);
|
|
65
65
|
if (!item) return null;
|
|
66
66
|
if (Date.now() - item.time > this.ttl) {
|
|
67
|
-
this.del(
|
|
67
|
+
this.del(item);
|
|
68
68
|
return null;
|
|
69
69
|
}
|
|
70
70
|
return item.value;
|
|
@@ -76,74 +76,73 @@ var TTLCache = class {
|
|
|
76
76
|
*/
|
|
77
77
|
put(key, value) {
|
|
78
78
|
if (this.size === 0) {
|
|
79
|
-
|
|
79
|
+
const item2 = {
|
|
80
80
|
key,
|
|
81
81
|
value,
|
|
82
|
-
|
|
82
|
+
prev: null,
|
|
83
83
|
next: null,
|
|
84
84
|
time: Date.now()
|
|
85
|
-
}
|
|
86
|
-
this.head = this.tail =
|
|
85
|
+
};
|
|
86
|
+
this.head = this.tail = item2;
|
|
87
|
+
this.store.set(key, item2);
|
|
87
88
|
return;
|
|
88
89
|
}
|
|
89
90
|
if (this.store.has(key)) {
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
this.moveToTail(
|
|
91
|
+
const item2 = this.store.get(key);
|
|
92
|
+
item2.value = value;
|
|
93
|
+
item2.time = Date.now();
|
|
94
|
+
this.moveToTail(item2);
|
|
94
95
|
return;
|
|
95
96
|
}
|
|
96
|
-
const
|
|
97
|
-
const newItem = {
|
|
97
|
+
const item = {
|
|
98
98
|
key,
|
|
99
99
|
value,
|
|
100
|
-
|
|
100
|
+
prev: this.tail,
|
|
101
101
|
next: null,
|
|
102
102
|
time: Date.now()
|
|
103
103
|
};
|
|
104
|
-
|
|
105
|
-
this.
|
|
106
|
-
this.
|
|
104
|
+
this.tail.next = item;
|
|
105
|
+
this.tail = item;
|
|
106
|
+
this.store.set(key, item);
|
|
107
107
|
if (this.size > this.capacity) {
|
|
108
108
|
this.del(this.head);
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
112
|
* 移除节点
|
|
113
|
-
* @param
|
|
113
|
+
* @param item
|
|
114
114
|
*/
|
|
115
|
-
del(
|
|
115
|
+
del(item) {
|
|
116
|
+
const key = item.key;
|
|
116
117
|
if (!this.store.has(key)) return false;
|
|
117
|
-
const curItem = this.store.get(key);
|
|
118
118
|
if (this.size === 1) {
|
|
119
119
|
this.head = this.tail = null;
|
|
120
|
-
} else if (this.head ===
|
|
121
|
-
this.head =
|
|
122
|
-
|
|
123
|
-
} else if (this.tail ===
|
|
124
|
-
this.tail =
|
|
125
|
-
|
|
120
|
+
} else if (this.head === item) {
|
|
121
|
+
this.head = item.next;
|
|
122
|
+
this.head.prev = null;
|
|
123
|
+
} else if (this.tail === item) {
|
|
124
|
+
this.tail = item.prev;
|
|
125
|
+
this.tail.next = null;
|
|
126
126
|
} else {
|
|
127
|
-
|
|
128
|
-
|
|
127
|
+
item.prev.next = item.next;
|
|
128
|
+
item.next.prev = item.prev;
|
|
129
129
|
}
|
|
130
130
|
this.store.delete(key);
|
|
131
131
|
return true;
|
|
132
132
|
}
|
|
133
133
|
/**
|
|
134
134
|
* 将节点移动到队尾,队尾的节点一定是最后一个更新的
|
|
135
|
-
* @param
|
|
135
|
+
* @param item
|
|
136
136
|
*/
|
|
137
|
-
moveToTail(
|
|
137
|
+
moveToTail(item) {
|
|
138
|
+
const key = item.key;
|
|
138
139
|
if (!this.store.has(key)) return;
|
|
139
|
-
if (this.tail ===
|
|
140
|
-
|
|
141
|
-
this.
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
this.tail = key;
|
|
147
|
-
this.store.set(key, curItem);
|
|
140
|
+
if (this.tail === item) return;
|
|
141
|
+
this.del(item);
|
|
142
|
+
this.tail.next = item;
|
|
143
|
+
item.prev = this.tail;
|
|
144
|
+
item.next = null;
|
|
145
|
+
this.tail = item;
|
|
146
|
+
this.store.set(key, item);
|
|
148
147
|
}
|
|
149
148
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/index.mjs
|
|
2
|
-
var
|
|
2
|
+
var FastTTLCache = class {
|
|
3
3
|
/**
|
|
4
4
|
* 构造函数
|
|
5
5
|
* @param options 配置选项,包含ttl(过期时间)和capacity(容量)
|
|
@@ -41,7 +41,7 @@ var TTLCache = class {
|
|
|
41
41
|
const item = this.store.get(key);
|
|
42
42
|
if (!item) return null;
|
|
43
43
|
if (Date.now() - item.time > this.ttl) {
|
|
44
|
-
this.del(
|
|
44
|
+
this.del(item);
|
|
45
45
|
return null;
|
|
46
46
|
}
|
|
47
47
|
return item.value;
|
|
@@ -53,77 +53,76 @@ var TTLCache = class {
|
|
|
53
53
|
*/
|
|
54
54
|
put(key, value) {
|
|
55
55
|
if (this.size === 0) {
|
|
56
|
-
|
|
56
|
+
const item2 = {
|
|
57
57
|
key,
|
|
58
58
|
value,
|
|
59
|
-
|
|
59
|
+
prev: null,
|
|
60
60
|
next: null,
|
|
61
61
|
time: Date.now()
|
|
62
|
-
}
|
|
63
|
-
this.head = this.tail =
|
|
62
|
+
};
|
|
63
|
+
this.head = this.tail = item2;
|
|
64
|
+
this.store.set(key, item2);
|
|
64
65
|
return;
|
|
65
66
|
}
|
|
66
67
|
if (this.store.has(key)) {
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
this.moveToTail(
|
|
68
|
+
const item2 = this.store.get(key);
|
|
69
|
+
item2.value = value;
|
|
70
|
+
item2.time = Date.now();
|
|
71
|
+
this.moveToTail(item2);
|
|
71
72
|
return;
|
|
72
73
|
}
|
|
73
|
-
const
|
|
74
|
-
const newItem = {
|
|
74
|
+
const item = {
|
|
75
75
|
key,
|
|
76
76
|
value,
|
|
77
|
-
|
|
77
|
+
prev: this.tail,
|
|
78
78
|
next: null,
|
|
79
79
|
time: Date.now()
|
|
80
80
|
};
|
|
81
|
-
|
|
82
|
-
this.
|
|
83
|
-
this.
|
|
81
|
+
this.tail.next = item;
|
|
82
|
+
this.tail = item;
|
|
83
|
+
this.store.set(key, item);
|
|
84
84
|
if (this.size > this.capacity) {
|
|
85
85
|
this.del(this.head);
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
89
89
|
* 移除节点
|
|
90
|
-
* @param
|
|
90
|
+
* @param item
|
|
91
91
|
*/
|
|
92
|
-
del(
|
|
92
|
+
del(item) {
|
|
93
|
+
const key = item.key;
|
|
93
94
|
if (!this.store.has(key)) return false;
|
|
94
|
-
const curItem = this.store.get(key);
|
|
95
95
|
if (this.size === 1) {
|
|
96
96
|
this.head = this.tail = null;
|
|
97
|
-
} else if (this.head ===
|
|
98
|
-
this.head =
|
|
99
|
-
|
|
100
|
-
} else if (this.tail ===
|
|
101
|
-
this.tail =
|
|
102
|
-
|
|
97
|
+
} else if (this.head === item) {
|
|
98
|
+
this.head = item.next;
|
|
99
|
+
this.head.prev = null;
|
|
100
|
+
} else if (this.tail === item) {
|
|
101
|
+
this.tail = item.prev;
|
|
102
|
+
this.tail.next = null;
|
|
103
103
|
} else {
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
item.prev.next = item.next;
|
|
105
|
+
item.next.prev = item.prev;
|
|
106
106
|
}
|
|
107
107
|
this.store.delete(key);
|
|
108
108
|
return true;
|
|
109
109
|
}
|
|
110
110
|
/**
|
|
111
111
|
* 将节点移动到队尾,队尾的节点一定是最后一个更新的
|
|
112
|
-
* @param
|
|
112
|
+
* @param item
|
|
113
113
|
*/
|
|
114
|
-
moveToTail(
|
|
114
|
+
moveToTail(item) {
|
|
115
|
+
const key = item.key;
|
|
115
116
|
if (!this.store.has(key)) return;
|
|
116
|
-
if (this.tail ===
|
|
117
|
-
|
|
118
|
-
this.
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
this.tail = key;
|
|
124
|
-
this.store.set(key, curItem);
|
|
117
|
+
if (this.tail === item) return;
|
|
118
|
+
this.del(item);
|
|
119
|
+
this.tail.next = item;
|
|
120
|
+
item.prev = this.tail;
|
|
121
|
+
item.next = null;
|
|
122
|
+
this.tail = item;
|
|
123
|
+
this.store.set(key, item);
|
|
125
124
|
}
|
|
126
125
|
};
|
|
127
126
|
export {
|
|
128
|
-
|
|
127
|
+
FastTTLCache as default
|
|
129
128
|
};
|