fast-ttl-cache 0.0.8 → 0.1.1

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,46 @@
1
+ import { CacheItem, CacheOptions } from './types.js';
2
+ /**
3
+ * FastTTLCache 缓存类
4
+ * @brief 不使用 timer 实现的支持 ttl 和 capacity 的缓存类,惰性删除
5
+ */
6
+ export default class FastTTLCache {
7
+ /** 缓存过期时间(毫秒) */
8
+ ttl: number;
9
+ /** 缓存最大容量 */
10
+ capacity: number;
11
+ /** 存储缓存项的Map */
12
+ private store;
13
+ /** 链表头部指针,指向最久未更新的节点 */
14
+ head: CacheItem | null;
15
+ /** 链表尾部指针,指向最新更新的节点 */
16
+ tail: CacheItem | null;
17
+ /** 缓存大小 */
18
+ size: number;
19
+ /**
20
+ * 构造函数
21
+ * @param options 配置选项,包含ttl(过期时间)和capacity(容量)
22
+ */
23
+ constructor(options?: CacheOptions);
24
+ /**
25
+ * 获取缓存,惰性删除
26
+ * @param key 缓存键
27
+ * @returns 如果缓存存在且未过期返回值,否则返回null
28
+ */
29
+ get(key: string): any | null;
30
+ /**
31
+ * 设置缓存,包含已存在或新增
32
+ * @param key 缓存键
33
+ * @param value 缓存值
34
+ */
35
+ put(key: string, value: any): void;
36
+ /**
37
+ * 移除节点
38
+ * @param item CacheItem
39
+ */
40
+ private del;
41
+ /**
42
+ * 将节点移动到队尾,队尾的节点一定是最后一个更新的
43
+ * @param item CacheItem
44
+ */
45
+ private moveToTail;
46
+ }
package/dist/index.js CHANGED
@@ -16,7 +16,7 @@ var __copyProps = (to, from, except, desc) => {
16
16
  };
17
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
18
 
19
- // src/index.mjs
19
+ // src/index.ts
20
20
  var index_exports = {};
