fast-ttl-cache 0.1.1 → 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.
@@ -0,0 +1,52 @@
1
+ import { CacheItem, CacheOptions } from './types.mjs';
2
+
3
+ /**
4
+ * FastTTLCache 缓存类
5
+ * @brief 不使用 timer 实现的支持 ttl 和 capacity 的缓存类,惰性删除
6
+ */
7
+ declare class FastTTLCache {
8
+ /** 缓存过期时间(毫秒) */
9
+ ttl: number;
10
+ /** 缓存最大容量 */
11
+ capacity: number;
12
+ /** 存储缓存项的Map */
13
+ private store;
14
+ /** 链表头部指针,指向最久未更新的节点 */
15
+ head: CacheItem;
16
+ /** 链表尾部指针,指向最新更新的节点 */
17
+ tail: CacheItem;
18
+ /** 缓存大小 */
19
+ size: number;
20
+ /**
21
+ * 构造函数
22
+ * @param {CacheOptions} options 配置选项,包含ttl(过期时间)和capacity(容量)
23
+ */
24
+ constructor(options?: CacheOptions);
25
+ /**
26
+ * 获取缓存,惰性删除
27
+ * @param {string} key 缓存键
28
+ * @returns 如果缓存存在且未过期返回值,否则返回null
29
+ */
30
+ get(key: string): any;
31
+ /**
32
+ * 设置缓存,包含已存在或新增
33
+ * @param {string} key 缓存键
34
+ * @param {any} value 缓存值
35
+ */
36
+ put(key: string, value: any): void;
37
+ /**
38
+ * 移除节点
39
+ * @param {CacheItem} item
40
+ * @param {boolean} [isExpire = false] 是否为过期删除
41
+ * @returns {boolean} 是否删除成功
42
+ */
43
+ private del;
44
+ /**
45
+ * 将节点移动到队尾,队尾的节点一定是最后一个更新的
46
+ * @param {CacheItem} item
47
+ * @returns void
48
+ */
49
+ private moveToTail;
50
+ }
51
+
52
+ export { FastTTLCache as default };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { CacheItem, CacheOptions } from './types.js';
2
+
2
3
  /**
3
4
  * FastTTLCache 缓存类
4
5
  * @brief 不使用 timer 实现的支持 ttl 和 capacity 的缓存类,惰性删除
5
6
  */
