@abtnode/db-cache 1.17.8-beta-20260109-075740-5f484e08 → 1.17.8-beta-20260113-015027-32a1cec4

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.cts CHANGED
@@ -6,6 +6,12 @@ interface BaseDBCacheParams {
6
6
  sqlitePath: string;
7
7
  /** Default TTL (ms) */
8
8
  ttl: number;
9
+ /** Maximum size for LRU cache */
10
+ lruMaxSize?: number;
11
+ /** Enable LRU cache */
12
+ enableLruCache?: boolean;
13
+ /** Enable sync */
14
+ enableSync?: boolean;
9
15
  /** Cleanup interval (ms) for expired rows in SQLite */
10
16
  cleanupInterval?: number;
11
17
  /** URL for Redis (falls back to process.env.REDIS_URL if undefined) */
@@ -34,6 +40,11 @@ interface IDBAdapter {
34
40
  deserialize(raw: string | null | undefined): unknown;
35
41
  close(): Promise<void>;
36
42
  flushAll(): Promise<void>;
43
+ getLruCacheStats(): {
44
+ size: number;
45
+ groupCount: number;
46
+ maxSize: number;
47
+ } | null;
37
48
  }
38
49
 
39
50
  declare class BaseDBCache {
@@ -62,6 +73,15 @@ declare class BaseDBCache {
62
73
  groupDel(key: string, subKey: string): Promise<void>;
63
74
  close(): Promise<void>;
64
75
  flushAll(): Promise<void>;
76
+ /**
77
+ * 获取 LRU 缓存统计信息
78
+ * @returns 缓存统计信息,如果未启用 LRU 缓存则返回 null
79
+ */
80
+ getLruCacheStats(): {
81
+ size: number;
82
+ groupCount: number;
83
+ maxSize: number;
84
+ } | null;
65
85
  }
66
86
 
67
87
  declare class SingleFlightDBCache extends BaseDBCache {
@@ -134,9 +154,120 @@ interface WithRetryOptions {
134
154
  }
135
155
  declare function withRetry<T>(fn: () => Promise<T>, { max, backoffBase, backoffExponent, backoffJitter, needRetry, }?: WithRetryOptions): Promise<T>;
136
156
 
157
+ interface LruCacheOptions {
158
+ /** 缓存前缀,用于区分不同的缓存实例 */
159
+ prefix: string;
160
+ /** 最大缓存条数,默认 10000 */
161
+ maxSize?: number;
162
+ /** 是否启用跨 worker 同步,默认 true(在支持的环境中) */
163
+ enableSync?: boolean;
164
+ }
165
+ /**
166
+ * LRU 内存缓存工具类
167
+ *
168
+ * 特点:
169
+ * 1. 基于 LRU 算法的内存缓存
170
+ * 2. 支持 TTL 过期
171
+ * 3. 支持 Node.js 集群模式下的跨 worker 同步
172
+ * 4. 可单独使用,也可作为 Redis/SQLite 适配器的 L1 缓存层
173
+ *
174
+ * CLUSTER MODE NOTE:
175
+ * 当运行在 cluster 模式下时,缓存数据会通过 event hub 广播给所有 worker,
176
+ * 确保所有 worker 的内存缓存数据一致。
177
+ */
178
+ declare class LruCache {
179
+ private static caches;
180
+ private static groupCaches;
181
+ private static listenerSetup;
182
+ private cacheKey;
183
+ private maxSize;
184
+ private enableSync;
185
+ constructor(options: LruCacheOptions);
186
+ private getCache;
187
+ private getGroupCaches;
188
+ private getOrCreateGroupCache;
189
+ /**
190
+ * 设置同步监听器,接收其他 worker 广播的缓存数据
191
+ */
192
+ private _setupSyncListener;
193
+ /**
194
+ * 广播缓存数据给其他 worker
195
+ */
196
+ private _broadcastSync;
197
+ /**
198
+ * 广播删除缓存给其他 worker
199
+ */
200
+ private _broadcastDelete;
201
+ /**
202
+ * 广播清空缓存给其他 worker
203
+ */
204
+ private _broadcastClear;
205
+ private isExpired;
206
+ /**
207
+ * 设置缓存
208
+ * @param key 缓存键
209
+ * @param value 序列化后的值
210
+ * @param expiresAt 过期时间戳,null 表示永不过期
211
+ */
212
+ set(key: string, value: string, expiresAt: number | null): void;
213
+ /**
214
+ * 获取缓存
215
+ * @param key 缓存键
216
+ * @returns 序列化的值,未命中或过期返回 null
217
+ */
218
+ get(key: string): string | null;
219
+ /**
220
+ * 检查缓存是否存在且未过期
221
+ */
222
+ has(key: string): boolean;
223
+ /**
224
+ * 删除缓存
225
+ */
226
+ del(key: string): void;
227
+ /**
228
+ * 按前缀删除缓存
229
+ * @returns 删除的数量
230
+ */
231
+ delByPrefix(prefix: string): number;
232
+ /**
233
+ * 设置分组缓存
234
+ */
235
+ groupSet(key: string, subKey: string, value: string, expiresAt: number | null): void;
236
+ /**
237
+ * 获取分组缓存
238
+ */
239
+ groupGet(key: string, subKey: string): string | null;
240
+ /**
241
+ * 检查分组缓存是否存在
242
+ */
243
+ groupHas(key: string, subKey: string): boolean;
244
+ /**
245
+ * 删除分组缓存
246
+ */
247
+ groupDel(key: string, subKey: string): void;
248
+ /**
249
+ * 清空所有缓存
250
+ */
251
+ clear(): void;
252
+ /**
253
+ * 获取缓存统计信息
254
+ */
255
+ getStats(): {
256
+ size: number;
257
+ groupCount: number;
258
+ maxSize: number;
259
+ };
260
+ /**
261
+ * 关闭缓存(清理资源)
262
+ *
263
+ * 注意:不会广播 close 事件给其他 worker,因为每个 worker 有独立的生命周期
264
+ */
265
+ close(): void;
266
+ }
267
+
137
268
  declare const getAbtNodeRedisAndSQLiteUrl: () => {
138
269
  redisUrl: string | undefined;
139
270
  sqlitePath: string | undefined;
140
271
  };
141
272
 
142
- export { type BaseDBCacheOptions, LockDBCache as DBCache, type IDBAdapter, getAbtNodeRedisAndSQLiteUrl, withRetry };
273
+ export { type BaseDBCacheOptions, LockDBCache as DBCache, type ForceType, type IDBAdapter, LruCache, getAbtNodeRedisAndSQLiteUrl, withRetry };
package/dist/index.d.mts CHANGED
@@ -6,6 +6,12 @@ interface BaseDBCacheParams {
6
6
  sqlitePath: string;
7
7
  /** Default TTL (ms) */
8
8
  ttl: number;
9
+ /** Maximum size for LRU cache */
10
+ lruMaxSize?: number;
11
+ /** Enable LRU cache */
12
+ enableLruCache?: boolean;
13
+ /** Enable sync */
14
+ enableSync?: boolean;
9
15
  /** Cleanup interval (ms) for expired rows in SQLite */
10
16
  cleanupInterval?: number;
11
17
  /** URL for Redis (falls back to process.env.REDIS_URL if undefined) */
@@ -34,6 +40,11 @@ interface IDBAdapter {
34
40
  deserialize(raw: string | null | undefined): unknown;
35
41
  close(): Promise<void>;
36
42
  flushAll(): Promise<void>;
43
+ getLruCacheStats(): {
44
+ size: number;
45
+ groupCount: number;
46
+ maxSize: number;
47
+ } | null;
37
48
  }
38
49
 
39
50
  declare class BaseDBCache {
@@ -62,6 +73,15 @@ declare class BaseDBCache {
62
73
  groupDel(key: string, subKey: string): Promise<void>;
63
74
  close(): Promise<void>;
64
75
  flushAll(): Promise<void>;
76
+ /**
77
+ * 获取 LRU 缓存统计信息
78
+ * @returns 缓存统计信息,如果未启用 LRU 缓存则返回 null
79
+ */
80
+ getLruCacheStats(): {
81
+ size: number;
82
+ groupCount: number;
83
+ maxSize: number;
84
+ } | null;
65
85
  }
66
86
 
67
87
  declare class SingleFlightDBCache extends BaseDBCache {
@@ -134,9 +154,120 @@ interface WithRetryOptions {
134
154
  }
135
155
  declare function withRetry<T>(fn: () => Promise<T>, { max, backoffBase, backoffExponent, backoffJitter, needRetry, }?: WithRetryOptions): Promise<T>;
136
156
 
157
+ interface LruCacheOptions {
158
+ /** 缓存前缀,用于区分不同的缓存实例 */
159
+ prefix: string;
160
+ /** 最大缓存条数,默认 10000 */
161
+ maxSize?: number;
162
+ /** 是否启用跨 worker 同步,默认 true(在支持的环境中) */
163
+ enableSync?: boolean;
164
+ }
165
+ /**
166
+ * LRU 内存缓存工具类
167
+ *
168
+ * 特点:
169
+ * 1. 基于 LRU 算法的内存缓存
170
+ * 2. 支持 TTL 过期
171
+ * 3. 支持 Node.js 集群模式下的跨 worker 同步
172
+ * 4. 可单独使用,也可作为 Redis/SQLite 适配器的 L1 缓存层
173
+ *
174
+ * CLUSTER MODE NOTE:
175
+ * 当运行在 cluster 模式下时,缓存数据会通过 event hub 广播给所有 worker,
176
+ * 确保所有 worker 的内存缓存数据一致。
177
+ */
178
+ declare class LruCache {
179
+ private static caches;
180
+ private static groupCaches;
181
+ private static listenerSetup;
182
+ private cacheKey;
183
+ private maxSize;
184
+ private enableSync;
185
+ constructor(options: LruCacheOptions);
186
+ private getCache;
187
+ private getGroupCaches;
188
+ private getOrCreateGroupCache;
189
+ /**
190
+ * 设置同步监听器,接收其他 worker 广播的缓存数据
191
+ */
192
+ private _setupSyncListener;
193
+ /**
194
+ * 广播缓存数据给其他 worker
195
+ */
196
+ private _broadcastSync;
197
+ /**
198
+ * 广播删除缓存给其他 worker
199
+ */
200
+ private _broadcastDelete;
201
+ /**
202
+ * 广播清空缓存给其他 worker
203
+ */
204
+ private _broadcastClear;
205
+ private isExpired;
206
+ /**
207
+ * 设置缓存
208
+ * @param key 缓存键
209
+ * @param value 序列化后的值
210
+ * @param expiresAt 过期时间戳,null 表示永不过期
211
+ */
212
+ set(key: string, value: string, expiresAt: number | null): void;
213
+ /**
214
+ * 获取缓存
215
+ * @param key 缓存键
216
+ * @returns 序列化的值,未命中或过期返回 null
217
+ */
218
+ get(key: string): string | null;
219
+ /**
220
+ * 检查缓存是否存在且未过期
221
+ */
222
+ has(key: string): boolean;
223
+ /**
224
+ * 删除缓存
225
+ */
226
+ del(key: string): void;
227
+ /**
228
+ * 按前缀删除缓存
229
+ * @returns 删除的数量
230
+ */
231
+ delByPrefix(prefix: string): number;
232
+ /**
233
+ * 设置分组缓存
234
+ */
235
+ groupSet(key: string, subKey: string, value: string, expiresAt: number | null): void;
236
+ /**
237
+ * 获取分组缓存
238
+ */
239
+ groupGet(key: string, subKey: string): string | null;
240
+ /**
241
+ * 检查分组缓存是否存在
242
+ */
243
+ groupHas(key: string, subKey: string): boolean;
244
+ /**
245
+ * 删除分组缓存
246
+ */
247
+ groupDel(key: string, subKey: string): void;
248
+ /**
249
+ * 清空所有缓存
250
+ */
251
+ clear(): void;
252
+ /**
253
+ * 获取缓存统计信息
254
+ */
255
+ getStats(): {
256
+ size: number;
257
+ groupCount: number;
258
+ maxSize: number;
259
+ };
260
+ /**
261
+ * 关闭缓存(清理资源)
262
+ *
263
+ * 注意:不会广播 close 事件给其他 worker,因为每个 worker 有独立的生命周期
264
+ */
265
+ close(): void;
266
+ }
267
+
137
268
  declare const getAbtNodeRedisAndSQLiteUrl: () => {
138
269
  redisUrl: string | undefined;
139
270
  sqlitePath: string | undefined;
140
271
  };
141
272
 
142
- export { type BaseDBCacheOptions, LockDBCache as DBCache, type IDBAdapter, getAbtNodeRedisAndSQLiteUrl, withRetry };
273
+ export { type BaseDBCacheOptions, LockDBCache as DBCache, type ForceType, type IDBAdapter, LruCache, getAbtNodeRedisAndSQLiteUrl, withRetry };
package/dist/index.d.ts CHANGED
@@ -6,6 +6,12 @@ interface BaseDBCacheParams {
6
6
  sqlitePath: string;
7
7
  /** Default TTL (ms) */
8
8
  ttl: number;
9
+ /** Maximum size for LRU cache */
10
+ lruMaxSize?: number;
11
+ /** Enable LRU cache */
12
+ enableLruCache?: boolean;
13
+ /** Enable sync */
14
+ enableSync?: boolean;
9
15
  /** Cleanup interval (ms) for expired rows in SQLite */
10
16
  cleanupInterval?: number;
11
17
  /** URL for Redis (falls back to process.env.REDIS_URL if undefined) */
@@ -34,6 +40,11 @@ interface IDBAdapter {
34
40
  deserialize(raw: string | null | undefined): unknown;
35
41
  close(): Promise<void>;
36
42
  flushAll(): Promise<void>;
43
+ getLruCacheStats(): {
44
+ size: number;
45
+ groupCount: number;
46
+ maxSize: number;
47
+ } | null;
37
48
  }
38
49
 
39
50
  declare class BaseDBCache {
@@ -62,6 +73,15 @@ declare class BaseDBCache {
62
73
  groupDel(key: string, subKey: string): Promise<void>;
63
74
  close(): Promise<void>;
64
75
  flushAll(): Promise<void>;
76
+ /**
77
+ * 获取 LRU 缓存统计信息
78
+ * @returns 缓存统计信息,如果未启用 LRU 缓存则返回 null
79
+ */
80
+ getLruCacheStats(): {
81
+ size: number;
82
+ groupCount: number;
83
+ maxSize: number;
84
+ } | null;
65
85
  }
66
86
 
67
87
  declare class SingleFlightDBCache extends BaseDBCache {
@@ -134,9 +154,120 @@ interface WithRetryOptions {
134
154
  }
135
155
  declare function withRetry<T>(fn: () => Promise<T>, { max, backoffBase, backoffExponent, backoffJitter, needRetry, }?: WithRetryOptions): Promise<T>;
136
156
 
157
+ interface LruCacheOptions {
158
+ /** 缓存前缀,用于区分不同的缓存实例 */
159
+ prefix: string;
160
+ /** 最大缓存条数,默认 10000 */
161
+ maxSize?: number;
162
+ /** 是否启用跨 worker 同步,默认 true(在支持的环境中) */
163
+ enableSync?: boolean;
164
+ }
165
+ /**
166
+ * LRU 内存缓存工具类
167
+ *
168
+ * 特点:
169
+ * 1. 基于 LRU 算法的内存缓存
170
+ * 2. 支持 TTL 过期
171
+ * 3. 支持 Node.js 集群模式下的跨 worker 同步
172
+ * 4. 可单独使用,也可作为 Redis/SQLite 适配器的 L1 缓存层
173
+ *
174
+ * CLUSTER MODE NOTE:
175
+ * 当运行在 cluster 模式下时,缓存数据会通过 event hub 广播给所有 worker,
176
+ * 确保所有 worker 的内存缓存数据一致。
177
+ */
178
+ declare class LruCache {
179
+ private static caches;
180
+ private static groupCaches;
181
+ private static listenerSetup;
182
+ private cacheKey;
183
+ private maxSize;
184
+ private enableSync;
185
+ constructor(options: LruCacheOptions);
186
+ private getCache;
187
+ private getGroupCaches;
188
+ private getOrCreateGroupCache;
189
+ /**
190
+ * 设置同步监听器,接收其他 worker 广播的缓存数据
191
+ */
192
+ private _setupSyncListener;
193
+ /**
194
+ * 广播缓存数据给其他 worker
195
+ */
196
+ private _broadcastSync;
197
+ /**
198
+ * 广播删除缓存给其他 worker
199
+ */
200
+ private _broadcastDelete;
201
+ /**
202
+ * 广播清空缓存给其他 worker
203
+ */
204
+ private _broadcastClear;
205
+ private isExpired;
206
+ /**
207
+ * 设置缓存
208
+ * @param key 缓存键
209
+ * @param value 序列化后的值
210
+ * @param expiresAt 过期时间戳,null 表示永不过期
211
+ */
212
+ set(key: string, value: string, expiresAt: number | null): void;
213
+ /**
214
+ * 获取缓存
215
+ * @param key 缓存键
216
+ * @returns 序列化的值,未命中或过期返回 null
217
+ */
218
+ get(key: string): string | null;
219
+ /**
220
+ * 检查缓存是否存在且未过期
221
+ */
222
+ has(key: string): boolean;
223
+ /**
224
+ * 删除缓存
225
+ */
226
+ del(key: string): void;
227
+ /**
228
+ * 按前缀删除缓存
229
+ * @returns 删除的数量
230
+ */
231
+ delByPrefix(prefix: string): number;
232
+ /**
233
+ * 设置分组缓存
234
+ */
235
+ groupSet(key: string, subKey: string, value: string, expiresAt: number | null): void;
236
+ /**
237
+ * 获取分组缓存
238
+ */
239
+ groupGet(key: string, subKey: string): string | null;
240
+ /**
241
+ * 检查分组缓存是否存在
242
+ */
243
+ groupHas(key: string, subKey: string): boolean;
244
+ /**
245
+ * 删除分组缓存
246
+ */
247
+ groupDel(key: string, subKey: string): void;
248
+ /**
249
+ * 清空所有缓存
250
+ */
251
+ clear(): void;
252
+ /**
253
+ * 获取缓存统计信息
254
+ */
255
+ getStats(): {
256
+ size: number;
257
+ groupCount: number;
258
+ maxSize: number;
259
+ };
260
+ /**
261
+ * 关闭缓存(清理资源)
262
+ *
263
+ * 注意:不会广播 close 事件给其他 worker,因为每个 worker 有独立的生命周期
264
+ */
265
+ close(): void;
266
+ }
267
+
137
268
  declare const getAbtNodeRedisAndSQLiteUrl: () => {
138
269
  redisUrl: string | undefined;
139
270
  sqlitePath: string | undefined;
140
271
  };
141
272
 
142
- export { type BaseDBCacheOptions, LockDBCache as DBCache, type IDBAdapter, getAbtNodeRedisAndSQLiteUrl, withRetry };
273
+ export { type BaseDBCacheOptions, LockDBCache as DBCache, type ForceType, type IDBAdapter, LruCache, getAbtNodeRedisAndSQLiteUrl, withRetry };