|
1
|
import type { |
|
2
|
InternalModuleDeclaration, |
|
3
|
LoaderOptions, |
|
4
|
ModulesSdkTypes, |
|
5
|
} from "@medusajs/framework/types" |
|
6
|
import { RedisCacheModuleOptions } from "@types" |
|
7
|
import Redis, { type RedisOptions } from "ioredis" |
|
8
|
|
|
9
|
export default async ( |
|
10
|
{ |
|
11
|
container, |
|
12
|
logger, |
|
13
|
options, |
|
14
|
}: LoaderOptions< |
|
15
|
( |
|
16
|
| ModulesSdkTypes.ModuleServiceInitializeOptions |
|
17
|
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions |
|
18
|
) & { logger?: any } |
|
19
|
>, |
|
20
|
moduleDeclaration?: InternalModuleDeclaration |
|
21
|
): Promise<void> => { |
|
22
|
const logger_ = logger || console |
|
23
|
|
|
24
|
const moduleOptions = (options ?? |
|
25
|
moduleDeclaration?.options ?? |
|
26
|
{}) as RedisCacheModuleOptions |
|
27
|
|
|
28
|
const { |
|
29
|
redisUrl, |
|
30
|
redisOptions: newRedisOptions, |
|
31
|
// Module options, not ioredis options. Pulled out so they are not |
|
32
|
// forwarded to the client along with the deprecated top-level options. |
|
33
|
ttl: _ttl, |
|
34
|
prefix: _prefix, |
|
35
|
compressionThreshold: _compressionThreshold, |
|
36
|
...deprecatedRedisOptions |
|
37
|
} = moduleOptions |
|
38
|
if (!redisUrl) { |
|
39
|
throw new Error("[caching-redis] redisUrl is required") |
|
40
|
} |
|
41
|
|
|
42
|
// Handle backward compatibility for deprecated options |
|
43
|
if (!newRedisOptions && Object.keys(deprecatedRedisOptions).length) { |
|
44
|
logger_.warn( |
|
45
|
"[caching-redis] Passing ioredis options at the top level of the module options is deprecated. Please use `redisOptions` instead for consistency with other modules." |
|
46
|
) |
|
47
|
} |
|
48
|
|
|
49
|
let redisClient: Redis |
|
50
|
|
|
51
|
const redisOptions: RedisOptions = { |
|
52
|
connectTimeout: 10000, |
|
53
|
commandTimeout: 5000, |
|
54
|
lazyConnect: true, |
|
55
|
maxRetriesPerRequest: 3, |
|
56
|
enableOfflineQueue: true, |
|
57
|
connectionName: "medusa-cache-redis", |
|
58
|
...(newRedisOptions ?? deprecatedRedisOptions), |
|
59
|
} |
|
60
|
|
|
61
|
redisClient = new Redis(redisUrl!, redisOptions) |
|
62
|
|
|
63
|
// Handle connection errors gracefully |
|
64
|
redisClient.on("error", (error) => { |
|
65
|
logger_.warn(`Redis cache connection error: ${error.message}`) |
|
66
|
}) |
|
67
|
|
|
68
|
redisClient.on("connect", () => { |
|
69
|
logger_.info("Redis cache connection established successfully") |
|
70
|
}) |
|
71
|
|
|
72
|
redisClient.on("ready", () => { |
|
73
|
logger_.info("Redis cache is ready to accept commands") |
|
74
|
}) |
|
75
|
|
|
76
|
redisClient.on("close", () => { |
|
77
|
logger_.warn("Redis cache connection closed") |
|
78
|
}) |
|
79
|
|
|
80
|
redisClient.on("reconnecting", () => { |
|
81
|
logger_.info("Redis cache attempting to reconnect...") |
|
82
|
}) |
|
83
|
|
|
84
|
try { |
|
85
|
// Test connection with timeout |
|
86
|
await redisClient.ping() |
|
87
|
logger_.info("Redis cache connection test successful") |
|
88
|
} catch (error) { |
|
89
|
logger_.warn( |
|
90
|
`Redis cache connection test failed: ${error.message}, but continuing with lazy connection` |
|
91
|
) |
|
92
|
} |
|
93
|
|
|
94
|
container.register({ |
|
95
|
redisClient: { |
|
96
|
resolve: () => redisClient, |
|
97
|
}, |
|
98
|
prefix: { |
|
99
|
resolve: () => moduleOptions.prefix ?? "mc:", |
|
100
|
}, |
|
101
|
logger: { |
|
102
|
resolve: () => logger_, |
|
103
|
}, |
|
104
|
}) |
|
105
|
} |