fix(admin): fix ioredis 5.x Cluster constructor type resolution
- Use `Cluster` as default export from ioredis (not RedisCluster named export) - Import ClusterNode type and use explicit type annotation on nodes array - Use `any` cast on Cluster constructor to bypass TS overload resolution issue - Fix closeRedis return type to Promise<unknown>
This commit is contained in:
parent
67e8c5edc7
commit
a9c51526b8
@ -3,7 +3,8 @@
|
|||||||
* 支持单节点和集群模式
|
* 支持单节点和集群模式
|
||||||
* 前缀:admin:*
|
* 前缀: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";
|
import { REDIS_URL, REDIS_CLUSTER_URLS } from "./env";
|
||||||
|
|
||||||
// Admin 专用的 Redis 前缀
|
// Admin 专用的 Redis 前缀
|
||||||
@ -28,26 +29,25 @@ function createClusterClient(): Redis {
|
|||||||
return createSingleClient();
|
return createSingleClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
const nodes = REDIS_CLUSTER_URLS.map((url) => {
|
const nodes: ClusterNode[] = REDIS_CLUSTER_URLS.map((url) => {
|
||||||
const u = new URL(url);
|
const u = new URL(url);
|
||||||
return { host: u.hostname, port: parseInt(u.port || "6379", 10) };
|
return { host: u.hostname, port: parseInt(u.port || "6379", 10) };
|
||||||
});
|
});
|
||||||
|
|
||||||
const firstUrl = new URL(REDIS_CLUSTER_URLS[0]);
|
const firstUrl = new URL(REDIS_CLUSTER_URLS[0]);
|
||||||
|
|
||||||
// ioredis 5.x: RedisCluster, redisOptions 展开到顶层, 无 clusterRetryStrategy
|
// ioredis 5.x: Cluster 是 default export, redisOptions 展开到顶层, 无 clusterRetryStrategy
|
||||||
const cluster = new RedisCluster(nodes, {
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
const cluster = new (Cluster as any)(nodes, {
|
||||||
lazyConnect: true,
|
lazyConnect: true,
|
||||||
maxRetriesPerRequest: 3,
|
maxRetriesPerRequest: 3,
|
||||||
retryStrategy(times) {
|
retryStrategy: (times: number) => Math.min(times * 100, 3000),
|
||||||
return Math.min(times * 100, 3000);
|
|
||||||
},
|
|
||||||
// 从第一个 URL 提取认证信息(所有节点共用相同密码)
|
// 从第一个 URL 提取认证信息(所有节点共用相同密码)
|
||||||
username: firstUrl.username || undefined,
|
username: firstUrl.username || undefined,
|
||||||
password: firstUrl.password || undefined,
|
password: firstUrl.password || undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
return cluster as unknown as Redis;
|
return cluster as Redis;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRedis(): Redis {
|
export function getRedis(): Redis {
|
||||||
@ -58,7 +58,7 @@ export function getRedis(): Redis {
|
|||||||
return redis;
|
return redis;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function closeRedis(): Promise<void> {
|
export function closeRedis(): Promise<unknown> {
|
||||||
if (redis) {
|
if (redis) {
|
||||||
return redis.quit();
|
return redis.quit();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user