fast-ttl-cache 0.1.2 → 0.1.3
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.d.mts +12 -9
- package/dist/index.d.ts +12 -9
- package/dist/index.js +33 -29
- package/dist/index.mjs +33 -29
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -12,36 +12,39 @@ declare class FastTTLCache {
|
|
|
12
12
|
/** 存储缓存项的Map */
|
|
13
13
|
private store;
|
|
14
14
|
/** 链表头部指针,指向最久未更新的节点 */
|
|
15
|
-
head: CacheItem
|
|
15
|
+
head: CacheItem;
|
|
16
16
|
/** 链表尾部指针,指向最新更新的节点 */
|
|
17
|
-
tail: CacheItem
|
|
17
|
+
tail: CacheItem;
|
|
18
18
|
/** 缓存大小 */
|
|
19
19
|
size: number;
|
|
20
20
|
/**
|
|
21
21
|
* 构造函数
|
|
22
|
-
* @param options 配置选项,包含ttl(过期时间)和capacity(容量)
|
|
22
|
+
* @param {CacheOptions} options 配置选项,包含ttl(过期时间)和capacity(容量)
|
|
23
23
|
*/
|
|
24
24
|
constructor(options?: CacheOptions);
|
|
25
25
|
/**
|
|
26
26
|
* 获取缓存,惰性删除
|
|
27
|
-
* @param key 缓存键
|
|
27
|
+
* @param {string} key 缓存键
|
|
28
28
|
* @returns 如果缓存存在且未过期返回值,否则返回null
|
|
29
29
|
*/
|
|
30
|
-
get(key: string): any
|
|
30
|
+
get(key: string): any;
|
|
31
31
|
/**
|
|
32
32
|
* 设置缓存,包含已存在或新增
|
|
33
|
-
* @param key 缓存键
|
|
34
|
-
* @param value 缓存值
|
|
33
|
+
* @param {string} key 缓存键
|
|
34
|
+
* @param {any} value 缓存值
|
|
35
35
|
*/
|
|
36
36
|
put(key: string, value: any): void;
|
|
37
37
|
/**
|
|
38
38
|
* 移除节点
|
|
39
|
-
* @param item
|
|
39
|
+
* @param {CacheItem} item
|
|
40
|
+
* @param {boolean} [isExpire = false] 是否为过期删除
|
|
41
|
+
* @returns {boolean} 是否删除成功
|
|
40
42
|
*/
|
|
41
43
|
private del;
|
|
42
44
|
/**
|
|
43
45
|
* 将节点移动到队尾,队尾的节点一定是最后一个更新的
|
|
44
|
-
* @param item
|
|
46
|
+
* @param {CacheItem} item
|
|
47
|
+
* @returns void
|
|
45
48
|
*/
|
|
46
49
|
private moveToTail;
|
|
47
50
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -12,36 +12,39 @@ declare class FastTTLCache {
|
|
|
12
12
|
/** 存储缓存项的Map */
|
|
13
13
|
private store;
|
|
14
14
|
/** 链表头部指针,指向最久未更新的节点 */
|
|
15
|
-
head: CacheItem
|
|
15
|
+
head: CacheItem;
|
|
16
16
|
/** 链表尾部指针,指向最新更新的节点 */
|
|
17
|
-
tail: CacheItem
|
|
17
|
+
tail: CacheItem;
|
|
18
18
|
/** 缓存大小 */
|
|
19
19
|
size: number;
|
|
20
20
|
/**
|
|
21
21
|
* 构造函数
|
|
22
|
-
* @param options 配置选项,包含ttl(过期时间)和capacity(容量)
|
|
22
|
+
* @param {CacheOptions} options 配置选项,包含ttl(过期时间)和capacity(容量)
|
|
23
23
|
*/
|
|
24
24
|
constructor(options?: CacheOptions);
|
|
25
25
|
/**
|
|
26
26
|
* 获取缓存,惰性删除
|
|
27
|
-
* @param key 缓存键
|
|
27
|
+
* @param {string} key 缓存键
|
|
28
28
|
* @returns 如果缓存存在且未过期返回值,否则返回null
|
|
29
29
|
*/
|
|
30
|
-
get(key: string): any
|
|
30
|
+
get(key: string): any;
|
|
31
31
|
/**
|
|
32
32
|
* 设置缓存,包含已存在或新增
|
|
33
|
-
* @param key 缓存键
|
|
34
|
-
* @param value 缓存值
|
|
33
|
+
* @param {string} key 缓存键
|
|
34
|
+
* @param {any} value 缓存值
|
|
35
35
|
*/
|
|
36
36
|
put(key: string, value: any): void;
|
|
37
37
|
/**
|
|
38
38
|
* 移除节点
|
|
39
|
-
* @param item
|
|
39
|
+
* @param {CacheItem} item
|
|
40
|
+
* @param {boolean} [isExpire = false] 是否为过期删除
|
|
41
|
+
* @returns {boolean} 是否删除成功
|
|
40
42
|
*/
|
|
41
43
|
private del;
|
|
42
44
|
/**
|
|
43
45
|
* 将节点移动到队尾,队尾的节点一定是最后一个更新的
|
|
44
|
-
* @param item
|
|
46
|
+
* @param {CacheItem} item
|
|
47
|
+
* @returns void
|
|
45
48
|
*/
|
|
46
49
|
private moveToTail;
|
|
47
50
|
}
|
package/dist/index.js
CHANGED
|
@@ -25,7 +25,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
25
25
|
var FastTTLCache = class {
|
|
26
26
|
/**
|
|
27
27
|
* 构造函数
|
|
28
|
-
* @param options 配置选项,包含ttl(过期时间)和capacity(容量)
|
|
28
|
+
* @param {CacheOptions} options 配置选项,包含ttl(过期时间)和capacity(容量)
|
|
29
29
|
*/
|
|
30
30
|
constructor(options = {}) {
|
|
31
31
|
/** 存储缓存项的Map */
|
|
@@ -62,36 +62,24 @@ var FastTTLCache = class {
|
|
|
62
62
|
}
|
|
63
63
|
/**
|
|
64
64
|
* 获取缓存,惰性删除
|
|
65
|
-
* @param key 缓存键
|
|
65
|
+
* @param {string} key 缓存键
|
|
66
66
|
* @returns 如果缓存存在且未过期返回值,否则返回null
|
|
67
67
|
*/
|
|
68
68
|
get(key) {
|
|
69
69
|
const item = this.store.get(key);
|
|
70
70
|
if (!item) return null;
|
|
71
71
|
if (Date.now() - item.time > this.ttl) {
|
|
72
|
-
this.del(item);
|
|
72
|
+
this.del(item, true);
|
|
73
73
|
return null;
|
|
74
74
|
}
|
|
75
75
|
return item.value;
|
|
76
76
|
}
|
|
77
77
|
/**
|
|
78
78
|
* 设置缓存,包含已存在或新增
|
|
79
|
-
* @param key 缓存键
|
|
80
|
-
* @param value 缓存值
|
|
79
|
+
* @param {string} key 缓存键
|
|
80
|
+
* @param {any} value 缓存值
|
|
81
81
|
*/
|
|
82
82
|
put(key, value) {
|
|
83
|
-
if (this.size === 0) {
|
|
84
|
-
const item2 = {
|
|
85
|
-
key,
|
|
86
|
-
value,
|
|
87
|
-
prev: null,
|
|
88
|
-
next: null,
|
|
89
|
-
time: Date.now()
|
|
90
|
-
};
|
|
91
|
-
this.head = this.tail = item2;
|
|
92
|
-
this.store.set(key, item2);
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
83
|
if (this.store.has(key)) {
|
|
96
84
|
const item2 = this.store.get(key);
|
|
97
85
|
item2.value = value;
|
|
@@ -106,8 +94,12 @@ var FastTTLCache = class {
|
|
|
106
94
|
next: null,
|
|
107
95
|
time: Date.now()
|
|
108
96
|
};
|
|
109
|
-
this.
|
|
110
|
-
|
|
97
|
+
if (this.size === 0) {
|
|
98
|
+
this.head = this.tail = item;
|
|
99
|
+
} else {
|
|
100
|
+
this.tail.next = item;
|
|
101
|
+
this.tail = item;
|
|
102
|
+
}
|
|
111
103
|
this.store.set(key, item);
|
|
112
104
|
if (this.size > this.capacity) {
|
|
113
105
|
this.del(this.head);
|
|
@@ -115,16 +107,28 @@ var FastTTLCache = class {
|
|
|
115
107
|
}
|
|
116
108
|
/**
|
|
117
109
|
* 移除节点
|
|
118
|
-
* @param item
|
|
110
|
+
* @param {CacheItem} item
|
|
111
|
+
* @param {boolean} [isExpire = false] 是否为过期删除
|
|
112
|
+
* @returns {boolean} 是否删除成功
|
|
119
113
|
*/
|
|
120
|
-
del(item) {
|
|
114
|
+
del(item, isExpire = false) {
|
|
121
115
|
const key = item.key;
|
|
122
116
|
if (!this.store.has(key)) return false;
|
|
123
|
-
if (
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
117
|
+
if (isExpire) {
|
|
118
|
+
let prevItem = item.prev;
|
|
119
|
+
while (prevItem) {
|
|
120
|
+
this.store.delete(prevItem.key);
|
|
121
|
+
prevItem = prevItem.prev;
|
|
122
|
+
}
|
|
123
|
+
this.head = item;
|
|
124
|
+
}
|
|
125
|
+
if (this.head === item) {
|
|
126
|
+
if (item.next) {
|
|
127
|
+
this.head = item.next;
|
|
128
|
+
this.head.prev = null;
|
|
129
|
+
} else {
|
|
130
|
+
this.head = this.tail = null;
|
|
131
|
+
}
|
|
128
132
|
} else if (this.tail === item) {
|
|
129
133
|
this.tail = item.prev;
|
|
130
134
|
this.tail.next = null;
|
|
@@ -132,12 +136,12 @@ var FastTTLCache = class {
|
|
|
132
136
|
item.prev.next = item.next;
|
|
133
137
|
item.next.prev = item.prev;
|
|
134
138
|
}
|
|
135
|
-
this.store.delete(key);
|
|
136
|
-
return true;
|
|
139
|
+
return this.store.delete(key);
|
|
137
140
|
}
|
|
138
141
|
/**
|
|
139
142
|
* 将节点移动到队尾,队尾的节点一定是最后一个更新的
|
|
140
|
-
* @param item
|
|
143
|
+
* @param {CacheItem} item
|
|
144
|
+
* @returns void
|
|
141
145
|
*/
|
|
142
146
|
moveToTail(item) {
|
|
143
147
|
const key = item.key;
|
package/dist/index.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
var FastTTLCache = class {
|
|
3
3
|
/**
|
|
4
4
|
* 构造函数
|
|
5
|
-
* @param options 配置选项,包含ttl(过期时间)和capacity(容量)
|
|
5
|
+
* @param {CacheOptions} options 配置选项,包含ttl(过期时间)和capacity(容量)
|
|
6
6
|
*/
|
|
7
7
|
constructor(options = {}) {
|
|
8
8
|
/** 存储缓存项的Map */
|
|
@@ -39,36 +39,24 @@ var FastTTLCache = class {
|
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
41
|
* 获取缓存,惰性删除
|
|
42
|
-
* @param key 缓存键
|
|
42
|
+
* @param {string} key 缓存键
|
|
43
43
|
* @returns 如果缓存存在且未过期返回值,否则返回null
|
|
44
44
|
*/
|
|
45
45
|
get(key) {
|
|
46
46
|
const item = this.store.get(key);
|
|
47
47
|
if (!item) return null;
|
|
48
48
|
if (Date.now() - item.time > this.ttl) {
|
|
49
|
-
this.del(item);
|
|
49
|
+
this.del(item, true);
|
|
50
50
|
return null;
|
|
51
51
|
}
|
|
52
52
|
return item.value;
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
55
|
* 设置缓存,包含已存在或新增
|
|
56
|
-
* @param key 缓存键
|
|
57
|
-
* @param value 缓存值
|
|
56
|
+
* @param {string} key 缓存键
|
|
57
|
+
* @param {any} value 缓存值
|
|
58
58
|
*/
|
|
59
59
|
put(key, value) {
|
|
60
|
-
if (this.size === 0) {
|
|
61
|
-
const item2 = {
|
|
62
|
-
key,
|
|
63
|
-
value,
|
|
64
|
-
prev: null,
|
|
65
|
-
next: null,
|
|
66
|
-
time: Date.now()
|
|
67
|
-
};
|
|
68
|
-
this.head = this.tail = item2;
|
|
69
|
-
this.store.set(key, item2);
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
60
|
if (this.store.has(key)) {
|
|
73
61
|
const item2 = this.store.get(key);
|
|
74
62
|
item2.value = value;
|
|
@@ -83,8 +71,12 @@ var FastTTLCache = class {
|
|
|
83
71
|
next: null,
|
|
84
72
|
time: Date.now()
|
|
85
73
|
};
|
|
86
|
-
this.
|
|
87
|
-
|
|
74
|
+
if (this.size === 0) {
|
|
75
|
+
this.head = this.tail = item;
|
|
76
|
+
} else {
|
|
77
|
+
this.tail.next = item;
|
|
78
|
+
this.tail = item;
|
|
79
|
+
}
|
|
88
80
|
this.store.set(key, item);
|
|
89
81
|
if (this.size > this.capacity) {
|
|
90
82
|
this.del(this.head);
|
|
@@ -92,16 +84,28 @@ var FastTTLCache = class {
|
|
|
92
84
|
}
|
|
93
85
|
/**
|
|
94
86
|
* 移除节点
|
|
95
|
-
* @param item
|
|
87
|
+
* @param {CacheItem} item
|
|
88
|
+
* @param {boolean} [isExpire = false] 是否为过期删除
|
|
89
|
+
* @returns {boolean} 是否删除成功
|
|
96
90
|
*/
|
|
97
|
-
del(item) {
|
|
91
|
+
del(item, isExpire = false) {
|
|
98
92
|
const key = item.key;
|
|
99
93
|
if (!this.store.has(key)) return false;
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
94
|
+
if (isExpire) {
|
|
95
|
+
let prevItem = item.prev;
|
|
96
|
+
while (prevItem) {
|
|
97
|
+
this.store.delete(prevItem.key);
|
|
98
|
+
prevItem = prevItem.prev;
|
|
99
|
+
}
|
|
100
|
+
this.head = item;
|
|
101
|
+
}
|
|
102
|
+
if (this.head === item) {
|
|
103
|
+
if (item.next) {
|
|
104
|
+
this.head = item.next;
|
|
105
|
+
this.head.prev = null;
|
|
106
|
+
} else {
|
|
107
|
+
this.head = this.tail = null;
|
|
108
|
+
}
|
|
105
109
|
} else if (this.tail === item) {
|
|
106
110
|
this.tail = item.prev;
|
|
107
111
|
this.tail.next = null;
|
|
@@ -109,12 +113,12 @@ var FastTTLCache = class {
|
|
|
109
113
|
item.prev.next = item.next;
|
|
110
114
|
item.next.prev = item.prev;
|
|
111
115
|
}
|
|
112
|
-
this.store.delete(key);
|
|
113
|
-
return true;
|
|
116
|
+
return this.store.delete(key);
|
|
114
117
|
}
|
|
115
118
|
/**
|
|
116
119
|
* 将节点移动到队尾,队尾的节点一定是最后一个更新的
|
|
117
|
-
* @param item
|
|
120
|
+
* @param {CacheItem} item
|
|
121
|
+
* @returns void
|
|
118
122
|
*/
|
|
119
123
|
moveToTail(item) {
|
|
120
124
|
const key = item.key;
|