Skip to content

Commit 862d796

Browse files
committed
fix: resolve Redis Cluster CROSSSLOT error in cache deletions
1 parent e474793 commit 862d796

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/lib/cache.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,17 @@ export async function deleteCache(...keys: string[]): Promise<void> {
114114

115115
const startTime = Date.now();
116116
try {
117-
await cache.del(...keys);
117+
// In cluster mode, keys may hash to different slots
118+
// Use pipeline to delete individually (more efficient than separate awaits)
119+
if (keys.length === 1) {
120+
await cache.del(keys[0]);
121+
} else {
122+
const pipeline = cache.pipeline();
123+
for (const key of keys) {
124+
pipeline.del(key);
125+
}
126+
await pipeline.exec();
127+
}
118128
const duration = Date.now() - startTime;
119129

120130
// Log cache operation with metrics (use first key as representative)
@@ -149,11 +159,15 @@ export async function deleteCachePattern(pattern: string): Promise<void> {
149159
}
150160

151161
if (keys.length > 0) {
152-
// Delete in batches to avoid overwhelming the cluster
162+
// Delete in batches using pipeline (cluster mode compatible)
153163
const batchSize = 100;
154164
for (let i = 0; i < keys.length; i += batchSize) {
155165
const batch = keys.slice(i, i + batchSize);
156-
await cache.del(...batch);
166+
const pipeline = cache.pipeline();
167+
for (const key of batch) {
168+
pipeline.del(key);
169+
}
170+
await pipeline.exec();
157171
}
158172

159173
logger.info(`Deleted cache keys matching pattern`, {

src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// This file is automatically updated by semantic-release
2-
export const VERSION = "1.8.0"
2+
export const VERSION = "1.8.0";

0 commit comments

Comments
 (0)