21
21
  __export(index_exports, {
22
22
  default: () => FastTTLCache
@@ -28,11 +28,16 @@ var FastTTLCache = class {
28
28
  * @param options 配置选项,包含ttl(过期时间)和capacity(容量)
29
29
  */
30
30
  constructor(options = {}) {
31
- this.ttl = options.ttl || Infinity;
32
- this.capacity = options.capacity || Infinity;
31
+ /** 存储缓存项的Map */
33
32
  this.store = /* @__PURE__ */ new Map();
34
- this.head = this.tail = null;
33
+ /** 链表头部指针,指向最久未更新的节点 */
34
+ this.head = null;
35
+ /** 链表尾部指针,指向最新更新的节点 */
36
+ this.tail = null;
37
+ /** 缓存大小 */
35
38
  this.size = 0;
39
+ this.ttl = options.ttl || Infinity;
40
+ this.capacity = options.capacity || Infinity;
36
41
  Object.defineProperties(this, {
37
42
  size: {
38
43
  get() {
@@ -110,7 +115,7 @@ var FastTTLCache = class {
110
115
  }
111
116
  /**
112
117
  * 移除节点
113
- * @param item
118
+ * @param item CacheItem
114
119
  */
115
120
  del(item) {
116
121
  const key = item.key;
@@ -132,16 +137,16 @@ var FastTTLCache = class {
132
137
  }
133
138
  /**
134
139
  * 将节点移动到队尾,队尾的节点一定是最后一个更新的
135
- * @param item
140
+ * @param item CacheItem
136
141
  */
137
142
  moveToTail(item) {
138
143
  const key = item.key;
139
144
  if (!this.store.has(key)) return;
140
145
  if (this.tail === item) return;
141
146
  this.del(item);
142
- this.tail.next = item;
143
147
  item.prev = this.tail;
144
148
  item.next = null;
149
+ this.tail.next = item;
145
150
  this.tail = item;
146
151
  this.store.set(key, item);
147
152
  }
package/dist/index.mjs CHANGED
@@ -1,15 +1,20 @@
1
- // src/index.mjs
1
+ // src/index.ts
2
2
  var FastTTLCache = class {
3
3
  /**
4
4
  * 构造函数
5
5
  * @param options 配置选项,包含ttl(过期时间)和capacity(容量)
6
6
  */
7
7
  constructor(options = {}) {
8
- this.ttl = options.ttl || Infinity;
9
- this.capacity = options.capacity || Infinity;
8
+ /** 存储缓存项的Map */
10
9
  this.store = /* @__PURE__ */ new Map();
11
- this.head = this.tail = null;
10
+ /** 链表头部指针,指向最久未更新的节点 */
11
+ this.head = null;
12
+ /** 链表尾部指针,指向最新更新的节点 */
13
+ this.tail = null;
14
+ /** 缓存大小 */
12
15
  this.size = 0;
16
+ this.ttl = options.ttl || Infinity;
17
+ this.capacity = options.capacity || Infinity;
13
18
  Object.defineProperties(this, {
14
19
  size: {
15
20
  get() {
@@ -87,7 +92,7 @@ var FastTTLCache = class {
87
92
  }
88
93
  /**
89
94
  * 移除节点
90
- * @param item
95
+ * @param item CacheItem
91
96
  */
92
97
  del(item) {
93
98
  const key = item.key;
@@ -109,16 +114,16 @@ var FastTTLCache = class {
109
114
  }
110
115
  /**
111
116
  * 将节点移动到队尾,队尾的节点一定是最后一个更新的
112
- * @param item
117
+ * @param item CacheItem
113
118
  */
114
119
  moveToTail(item) {
115
120
  const key = item.key;
116
121
  if (!this.store.has(key)) return;
117
122
  if (this.tail === item) return;
118
123
  this.del(item);
119
- this.tail.next = item;
120
124
  item.prev = this.tail;
121
125
  item.next = null;
126
+ this.tail.next = item;
122
127
  this.tail = item;
123
128
  this.store.set(key, item);
124
129
  }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * 缓存项的数据结构,包含键值对和双向链表指针
3
+ */
4
+ export 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
+ export interface CacheOptions {
20
+ /** 缓存过期时间(毫秒) */
21
+ ttl?: number;
22
+ /** 缓存最大容量 */
23
+ capacity?: number;
24
+ }
package/dist/types.js ADDED
@@ -0,0 +1,17 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
14
+
15
+ // src/types.ts
16
+ var types_exports = {};
17
+ module.exports = __toCommonJS(types_exports);
package/dist/types.mjs ADDED
File without changes
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "fast-ttl-cache",
3
- "version": "0.0.8",
3
+ "version": "0.1.1",
4
4
  "description": "ttl cache with capacity support use no timer",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
7
8
  "exports": {
8
9
  ".": {
9
10
  "require": "./dist/index.js",
@@ -11,8 +12,9 @@
11
12
  }
12
13
  },
13
14
  "scripts": {
14
- "build": "tsup src/ --format cjs,esm",
15
- "prepublishOnly": "npm run build"
15
+ "build": "shx rm -rf dist/ && tsup src/ --format cjs,esm && tsc src/*.ts --declarationDir dist/ --esModuleInterop true --declaration --emitDeclarationOnly",
16
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --verbose",
17
+ "prepublishOnly": "npm run test && npm run build"
16
18
  },
17
19
  "repository": {
18
20
  "type": "git",
@@ -21,6 +23,12 @@
21
23
  "author": "linye<llxy8687@foxmail.com>",
22
24
  "license": "MIT",
23
25
  "devDependencies": {
26
+ "@types/jest": "^30.0.0",
27
+ "@types/node": "^24.5.2",
28
+ "jest": "^30.1.3",
29
+ "jest-environment-node": "^30.1.2",
30
+ "shx": "^0.4.0",
31
+ "ts-jest": "^29.4.3",
24
32
  "tsup": "^8.5.0",
25
33
  "typescript": "^5.9.2"
26
34
  },