@h-ai/cache 0.1.0-alpha5

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,476 @@
1
+ import { z } from 'zod';
2
+ import * as _h_ai_core from '@h-ai/core';
3
+ import { HaiResult } from '@h-ai/core';
4
+
5
+ /**
6
+ * @h-ai/cache — 错误码 + 配置 Schema
7
+ *
8
+ * 定义缓存模块的错误码常量、Zod Schema 和配置类型。
9
+ * @module cache-config
10
+ */
11
+
12
+ /** Redis 集群节点配置 */
13
+ declare const RedisClusterNodeSchema: z.ZodObject<{
14
+ host: z.ZodString;
15
+ port: z.ZodNumber;
16
+ }, z.core.$strip>;
17
+ /** Redis 集群节点类型 */
18
+ type RedisClusterNode = z.infer<typeof RedisClusterNodeSchema>;
19
+ /** Redis 哨兵模式配置 */
20
+ declare const RedisSentinelConfigSchema: z.ZodObject<{
21
+ sentinels: z.ZodArray<z.ZodObject<{
22
+ host: z.ZodString;
23
+ port: z.ZodNumber;
24
+ }, z.core.$strip>>;
25
+ name: z.ZodString;
26
+ }, z.core.$strip>;
27
+ /** Redis 哨兵模式配置类型 */
28
+ type RedisSentinelConfig = z.infer<typeof RedisSentinelConfigSchema>;
29
+ /** 内存缓存配置(仅用于开发/测试,不支持跨进程共享) */
30
+ declare const MemoryConfigSchema: z.ZodObject<{
31
+ type: z.ZodLiteral<"memory">;
32
+ }, z.core.$strip>;
33
+ /**
34
+ * Redis 缓存配置
35
+ *
36
+ * 连接优先级:url > cluster > sentinel > host
37
+ */
38
+ declare const RedisConfigSchema: z.ZodObject<{
39
+ type: z.ZodLiteral<"redis">;
40
+ url: z.ZodOptional<z.ZodString>;
41
+ host: z.ZodDefault<z.ZodString>;
42
+ port: z.ZodDefault<z.ZodNumber>;
43
+ password: z.ZodOptional<z.ZodString>;
44
+ db: z.ZodDefault<z.ZodNumber>;
45
+ cluster: z.ZodOptional<z.ZodArray<z.ZodObject<{
46
+ host: z.ZodString;
47
+ port: z.ZodNumber;
48
+ }, z.core.$strip>>>;
49
+ sentinel: z.ZodOptional<z.ZodObject<{
50
+ sentinels: z.ZodArray<z.ZodObject<{
51
+ host: z.ZodString;
52
+ port: z.ZodNumber;
53
+ }, z.core.$strip>>;
54
+ name: z.ZodString;
55
+ }, z.core.$strip>>;
56
+ connectTimeout: z.ZodDefault<z.ZodNumber>;
57
+ commandTimeout: z.ZodDefault<z.ZodNumber>;
58
+ keyPrefix: z.ZodOptional<z.ZodString>;
59
+ tls: z.ZodDefault<z.ZodBoolean>;
60
+ maxRetries: z.ZodDefault<z.ZodNumber>;
61
+ retryDelay: z.ZodDefault<z.ZodNumber>;
62
+ readOnly: z.ZodDefault<z.ZodBoolean>;
63
+ }, z.core.$strip>;
64
+ /** 缓存配置 Schema;通过 type 字段区分 memory / redis */
65
+ declare const CacheConfigSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
66
+ type: z.ZodLiteral<"memory">;
67
+ }, z.core.$strip>, z.ZodObject<{
68
+ type: z.ZodLiteral<"redis">;
69
+ url: z.ZodOptional<z.ZodString>;
70
+ host: z.ZodDefault<z.ZodString>;
71
+ port: z.ZodDefault<z.ZodNumber>;
72
+ password: z.ZodOptional<z.ZodString>;
73
+ db: z.ZodDefault<z.ZodNumber>;
74
+ cluster: z.ZodOptional<z.ZodArray<z.ZodObject<{
75
+ host: z.ZodString;
76
+ port: z.ZodNumber;
77
+ }, z.core.$strip>>>;
78
+ sentinel: z.ZodOptional<z.ZodObject<{
79
+ sentinels: z.ZodArray<z.ZodObject<{
80
+ host: z.ZodString;
81
+ port: z.ZodNumber;
82
+ }, z.core.$strip>>;
83
+ name: z.ZodString;
84
+ }, z.core.$strip>>;
85
+ connectTimeout: z.ZodDefault<z.ZodNumber>;
86
+ commandTimeout: z.ZodDefault<z.ZodNumber>;
87
+ keyPrefix: z.ZodOptional<z.ZodString>;
88
+ tls: z.ZodDefault<z.ZodBoolean>;
89
+ maxRetries: z.ZodDefault<z.ZodNumber>;
90
+ retryDelay: z.ZodDefault<z.ZodNumber>;
91
+ readOnly: z.ZodDefault<z.ZodBoolean>;
92
+ }, z.core.$strip>], "type">;
93
+ /** 缓存配置类型(Zod parse 后的完整类型,所有默认值已填充) */
94
+ type CacheConfig = z.infer<typeof CacheConfigSchema>;
95
+ /** 缓存配置输入类型(Zod parse 前,允许省略带默认值的字段) */
96
+ type CacheConfigInput = z.input<typeof CacheConfigSchema>;
97
+
98
+ declare const HaiCacheError: {
99
+ readonly CONNECTION_FAILED: _h_ai_core.HaiErrorDef;
100
+ readonly OPERATION_FAILED: _h_ai_core.HaiErrorDef;
101
+ readonly SERIALIZATION_FAILED: _h_ai_core.HaiErrorDef;
102
+ readonly DESERIALIZATION_FAILED: _h_ai_core.HaiErrorDef;
103
+ readonly KEY_NOT_FOUND: _h_ai_core.HaiErrorDef;
104
+ readonly TIMEOUT: _h_ai_core.HaiErrorDef;
105
+ readonly NOT_INITIALIZED: _h_ai_core.HaiErrorDef;
106
+ readonly UNSUPPORTED_TYPE: _h_ai_core.HaiErrorDef;
107
+ readonly CONFIG_ERROR: _h_ai_core.HaiErrorDef;
108
+ };
109
+ /**
110
+ * 可缓存的值类型
111
+ *
112
+ * 说明:`object` 包含普通对象与数组;函数、Symbol、BigInt 等不建议直接缓存。
113
+ */
114
+ type CacheValue = string | number | boolean | object | null;
115
+ /** 缓存设置选项 */
116
+ interface SetOptions {
117
+ /** 过期时间(秒) */
118
+ ex?: number;
119
+ /** 过期时间(毫秒) */
120
+ px?: number;
121
+ /** 过期时间点(Unix 时间戳,秒) */
122
+ exat?: number;
123
+ /** 过期时间点(Unix 时间戳,毫秒) */
124
+ pxat?: number;
125
+ /** 仅在键不存在时设置 */
126
+ nx?: boolean;
127
+ /** 仅在键存在时设置 */
128
+ xx?: boolean;
129
+ /** 保留原有的 TTL(与 ex/px/exat/pxat 互斥,冲突时以后端实现行为为准) */
130
+ keepTtl?: boolean;
131
+ }
132
+ /** 扫描选项 */
133
+ interface ScanOptions {
134
+ /** 匹配模式 */
135
+ match?: string;
136
+ /** 每次扫描数量 */
137
+ count?: number;
138
+ }
139
+ /** 有序集合成员(score + member 对) */
140
+ interface ZMember {
141
+ /** 分数,用于排序 */
142
+ score: number;
143
+ /** 成员标识 */
144
+ member: string;
145
+ }
146
+ /**
147
+ * 基础键值操作接口
148
+ *
149
+ * @example
150
+ * ```ts
151
+ * await cache.kv.set('key', { name: '张三' }, { ex: 3600 })
152
+ * const result = await cache.kv.get<{ name: string }>('key')
153
+ * ```
154
+ */
155
+ interface KvOperations {
156
+ /** 获取值;键不存在时返回 null */
157
+ get: <T = CacheValue>(key: string) => Promise<HaiResult<T | null>>;
158
+ /** 设置值;可通过 options 控制过期时间、NX/XX 条件等 */
159
+ set: (key: string, value: CacheValue, options?: SetOptions) => Promise<HaiResult<void>>;
160
+ /** 删除一个或多个键;返回实际删除的数量 */
161
+ del: (...keys: string[]) => Promise<HaiResult<number>>;
162
+ /** 检查一个或多个键是否存在;返回存在的数量 */
163
+ exists: (...keys: string[]) => Promise<HaiResult<number>>;
164
+ /** 设置过期时间(秒) */
165
+ expire: (key: string, seconds: number) => Promise<HaiResult<boolean>>;
166
+ /** 设置过期时间点(Unix 时间戳,秒) */
167
+ expireAt: (key: string, timestamp: number) => Promise<HaiResult<boolean>>;
168
+ /** 获取剩余过期时间(秒);-1 永不过期,-2 不存在 */
169
+ ttl: (key: string) => Promise<HaiResult<number>>;
170
+ /** 移除过期时间 */
171
+ persist: (key: string) => Promise<HaiResult<boolean>>;
172
+ /** 自增 1;键不存在时从 0 开始;值非数字时返回 OPERATION_FAILED */
173
+ incr: (key: string) => Promise<HaiResult<number>>;
174
+ /** 自增指定值;键不存在时从 0 开始 */
175
+ incrBy: (key: string, increment: number) => Promise<HaiResult<number>>;
176
+ /** 自减 1;键不存在时从 0 开始 */
177
+ decr: (key: string) => Promise<HaiResult<number>>;
178
+ /** 自减指定值;键不存在时从 0 开始 */
179
+ decrBy: (key: string, decrement: number) => Promise<HaiResult<number>>;
180
+ /** 批量获取;不存在的键对应位置返回 null */
181
+ mget: <T = CacheValue>(...keys: string[]) => Promise<HaiResult<(T | null)[]>>;
182
+ /** 批量设置键值对 */
183
+ mset: (entries: Array<[string, CacheValue]>) => Promise<HaiResult<void>>;
184
+ /** 游标扫描键;返回 [下一游标, 匹配的键列表],游标为 0 时表示遍历完成 */
185
+ scan: (cursor: number, options?: ScanOptions) => Promise<HaiResult<[number, string[]]>>;
186
+ /** 获取匹配模式的所有键(慎用,生产环境请用 scan 迭代) */
187
+ keys: (pattern: string) => Promise<HaiResult<string[]>>;
188
+ /** 获取键的数据类型;不存在时返回 "none" */
189
+ type: (key: string) => Promise<HaiResult<string>>;
190
+ }
191
+ /**
192
+ * Hash 操作接口
193
+ *
194
+ * @example
195
+ * ```ts
196
+ * await cache.hash.hset('user:1', { name: '张三', age: 25 })
197
+ * const name = await cache.hash.hget<string>('user:1', 'name')
198
+ * ```
199
+ */
200
+ interface HashOperations {
201
+ /** 获取字段值;字段不存在时返回 null */
202
+ hget: <T = CacheValue>(key: string, field: string) => Promise<HaiResult<T | null>>;
203
+ /** 设置字段值(单字段或批量);返回新增的字段数(已存在的字段不计入) */
204
+ hset: ((key: string, field: string, value: CacheValue) => Promise<HaiResult<number>>) & ((key: string, data: Record<string, CacheValue>) => Promise<HaiResult<number>>);
205
+ /** 删除字段 */
206
+ hdel: (key: string, ...fields: string[]) => Promise<HaiResult<number>>;
207
+ /** 检查字段是否存在 */
208
+ hexists: (key: string, field: string) => Promise<HaiResult<boolean>>;
209
+ /** 获取所有字段和值;键不存在时返回空对象 */
210
+ hgetall: <T = Record<string, CacheValue>>(key: string) => Promise<HaiResult<T>>;
211
+ /** 获取所有字段名;键不存在时返回空数组 */
212
+ hkeys: (key: string) => Promise<HaiResult<string[]>>;
213
+ /** 获取所有字段值;键不存在时返回空数组 */
214
+ hvals: <T = CacheValue>(key: string) => Promise<HaiResult<T[]>>;
215
+ /** 获取字段数量;键不存在时返回 0 */
216
+ hlen: (key: string) => Promise<HaiResult<number>>;
217
+ /** 批量获取字段值;不存在的字段对应位置返回 null */
218
+ hmget: <T = CacheValue>(key: string, ...fields: string[]) => Promise<HaiResult<(T | null)[]>>;
219
+ /** 字段自增;字段不存在时从 0 开始 */
220
+ hincrBy: (key: string, field: string, increment: number) => Promise<HaiResult<number>>;
221
+ }
222
+ /**
223
+ * List 操作接口
224
+ *
225
+ * @example
226
+ * ```ts
227
+ * await cache.list.lpush('queue', 'task1', 'task2')
228
+ * const item = await cache.list.rpop<string>('queue')
229
+ * ```
230
+ */
231
+ interface ListOperations {
232
+ /** 从左侧推入 */
233
+ lpush: (key: string, ...values: CacheValue[]) => Promise<HaiResult<number>>;
234
+ /** 从右侧推入 */
235
+ rpush: (key: string, ...values: CacheValue[]) => Promise<HaiResult<number>>;
236
+ /** 从左侧弹出 */
237
+ lpop: <T = CacheValue>(key: string) => Promise<HaiResult<T | null>>;
238
+ /** 从右侧弹出 */
239
+ rpop: <T = CacheValue>(key: string) => Promise<HaiResult<T | null>>;
240
+ /** 获取列表长度 */
241
+ llen: (key: string) => Promise<HaiResult<number>>;
242
+ /** 获取指定范围的元素;支持负索引(-1 为最后一个);键不存在时返回空数组 */
243
+ lrange: <T = CacheValue>(key: string, start: number, stop: number) => Promise<HaiResult<T[]>>;
244
+ /** 获取指定索引的元素;支持负索引;不存在时返回 null */
245
+ lindex: <T = CacheValue>(key: string, index: number) => Promise<HaiResult<T | null>>;
246
+ /** 设置指定索引的元素;键不存在或索引越界时返回错误 */
247
+ lset: (key: string, index: number, value: CacheValue) => Promise<HaiResult<void>>;
248
+ /** 保留指定范围的元素,范围外的元素被删除 */
249
+ ltrim: (key: string, start: number, stop: number) => Promise<HaiResult<void>>;
250
+ /** 阻塞式从左侧弹出;返回 [key, value] 或超时返回 null。memory 实现为非阻塞立即返回。 */
251
+ blpop: <T = CacheValue>(timeout: number, ...keys: string[]) => Promise<HaiResult<[string, T] | null>>;
252
+ /** 阻塞式从右侧弹出;返回 [key, value] 或超时返回 null。memory 实现为非阻塞立即返回。 */
253
+ brpop: <T = CacheValue>(timeout: number, ...keys: string[]) => Promise<HaiResult<[string, T] | null>>;
254
+ }
255
+ /**
256
+ * Set 操作接口
257
+ *
258
+ * @example
259
+ * ```ts
260
+ * await cache.set_.sadd('tags', 'redis', 'cache')
261
+ * const members = await cache.set_.smembers<string>('tags')
262
+ * ```
263
+ */
264
+ interface SetOperations {
265
+ /** 添加成员;返回新增的成员数(已存在的不计入) */
266
+ sadd: (key: string, ...members: CacheValue[]) => Promise<HaiResult<number>>;
267
+ /** 移除成员;返回实际移除的数量 */
268
+ srem: (key: string, ...members: CacheValue[]) => Promise<HaiResult<number>>;
269
+ /** 获取所有成员;键不存在时返回空数组 */
270
+ smembers: <T = CacheValue>(key: string) => Promise<HaiResult<T[]>>;
271
+ /** 检查是否为集合成员 */
272
+ sismember: (key: string, member: CacheValue) => Promise<HaiResult<boolean>>;
273
+ /** 获取成员数量;键不存在时返回 0 */
274
+ scard: (key: string) => Promise<HaiResult<number>>;
275
+ /** 随机获取成员;不指定 count 返回单个值,指定 count 返回数组;空集合返回 null */
276
+ srandmember: <T = CacheValue>(key: string, count?: number) => Promise<HaiResult<T | T[] | null>>;
277
+ /** 随机弹出成员(会从集合中移除);不指定 count 返回单个值,指定 count 返回数组;空集合返回 null */
278
+ spop: <T = CacheValue>(key: string, count?: number) => Promise<HaiResult<T | T[] | null>>;
279
+ /** 集合交集;任一 key 不存在时返回空数组 */
280
+ sinter: <T = CacheValue>(...keys: string[]) => Promise<HaiResult<T[]>>;
281
+ /** 集合并集 */
282
+ sunion: <T = CacheValue>(...keys: string[]) => Promise<HaiResult<T[]>>;
283
+ /** 集合差集(第一个集合减去其余集合) */
284
+ sdiff: <T = CacheValue>(...keys: string[]) => Promise<HaiResult<T[]>>;
285
+ }
286
+ /**
287
+ * 有序集合操作接口
288
+ *
289
+ * @example
290
+ * ```ts
291
+ * await cache.zset.zadd('rank', { score: 100, member: 'player1' })
292
+ * const top = await cache.zset.zrevrange('rank', 0, 9, true)
293
+ * ```
294
+ */
295
+ interface ZSetOperations {
296
+ /** 添加成员;已存在的成员只更新分数不计入新增数;返回新增的成员数 */
297
+ zadd: (key: string, ...members: ZMember[]) => Promise<HaiResult<number>>;
298
+ /** 移除成员;返回实际移除的数量 */
299
+ zrem: (key: string, ...members: string[]) => Promise<HaiResult<number>>;
300
+ /** 获取成员分数;成员不存在时返回 null */
301
+ zscore: (key: string, member: string) => Promise<HaiResult<number | null>>;
302
+ /** 获取成员升序排名(0-based);成员不存在时返回 null */
303
+ zrank: (key: string, member: string) => Promise<HaiResult<number | null>>;
304
+ /** 获取成员降序排名(0-based);成员不存在时返回 null */
305
+ zrevrank: (key: string, member: string) => Promise<HaiResult<number | null>>;
306
+ /** 获取指定排名范围的成员(升序);withScores=true 时返回 ZMember[];键不存在时返回空数组 */
307
+ zrange: (key: string, start: number, stop: number, withScores?: boolean) => Promise<HaiResult<string[] | ZMember[]>>;
308
+ /** 获取指定排名范围的成员(降序);withScores=true 时返回 ZMember[] */
309
+ zrevrange: (key: string, start: number, stop: number, withScores?: boolean) => Promise<HaiResult<string[] | ZMember[]>>;
310
+ /** 获取指定分数范围的成员;min/max 支持 '-inf'/'+inf';可通过 offset/count 分页 */
311
+ zrangeByScore: (key: string, min: number | string, max: number | string, options?: {
312
+ withScores?: boolean;
313
+ offset?: number;
314
+ count?: number;
315
+ }) => Promise<HaiResult<string[] | ZMember[]>>;
316
+ /** 获取成员数量;键不存在时返回 0 */
317
+ zcard: (key: string) => Promise<HaiResult<number>>;
318
+ /** 获取指定分数范围内的成员数量;min/max 支持 '-inf'/'+inf' */
319
+ zcount: (key: string, min: number | string, max: number | string) => Promise<HaiResult<number>>;
320
+ /** 增加成员分数;成员不存在时以 0 为初始值;返回更新后的分数 */
321
+ zincrBy: (key: string, increment: number, member: string) => Promise<HaiResult<number>>;
322
+ /** 移除指定排名范围的成员;返回实际移除的数量 */
323
+ zremRangeByRank: (key: string, start: number, stop: number) => Promise<HaiResult<number>>;
324
+ /** 移除指定分数范围的成员;返回实际移除的数量 */
325
+ zremRangeByScore: (key: string, min: number | string, max: number | string) => Promise<HaiResult<number>>;
326
+ }
327
+ /**
328
+ * 分布式锁操作接口
329
+ *
330
+ * 基于 SET NX EX 模式实现分布式互斥锁,适用于多节点部署场景。
331
+ *
332
+ * @example
333
+ * ```ts
334
+ * const acquired = await cache.lock.acquire('my-lock', { ttl: 30 })
335
+ * if (acquired.success && acquired.data) {
336
+ * try {
337
+ * // 执行受保护操作
338
+ * } finally {
339
+ * await cache.lock.release('my-lock')
340
+ * }
341
+ * }
342
+ * ```
343
+ */
344
+ interface LockOperations {
345
+ /**
346
+ * 尝试获取分布式锁
347
+ *
348
+ * 通过 SET NX EX 原子操作实现。获锁成功返回 true,锁已被持有返回 false。
349
+ *
350
+ * @param key - 锁键名
351
+ * @param options - 锁选项
352
+ * @returns true 表示获锁成功,false 表示锁已被其他持有者持有
353
+ */
354
+ acquire: (key: string, options?: LockOptions) => Promise<HaiResult<boolean>>;
355
+ /**
356
+ * 释放分布式锁
357
+ *
358
+ * 仅当锁由当前持有者持有时才释放(通过 owner 比对),防止误释放他人的锁。
359
+ *
360
+ * @param key - 锁键名
361
+ * @param owner - 锁持有者标识(须与 acquire 时一致);未传则强制释放
362
+ * @returns true 表示释放成功,false 表示锁不存在或非当前持有者
363
+ */
364
+ release: (key: string, owner?: string) => Promise<HaiResult<boolean>>;
365
+ /**
366
+ * 检查锁是否被持有
367
+ *
368
+ * @param key - 锁键名
369
+ * @returns true 表示锁存在且未过期
370
+ */
371
+ isLocked: (key: string) => Promise<HaiResult<boolean>>;
372
+ /**
373
+ * 续期锁的过期时间
374
+ *
375
+ * 仅当锁由当前持有者持有时才续期。
376
+ *
377
+ * @param key - 锁键名
378
+ * @param ttl - 新的过期时间(秒)
379
+ * @param owner - 锁持有者标识;未传则直接续期
380
+ * @returns true 表示续期成功,false 表示锁不存在或非当前持有者
381
+ */
382
+ extend: (key: string, ttl: number, owner?: string) => Promise<HaiResult<boolean>>;
383
+ }
384
+ /** 分布式锁选项 */
385
+ interface LockOptions {
386
+ /** 锁过期时间(秒);默认 30 */
387
+ ttl?: number;
388
+ /** 锁持有者标识;默认 'default'(多节点部署时建议设置固定值以便审计) */
389
+ owner?: string;
390
+ }
391
+ /**
392
+ * 复合缓存操作接口,聚合所有数据结构的操作子接口
393
+ *
394
+ * 通过 `cache.kv` / `cache.hash` / `cache.list` / `cache.set_` / `cache.zset` / `cache.lock` 分别访问
395
+ */
396
+ interface CacheCompositeOperations {
397
+ /** KV 操作 */
398
+ readonly kv: KvOperations;
399
+ /** Hash 操作 */
400
+ readonly hash: HashOperations;
401
+ /** List 操作 */
402
+ readonly list: ListOperations;
403
+ /** Set 操作 */
404
+ readonly set_: SetOperations;
405
+ /** SortedSet 操作 */
406
+ readonly zset: ZSetOperations;
407
+ /** 分布式锁操作 */
408
+ readonly lock: LockOperations;
409
+ /** 测试连接 */
410
+ ping: () => Promise<HaiResult<string>>;
411
+ }
412
+ /**
413
+ * 缓存函数接口
414
+ *
415
+ * @example
416
+ * ```ts
417
+ * import { cache } from '@h-ai/cache'
418
+ *
419
+ * await cache.init({ type: 'redis', host: 'localhost' })
420
+ * await cache.kv.set('key', 'value', { ex: 3600 })
421
+ * const result = await cache.kv.get('key')
422
+ * await cache.close()
423
+ * ```
424
+ */
425
+ interface CacheFunctions extends CacheCompositeOperations {
426
+ /** 初始化缓存连接;会先关闭已有连接再以新配置重新初始化 */
427
+ init: (config: CacheConfigInput) => Promise<HaiResult<void>>;
428
+ /** 当前配置(parse 后);未初始化时为 null */
429
+ readonly config: CacheConfig | null;
430
+ /** 是否已初始化并连接 */
431
+ readonly isInitialized: boolean;
432
+ /** 关闭连接并清理资源;未初始化时调用安全无副作用 */
433
+ close: () => Promise<void>;
434
+ }
435
+ /**
436
+ * 缓存 Provider 接口
437
+ *
438
+ * 由具体实现(memory / redis)提供;上层通过 cache-main 统一管理生命周期。
439
+ * 此接口仅供模块内部使用,消费者请使用 {@link CacheFunctions}。
440
+ *
441
+ * @internal
442
+ */
443
+ interface CacheProvider extends CacheCompositeOperations {
444
+ /** Provider 名称(如 'memory' / 'redis') */
445
+ readonly name: string;
446
+ /** 连接缓存服务;config 已经过 Zod Schema 校验 */
447
+ connect: (config: CacheConfig) => Promise<HaiResult<void>>;
448
+ /** 关闭连接并释放资源 */
449
+ close: () => Promise<void>;
450
+ /** 是否处于已连接状态 */
451
+ isConnected: () => boolean;
452
+ }
453
+
454
+ /**
455
+ * @h-ai/cache — 缓存服务主入口
456
+ *
457
+ * 提供统一的 `cache` 对象,聚合所有缓存操作功能。
458
+ * @module cache-main
459
+ */
460
+
461
+ /**
462
+ * 缓存服务对象
463
+ *
464
+ * @example
465
+ * ```ts
466
+ * import { cache } from '@h-ai/cache'
467
+ *
468
+ * await cache.init({ type: 'redis', host: 'localhost', port: 6379 })
469
+ * await cache.kv.set('user:1', { name: '张三' }, { ex: 3600 })
470
+ * const result = await cache.kv.get<{ name: string }>('user:1')
471
+ * await cache.close()
472
+ * ```
473
+ */
474
+ declare const cache: CacheFunctions;
475
+
476
+ export { type CacheCompositeOperations, type CacheConfig, type CacheConfigInput, CacheConfigSchema, type CacheFunctions, type CacheProvider, type CacheValue, HaiCacheError, type HashOperations, type KvOperations, type ListOperations, type LockOperations, type LockOptions, MemoryConfigSchema, type RedisClusterNode, RedisClusterNodeSchema, RedisConfigSchema, type RedisSentinelConfig, RedisSentinelConfigSchema, type ScanOptions, type SetOperations, type SetOptions, type ZMember, type ZSetOperations, cache };