fast-ttl-cache 0.1.0 → 0.1.2

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,49 @@
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 | null;
16
+ /** 链表尾部指针,指向最新更新的节点 */
17
+ tail: CacheItem | null;
18
+ /** 缓存大小 */
19
+ size: number;
20
+ /**
21
+ * 构造函数
22
+ * @param options 配置选项,包含ttl(过期时间)和capacity(容量)
23
+ */
24
+ constructor(options?: CacheOptions);
25
+ /**
26
+ * 获取缓存,惰性删除
27
+ * @param key 缓存键
28
+ * @returns 如果缓存存在且未过期返回值,否则返回null
29
+ */
30
+ get(key: string): any | null;
31
+ /**
32
+ * 设置缓存,包含已存在或新增
33
+ * @param key 缓存键
34
+ * @param value 缓存值
35
+ */
36
+ put(key: string, value: any): void;
37
+ /**
38
+ * 移除节点
39
+ * @param item CacheItem
40
+ */
41
+ private del;
42
+ /**
43
+ * 将节点移动到队尾,队尾的节点一定是最后一个更新的
44
+ * @param item CacheItem
45
+ */
46
+ private moveToTail;
47
+ }
48
+
49
+ 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
  /** 缓存最大容量 */
@@ -26,7 +27,7 @@ export default class FastTTLCache {
26
27
  * @param key 缓存键
27
28
  * @returns 如果缓存存在且未过期返回值,否则返回null
28
29
  */
29
- getToken(key: string): any | null;
30
+ get(key: string): any | null;
30
31
  /**
31
32
  * 设置缓存,包含已存在或新增
32
33
  * @param key 缓存键
@@ -44,3 +45,5 @@ export default class FastTTLCache {
44
45
  */
45
46
  private moveToTail;
46
47
  }
48
+
49
+ export { FastTTLCache as default };
package/dist/index.js CHANGED
@@ -65,7 +65,7 @@ var FastTTLCache = class {
65
65
  * @param key 缓存键
66
66
  * @returns 如果缓存存在且未过期返回值,否则返回null
67
67
  */
68
- getToken(key) {
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) {
package/dist/index.mjs CHANGED
@@ -42,7 +42,7 @@ var FastTTLCache = class {
42
42
  * @param key 缓存键
43
43
  * @returns 如果缓存存在且未过期返回值,否则返回null
44
44
  */
45
- getToken(key) {
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) {
@@ -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.0",
3
+ "version": "0.1.2",
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"