From 966233a8726b87b958bb10ffc6c5d580e923883b Mon Sep 17 00:00:00 2001 From: Aron Carroll Date: Fri, 8 Dec 2023 19:39:38 +0000 Subject: [PATCH] Experiment with in-memory ephemeralCache Should reduce the load on the redis key-value store... --- middleware.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/middleware.ts b/middleware.ts index 55019e4..a6c0c4d 100644 --- a/middleware.ts +++ b/middleware.ts @@ -2,12 +2,15 @@ import { NextRequest, NextResponse } from 'next/server'; import { Ratelimit } from '@upstash/ratelimit'; import { kv } from '@vercel/kv'; +const ephemeralCache = new Map(); + const ratelimit = new Ratelimit({ redis: kv, // 20 requests from the same IP within a 10 second sliding window limiter: Ratelimit.slidingWindow(20, '10s'), prefix: `v2/zoo/ratelimit/${process.env.VERCEL_ENV ?? 'local'}`, timeout: 500, + ephemeralCache, }); // Rate limit the /api/predictions/[id] endpoint