Skip to content

Commit

Permalink
Merge pull request #97 from upstash/cache-fix
Browse files Browse the repository at this point in the history
pop identifier from cache in resetTokens calls
  • Loading branch information
ogzhanolguncu authored Apr 15, 2024
2 parents 1cb10c3 + 1ba7e3b commit c326516
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export class Cache implements EphemeralCache {
return value;
}

public pop(key: string): void {
this.cache.delete(key)
}

public empty(): void {
this.cache.clear()
}
Expand Down
6 changes: 6 additions & 0 deletions src/multi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ export class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
},
async resetTokens(ctx: MultiRegionContext, identifier: string) {
const pattern = [identifier, "*"].join(":");
if (ctx.cache) {
ctx.cache.pop(identifier)
}
for (const db of ctx.redis) {
await db.eval(resetScript, [pattern], [null]);
}
Expand Down Expand Up @@ -470,6 +473,9 @@ export class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
},
async resetTokens(ctx: MultiRegionContext, identifier: string) {
const pattern = [identifier, "*"].join(":");
if (ctx.cache) {
ctx.cache.pop(identifier)
}
for (const db of ctx.redis) {
await db.eval(resetScript, [pattern], [null]);
}
Expand Down
14 changes: 10 additions & 4 deletions src/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ export class RegionRatelimit extends Ratelimit<RegionContext> {
},
async resetTokens(ctx: RegionContext, identifier: string) {
const pattern = [identifier, "*"].join(":");

if (ctx.cache) {
ctx.cache.pop(identifier)
}
await ctx.redis.eval(resetScript, [pattern], [null]);
},
});
Expand Down Expand Up @@ -284,7 +286,9 @@ export class RegionRatelimit extends Ratelimit<RegionContext> {
},
async resetTokens(ctx: RegionContext, identifier: string) {
const pattern = [identifier, "*"].join(":");

if (ctx.cache) {
ctx.cache.pop(identifier)
}
await ctx.redis.eval(resetScript, [pattern], [null]);
},
});
Expand Down Expand Up @@ -370,7 +374,9 @@ export class RegionRatelimit extends Ratelimit<RegionContext> {
},
async resetTokens(ctx: RegionContext, identifier: string) {
const pattern = identifier;

if (ctx.cache) {
ctx.cache.pop(identifier)
}
await ctx.redis.eval(resetScript, [pattern], [null]);
},
});
Expand Down Expand Up @@ -486,7 +492,7 @@ export class RegionRatelimit extends Ratelimit<RegionContext> {
if (!ctx.cache) {
throw new Error("This algorithm requires a cache");
}
ctx.cache.empty()
ctx.cache.pop(identifier)
await ctx.redis.eval(resetScript, [pattern], [null]);
},
});
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface EphemeralCache {

incr: (key: string) => number;

pop: (key: string) => void;
empty: () => void;
}

Expand Down

0 comments on commit c326516

Please sign in to comment.