Next.js Discord

Discord Forum

Using a map in edge functions. How long does it live?

Unanswered
Ocicat posted this in #help-forum
Open in Discord
OcicatOP
How much does something like this live in subsequent requests?
const edgeFnCacheMap = new Map<string, CachedData>();

export const runtime = "edge";

I query redis to populate it when it's empty for a specific key, and then try to use the cached content in subsequent requests. I saw sometimes it uses it, and sometimes it keeps querying redis, so I don't understand completely the lifecycle of an edge function and how can I use something like this for caching cache 😄

13 Replies

why are you using map instead of record…?
@DirtyCajunRice | AppDir why are you using map instead of record…?
OcicatOP
you mean a simple object?
@Ocicat you mean a simple object?
no i mean a record
The type
if your key is a string
and you have a typed value
why make a map
just do Record<string, CachedData>
OcicatOP
yeah, it's possible, does it solve the issue?
@Ocicat yeah, it's possible, does it solve the issue?
It is just the only oddity visible. Need to see more code to be able to tell why your redis is acting up.
@DirtyCajunRice | AppDir It is just the only oddity visible. Need to see more code to be able to tell why your redis is acting up.
OcicatOP
tried it with a Record. First 4-5 requests used a the cache, they could find it in the record, but then all of a sudden all the subsequent requests always fetch from redis and the record doesn't persist the newly gotten data. That's what happened with the map too, so I'm not sure it's the data structure
let cachedData = requestCache[id];
if (!cachedData) {
      cachedData =
        (await kv.get<CachedData>(id)) ?? undefined;
      requestCache[id] = cachedData;
}