diff --git a/admin/src/lib/redis.ts b/admin/src/lib/redis.ts index 42b364d..7bf0303 100644 --- a/admin/src/lib/redis.ts +++ b/admin/src/lib/redis.ts @@ -3,7 +3,8 @@ * 支持单节点和集群模式 * 前缀:admin:* */ -import Redis, { default as RedisCluster } from "ioredis"; +import Redis, { default as Cluster } from "ioredis"; +import type { ClusterNode } from "ioredis"; import { REDIS_URL, REDIS_CLUSTER_URLS } from "./env"; // Admin 专用的 Redis 前缀 @@ -28,26 +29,25 @@ function createClusterClient(): Redis { return createSingleClient(); } - const nodes = REDIS_CLUSTER_URLS.map((url) => { + const nodes: ClusterNode[] = REDIS_CLUSTER_URLS.map((url) => { const u = new URL(url); return { host: u.hostname, port: parseInt(u.port || "6379", 10) }; }); const firstUrl = new URL(REDIS_CLUSTER_URLS[0]); - // ioredis 5.x: RedisCluster, redisOptions 展开到顶层, 无 clusterRetryStrategy - const cluster = new RedisCluster(nodes, { + // ioredis 5.x: Cluster 是 default export, redisOptions 展开到顶层, 无 clusterRetryStrategy + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const cluster = new (Cluster as any)(nodes, { lazyConnect: true, maxRetriesPerRequest: 3, - retryStrategy(times) { - return Math.min(times * 100, 3000); - }, + retryStrategy: (times: number) => Math.min(times * 100, 3000), // 从第一个 URL 提取认证信息(所有节点共用相同密码) username: firstUrl.username || undefined, password: firstUrl.password || undefined, }); - return cluster as unknown as Redis; + return cluster as Redis; } export function getRedis(): Redis { @@ -58,7 +58,7 @@ export function getRedis(): Redis { return redis; } -export function closeRedis(): Promise { +export function closeRedis(): Promise { if (redis) { return redis.quit(); }