6
- export default class FastTTLCache {
7
+ declare class FastTTLCache {
7
8
  /** 缓存过期时间(毫秒) */
8
9
  ttl: number;
9
10
  /** 缓存最大容量 */
@@ -11,36 +12,41 @@ export default class FastTTLCache {
11
12
  /** 存储缓存项的Map */
12
13
  private store;
13
14
  /** 链表头部指针,指向最久未更新的节点 */
14
- head: CacheItem | null;
15
+ head: CacheItem;
15
16
  /** 链表尾部指针,指向最新更新的节点 */
16
- tail: CacheItem | null;
17
+ tail: CacheItem;
17
18
  /** 缓存大小 */
18
19
  size: number;
19
20
  /**
20
21
  * 构造函数
21
- * @param options 配置选项,包含ttl(过期时间)和capacity(容量)
22
+ * @param {CacheOptions} options 配置选项,包含ttl(过期时间)和capacity(容量)
22
23
  */
23
24
  constructor(options?: CacheOptions);
24
25
  /**
25
26
  * 获取缓存,惰性删除
26
- * @param key 缓存键
27
+ * @param {string} key 缓存键
27
28
  * @returns 如果缓存存在且未过期返回值,否则返回null
28
29
  */
29
- get(key: string): any | null;
30
+ get(key: string): any;
30
31
  /**
31
32
  * 设置缓存,包含已存在或新增
32
- * @param key 缓存键
33
- * @param value 缓存值
33
+ * @param {string} key 缓存键
34
+ * @param {any} value 缓存值
34
35
  */
35
36
  put(key: string, value: any): void;
36
37
  /**
37
38
  * 移除节点
38
- * @param item CacheItem
39
+ * @param {CacheItem} item
40
+ * @param {boolean} [isExpire = false] 是否为过期删除
41
+ * @returns {boolean} 是否删除成功
39
42
  */
40
43
  private del;
41
44
  /**
42
45
  * 将节点移动到队尾,队尾的节点一定是最后一个更新的
43
- * @param item CacheItem
46
+ * @param {CacheItem} item
47
+ * @returns void
44
48
  */
45
49
  private moveToTail;
46
50
  }
51
+
52
+ export { FastTTLCache as default };
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.tail.next = item;
110
- this.tail = item;
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 CacheItem
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 (this.size === 1) {
124
- this.head = this.tail = null;
125
- } else if (this.head === item) {
126
- this.head = item.next;
127
- this.head.prev = null;
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 CacheItem
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.tail.next = item;
87
- this.tail = item;
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 CacheItem
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 (this.size === 1) {
101
- this.head = this.tail = null;
102
- } else if (this.head === item) {
103
- this.head = item.next;
104
- this.head.prev = null;
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 CacheItem
120
+ * @param {CacheItem} item
121
+ * @returns void
118
122
  */
119
123
  moveToTail(item) {
120
124
  const key = item.key;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * 缓存项的数据结构,包含键值对和双向链表指针
3
+ */
4
+ interface CacheItem {
5
+ /** 缓存项的键 */
6
+ key: string;
7
+ /** 缓存项的值 */
8
+ value: any;
9
+ /** 前一个节点指针 */
10
+ prev: CacheItem | null;
11
+ /** 后一个节点指针 */
12
+ next: CacheItem | null;
13
+ /** 缓存时间戳 */
14
+ time: number;
15
+ }
16
+ /**
17
+ * FastTTLCache constructor options
18
+ */
19
+ interface CacheOptions {
20
+ /** 缓存过期时间(毫秒) */
21
+ ttl?: number;
22
+ /** 缓存最大容量 */
23
+ capacity?: number;
24
+ }
25
+
26
+ export type { CacheItem, CacheOptions };
package/dist/types.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * 缓存项的数据结构,包含键值对和双向链表指针
3
3
  */
4
- export interface CacheItem {
4
+ interface CacheItem {
5
5
  /** 缓存项的键 */
6
6
  key: string;
7
7
  /** 缓存项的值 */
@@ -16,9 +16,11 @@ export interface CacheItem {
16
16
  /**
17
17
  * FastTTLCache constructor options
18
18
  */
19
- export interface CacheOptions {
19
+ interface CacheOptions {
20
20
  /** 缓存过期时间(毫秒) */
21
21
  ttl?: number;
22
22
  /** 缓存最大容量 */
23
23
  capacity?: number;
24
24
  }
25
+
26
+ export type { CacheItem, CacheOptions };
package/package.json CHANGED
@@ -1,18 +1,20 @@
1
1
  {
2
2
  "name": "fast-ttl-cache",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "ttl cache with capacity support use no timer",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
7
7
  "types": "./dist/index.d.ts",
8
8
  "exports": {
9
9
  ".": {
10
+ "types": "./dist/index.d.ts",
10
11
  "require": "./dist/index.js",
11
12
  "import": "./dist/index.mjs"
12
13
  }
13
14
  },
14
15
  "scripts": {
15
- "build": "shx rm -rf dist/ && tsup src/ --format cjs,esm && tsc src/*.ts --declarationDir dist/ --esModuleInterop true --declaration --emitDeclarationOnly",
16
+ "dev": "tsup src/ --clean --format cjs,esm --dts --watch",
17
+ "build": "tsup src/ --clean --format cjs,esm --dts",
16
18
  "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --verbose",
17
19
  "prepublishOnly": "npm run test && npm run build"
18
20
  },
@@ -27,7 +29,6 @@
27
29
  "@types/node": "^24.5.2",
28
30
  "jest": "^30.1.3",
29
31
  "jest-environment-node": "^30.1.2",
30
- "shx": "^0.4.0",
31
32
  "ts-jest": "^29.4.3",
32
33
  "tsup": "^8.5.0",
33
34
  "typescript": "^5.9.2"