Next.js Discord

Discord Forum

Routes access different instances of an object

Unanswered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
I create this file for cache:
const cache: any = {}; // eslint-disable-line @typescript-eslint/no-explicit-any

export const customCache: {
  getDataFromCache(key: string): {};
  setDataInCache(key: string, value: {}): void;
} = {
  getDataFromCache: function(key: string) {
    return cache[key];
  },

  setDataInCache: function(key: string, value: {}) {
    cache[key] = value;
  },
};


Then in a new file I create special object, where I use customCache.getDataFromCache and customCache.setDataInCache, then I use this object in two api routes. But these routes access different instances of the customCache object. What could be the problem?

4 Replies

Is this when deployed to vercel / serverless architecture?
If so, every api route is its own lambda func
so it wont have access to the memory of another api route
Asiatic LionOP
No, I run application on localhost. I deploy this application in docker container.