@abtnode/db-cache 1.16.45-beta-20250609-025419-7fd1f86c

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,128 @@
1
+ type ForceType = 'redis' | 'sqlite';
2
+ interface BaseDBCacheParams {
3
+ /** Required non-empty prefix for all stored keys */
4
+ prefix: string;
5
+ /** Filesystem path for SQLite DB (also used to derive LMDB path) */
6
+ sqlitePath: string;
7
+ /** Default TTL (ms) */
8
+ ttl: number;
9
+ /** Cleanup interval (ms) for expired rows in SQLite */
10
+ cleanupInterval?: number;
11
+ /** URL for Redis (falls back to process.env.REDIS_URL if undefined) */
12
+ redisUrl?: string;
13
+ forceType?: ForceType;
14
+ }
15
+ type BaseDBCacheOptions = () => BaseDBCacheParams;
16
+ interface IDBAdapter {
17
+ opts: BaseDBCacheParams;
18
+ ensure(): Promise<void>;
19
+ set(key: string, value: unknown, opts: {
20
+ ttl?: number;
21
+ nx?: boolean;
22
+ }): Promise<boolean>;
23
+ get(key: string): Promise<unknown>;
24
+ del(key: string): Promise<void>;
25
+ has(key: string): Promise<boolean>;
26
+ groupSet(key: string, subKey: string, value: unknown, opts: {
27
+ ttl?: number;
28
+ nx?: boolean;
29
+ }): Promise<void>;
30
+ groupGet(key: string, subKey: string): Promise<unknown>;
31
+ groupHas(key: string, subKey: string): Promise<boolean>;
32
+ groupDel(key: string, subKey: string): Promise<void>;
33
+ serialize(value: unknown): string;
34
+ deserialize(raw: string | null | undefined): unknown;
35
+ close(): Promise<void>;
36
+ flushAll(): Promise<void>;
37
+ }
38
+
39
+ declare class BaseDBCache {
40
+ private adapter;
41
+ prefix: string;
42
+ type: string;
43
+ defaultTtl: number;
44
+ private getOpts;
45
+ private _initdAdapter;
46
+ constructor(opts: BaseDBCacheOptions);
47
+ protected initAdapter(): void;
48
+ static randomKey(): string;
49
+ set(key: string, value: unknown, opts?: {
50
+ ttl?: number;
51
+ nx?: boolean;
52
+ }): Promise<boolean>;
53
+ get(key: string): Promise<unknown>;
54
+ del(key: string): Promise<void>;
55
+ has(key: string): Promise<boolean>;
56
+ groupSet(key: string, subKey: string, value: unknown, opts?: {
57
+ ttl?: number;
58
+ nx?: boolean;
59
+ }): Promise<void>;
60
+ groupGet(key: string, subKey: string): Promise<unknown>;
61
+ groupHas(key: string, subKey: string): Promise<boolean>;
62
+ groupDel(key: string, subKey: string): Promise<void>;
63
+ close(): Promise<void>;
64
+ flushAll(): Promise<void>;
65
+ }
66
+
67
+ declare class SingleFlightDBCache extends BaseDBCache {
68
+ private _pending;
69
+ constructor(opts: BaseDBCacheOptions);
70
+ /**
71
+ * If a request for the same key is already in flight, reuse it.
72
+ * Otherwise, fetch, cache, and return the value.
73
+ */
74
+ autoCache<T>(key: string, fn: () => Promise<T>, { ttl }?: {
75
+ ttl?: number;
76
+ }): Promise<T>;
77
+ /**
78
+ * Same logic as autoCache, but for group fields (hash).
79
+ */
80
+ autoCacheGroup<T>(key: string, subKey: string, fn: () => Promise<T>, { ttl }?: {
81
+ ttl?: number;
82
+ }): Promise<T>;
83
+ }
84
+
85
+ declare class LockDBCache extends SingleFlightDBCache {
86
+ private _inFlight;
87
+ createLock(lockName: string): Promise<boolean>;
88
+ /**
89
+ * 检查锁是否已经过期:只要缓存层面已经没有该 key,就代表已过期或从未创建。
90
+ *
91
+ * @param lockName 锁的名称
92
+ * @returns 如果锁不存在或已过期,返回 true;否则返回 false
93
+ */
94
+ isExpired(lockName: string): Promise<boolean>;
95
+ /**
96
+ * 主动释放锁:删除对应的缓存 key
97
+ *
98
+ * @param lockName 锁的名称
99
+ */
100
+ releaseLock(lockName: string): Promise<void>;
101
+ /**
102
+ * 等待锁释放或超时。每隔 100ms 检查一次缓存层面是否仍存在该锁 key。
103
+ * 如果在 timeoutMs 毫秒内 key 不再存在,则 resolve(true)。否则 resolve(false)。
104
+ *
105
+ * @param lockName 锁的名称
106
+ * @param timeoutMs 等待超时时间(毫秒),默认使用构造时传入的 this._timeout
107
+ * @returns 如果锁被释放或过期,resolve(true);等待到超时还没释放,则 resolve(false)
108
+ */
109
+ waitUnLock(lockName: string, timeoutMs?: number): Promise<boolean>;
110
+ /**
111
+ * 获取锁的流程:
112
+ * 1. 先尝试 createLock(lockName):
113
+ * - 如果立刻拿到(createLock 返回 true),直接返回;
114
+ * - 如果没拿到,就等待 waitUnLock(lockName)。
115
+ * 2. 等待到释放/过期后,再次尝试 createLock(lockName)。
116
+ *
117
+ * @param lockName 锁的名称
118
+ */
119
+ acquire(lockName: string): Promise<void>;
120
+ private _formatLockKey;
121
+ }
122
+
123
+ declare const getAbtNodeRedisAndSQLiteUrl: () => {
124
+ redisUrl: string | undefined;
125
+ sqlitePath: string | undefined;
126
+ };
127
+
128
+ export { type BaseDBCacheOptions, LockDBCache as DBCache, type IDBAdapter, getAbtNodeRedisAndSQLiteUrl };
@@ -0,0 +1,128 @@
1
+ type ForceType = 'redis' | 'sqlite';
2
+ interface BaseDBCacheParams {
3
+ /** Required non-empty prefix for all stored keys */
4
+ prefix: string;
5
+ /** Filesystem path for SQLite DB (also used to derive LMDB path) */
6
+ sqlitePath: string;
7
+ /** Default TTL (ms) */
8
+ ttl: number;
9
+ /** Cleanup interval (ms) for expired rows in SQLite */
10
+ cleanupInterval?: number;
11
+ /** URL for Redis (falls back to process.env.REDIS_URL if undefined) */
12
+ redisUrl?: string;
13
+ forceType?: ForceType;
14
+ }
15
+ type BaseDBCacheOptions = () => BaseDBCacheParams;
16
+ interface IDBAdapter {
17
+ opts: BaseDBCacheParams;
18
+ ensure(): Promise<void>;
19
+ set(key: string, value: unknown, opts: {
20
+ ttl?: number;
21
+ nx?: boolean;
22
+ }): Promise<boolean>;
23
+ get(key: string): Promise<unknown>;
24
+ del(key: string): Promise<void>;
25
+ has(key: string): Promise<boolean>;
26
+ groupSet(key: string, subKey: string, value: unknown, opts: {
27
+ ttl?: number;
28
+ nx?: boolean;
29
+ }): Promise<void>;
30
+ groupGet(key: string, subKey: string): Promise<unknown>;
31
+ groupHas(key: string, subKey: string): Promise<boolean>;
32
+ groupDel(key: string, subKey: string): Promise<void>;
33
+ serialize(value: unknown): string;
34
+ deserialize(raw: string | null | undefined): unknown;
35
+ close(): Promise<void>;
36
+ flushAll(): Promise<void>;
37
+ }
38
+
39
+ declare class BaseDBCache {
40
+ private adapter;
41
+ prefix: string;
42
+ type: string;
43
+ defaultTtl: number;
44
+ private getOpts;
45
+ private _initdAdapter;
46
+ constructor(opts: BaseDBCacheOptions);
47
+ protected initAdapter(): void;
48
+ static randomKey(): string;
49
+ set(key: string, value: unknown, opts?: {
50
+ ttl?: number;
51
+ nx?: boolean;
52
+ }): Promise<boolean>;
53
+ get(key: string): Promise<unknown>;
54
+ del(key: string): Promise<void>;
55
+ has(key: string): Promise<boolean>;
56
+ groupSet(key: string, subKey: string, value: unknown, opts?: {
57
+ ttl?: number;
58
+ nx?: boolean;
59
+ }): Promise<void>;
60
+ groupGet(key: string, subKey: string): Promise<unknown>;
61
+ groupHas(key: string, subKey: string): Promise<boolean>;
62
+ groupDel(key: string, subKey: string): Promise<void>;
63
+ close(): Promise<void>;
64
+ flushAll(): Promise<void>;
65
+ }
66
+
67
+ declare class SingleFlightDBCache extends BaseDBCache {
68
+ private _pending;
69
+ constructor(opts: BaseDBCacheOptions);
70
+ /**
71
+ * If a request for the same key is already in flight, reuse it.
72
+ * Otherwise, fetch, cache, and return the value.
73
+ */
74
+ autoCache<T>(key: string, fn: () => Promise<T>, { ttl }?: {
75
+ ttl?: number;
76
+ }): Promise<T>;
77
+ /**
78
+ * Same logic as autoCache, but for group fields (hash).
79
+ */
80
+ autoCacheGroup<T>(key: string, subKey: string, fn: () => Promise<T>, { ttl }?: {
81
+ ttl?: number;
82
+ }): Promise<T>;
83
+ }
84
+
85
+ declare class LockDBCache extends SingleFlightDBCache {
86
+ private _inFlight;
87
+ createLock(lockName: string): Promise<boolean>;
88
+ /**
89
+ * 检查锁是否已经过期:只要缓存层面已经没有该 key,就代表已过期或从未创建。
90
+ *
91
+ * @param lockName 锁的名称
92
+ * @returns 如果锁不存在或已过期,返回 true;否则返回 false
93
+ */
94
+ isExpired(lockName: string): Promise<boolean>;
95
+ /**
96
+ * 主动释放锁:删除对应的缓存 key
97
+ *
98
+ * @param lockName 锁的名称
99
+ */
100
+ releaseLock(lockName: string): Promise<void>;
101
+ /**
102
+ * 等待锁释放或超时。每隔 100ms 检查一次缓存层面是否仍存在该锁 key。
103
+ * 如果在 timeoutMs 毫秒内 key 不再存在,则 resolve(true)。否则 resolve(false)。
104
+ *
105
+ * @param lockName 锁的名称
106
+ * @param timeoutMs 等待超时时间(毫秒),默认使用构造时传入的 this._timeout
107
+ * @returns 如果锁被释放或过期,resolve(true);等待到超时还没释放,则 resolve(false)
108
+ */
109
+ waitUnLock(lockName: string, timeoutMs?: number): Promise<boolean>;
110
+ /**
111
+ * 获取锁的流程:
112
+ * 1. 先尝试 createLock(lockName):
113
+ * - 如果立刻拿到(createLock 返回 true),直接返回;
114
+ * - 如果没拿到,就等待 waitUnLock(lockName)。
115
+ * 2. 等待到释放/过期后,再次尝试 createLock(lockName)。
116
+ *
117
+ * @param lockName 锁的名称
118
+ */
119
+ acquire(lockName: string): Promise<void>;
120
+ private _formatLockKey;
121
+ }
122
+
123
+ declare const getAbtNodeRedisAndSQLiteUrl: () => {
124
+ redisUrl: string | undefined;
125
+ sqlitePath: string | undefined;
126
+ };
127
+
128
+ export { type BaseDBCacheOptions, LockDBCache as DBCache, type IDBAdapter, getAbtNodeRedisAndSQLiteUrl };
@@ -0,0 +1,128 @@
1
+ type ForceType = 'redis' | 'sqlite';
2
+ interface BaseDBCacheParams {
3
+ /** Required non-empty prefix for all stored keys */
4
+ prefix: string;
5
+ /** Filesystem path for SQLite DB (also used to derive LMDB path) */
6
+ sqlitePath: string;
7
+ /** Default TTL (ms) */
8
+ ttl: number;
9
+ /** Cleanup interval (ms) for expired rows in SQLite */
10
+ cleanupInterval?: number;
11
+ /** URL for Redis (falls back to process.env.REDIS_URL if undefined) */
12
+ redisUrl?: string;
13
+ forceType?: ForceType;
14
+ }
15
+ type BaseDBCacheOptions = () => BaseDBCacheParams;
16
+ interface IDBAdapter {
17
+ opts: BaseDBCacheParams;
18
+ ensure(): Promise<void>;
19
+ set(key: string, value: unknown, opts: {
20
+ ttl?: number;
21
+ nx?: boolean;
22
+ }): Promise<boolean>;
23
+ get(key: string): Promise<unknown>;
24
+ del(key: string): Promise<void>;
25
+ has(key: string): Promise<boolean>;
26
+ groupSet(key: string, subKey: string, value: unknown, opts: {
27
+ ttl?: number;
28
+ nx?: boolean;
29
+ }): Promise<void>;
30
+ groupGet(key: string, subKey: string): Promise<unknown>;
31
+ groupHas(key: string, subKey: string): Promise<boolean>;
32
+ groupDel(key: string, subKey: string): Promise<void>;
33
+ serialize(value: unknown): string;
34
+ deserialize(raw: string | null | undefined): unknown;
35
+ close(): Promise<void>;
36
+ flushAll(): Promise<void>;
37
+ }
38
+
39
+ declare class BaseDBCache {
40
+ private adapter;
41
+ prefix: string;
42
+ type: string;
43
+ defaultTtl: number;
44
+ private getOpts;
45
+ private _initdAdapter;
46
+ constructor(opts: BaseDBCacheOptions);
47
+ protected initAdapter(): void;
48
+ static randomKey(): string;
49
+ set(key: string, value: unknown, opts?: {
50
+ ttl?: number;
51
+ nx?: boolean;
52
+ }): Promise<boolean>;
53
+ get(key: string): Promise<unknown>;
54
+ del(key: string): Promise<void>;
55
+ has(key: string): Promise<boolean>;
56
+ groupSet(key: string, subKey: string, value: unknown, opts?: {
57
+ ttl?: number;
58
+ nx?: boolean;
59
+ }): Promise<void>;
60
+ groupGet(key: string, subKey: string): Promise<unknown>;
61
+ groupHas(key: string, subKey: string): Promise<boolean>;
62
+ groupDel(key: string, subKey: string): Promise<void>;
63
+ close(): Promise<void>;
64
+ flushAll(): Promise<void>;
65
+ }
66
+
67
+ declare class SingleFlightDBCache extends BaseDBCache {
68
+ private _pending;
69
+ constructor(opts: BaseDBCacheOptions);
70
+ /**
71
+ * If a request for the same key is already in flight, reuse it.
72
+ * Otherwise, fetch, cache, and return the value.
73
+ */
74
+ autoCache<T>(key: string, fn: () => Promise<T>, { ttl }?: {
75
+ ttl?: number;
76
+ }): Promise<T>;
77
+ /**
78
+ * Same logic as autoCache, but for group fields (hash).
79
+ */
80
+ autoCacheGroup<T>(key: string, subKey: string, fn: () => Promise<T>, { ttl }?: {
81
+ ttl?: number;
82
+ }): Promise<T>;
83
+ }
84
+
85
+ declare class LockDBCache extends SingleFlightDBCache {
86
+ private _inFlight;
87
+ createLock(lockName: string): Promise<boolean>;
88
+ /**
89
+ * 检查锁是否已经过期:只要缓存层面已经没有该 key,就代表已过期或从未创建。
90
+ *
91
+ * @param lockName 锁的名称
92
+ * @returns 如果锁不存在或已过期,返回 true;否则返回 false
93
+ */
94
+ isExpired(lockName: string): Promise<boolean>;
95
+ /**
96
+ * 主动释放锁:删除对应的缓存 key
97
+ *
98
+ * @param lockName 锁的名称
99
+ */
100
+ releaseLock(lockName: string): Promise<void>;
101
+ /**
102
+ * 等待锁释放或超时。每隔 100ms 检查一次缓存层面是否仍存在该锁 key。
103
+ * 如果在 timeoutMs 毫秒内 key 不再存在,则 resolve(true)。否则 resolve(false)。
104
+ *
105
+ * @param lockName 锁的名称
106
+ * @param timeoutMs 等待超时时间(毫秒),默认使用构造时传入的 this._timeout
107
+ * @returns 如果锁被释放或过期,resolve(true);等待到超时还没释放,则 resolve(false)
108
+ */
109
+ waitUnLock(lockName: string, timeoutMs?: number): Promise<boolean>;
110
+ /**
111
+ * 获取锁的流程:
112
+ * 1. 先尝试 createLock(lockName):
113
+ * - 如果立刻拿到(createLock 返回 true),直接返回;
114
+ * - 如果没拿到,就等待 waitUnLock(lockName)。
115
+ * 2. 等待到释放/过期后,再次尝试 createLock(lockName)。
116
+ *
117
+ * @param lockName 锁的名称
118
+ */
119
+ acquire(lockName: string): Promise<void>;
120
+ private _formatLockKey;
121
+ }
122
+
123
+ declare const getAbtNodeRedisAndSQLiteUrl: () => {
124
+ redisUrl: string | undefined;
125
+ sqlitePath: string | undefined;
126
+ };
127
+
128
+ export { type BaseDBCacheOptions, LockDBCache as DBCache, type IDBAdapter, getAbtNodeRedisAndSQLiteUrl };