Skip to content

Commit

Permalink
fix: to support cluster, do not use pipeline during invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Nuesch committed Oct 21, 2022
1 parent dc1077e commit 24c5994
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/CachedQuery/invalidation/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,24 @@ export default class InvalidationHandler {
const { keys, sets } = collected;
if ((!keys || !keys.length) && (!sets || !sets.length)) return;

const pipeline = this.context.redis.pipeline();
const { redis } = this.context;
const promises: Promise<string[] | 0 | 1>[] = [];
if (keys && keys.length) {
keys.forEach((key) => pipeline.delQuery(key));
promises.push(...keys.map((key) => redis.delQuery(key)));
}
if (sets && sets.length) {
sets.forEach(({ set, filter }) => pipeline.delQueriesIn(set, filter));
promises.push(...sets.map(({ set, filter }) => redis.delQueriesIn(set, filter)));
}
const result = await pipeline.exec();
const result = await Promise.allSettled(promises);

const { keysInvalidated } = this;
result?.forEach(([err, res], idx) => {
if (err) return;
result.forEach((settled, idx) => {
if (settled.status === 'rejected') return;
const res = settled.value;
if (res === 1) {
keysInvalidated.push(keys![idx]!);
} else if (Array.isArray(res) && res.length) {
keysInvalidated.push(...(res as string[]));
keysInvalidated.push(...res);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/mondis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import bindPlugin from './CachedQuery/invalidation/mongoose-plugin';
declare module 'ioredis' {
interface RedisCommander<Context> {
expiregt(key: string, ttl: number): Result<void, Context>;
delQuery(queryKey: string): Result<string[], Context>;
delQuery(queryKey: string): Result<0 | 1, Context>;
delQueriesIn(setKey: string, filterHashes?: string): Result<string[], Context>;
}
}
Expand Down

0 comments on commit 24c5994

Please sign in to comment.