1

I had a question. The following code has a memory leak:

let inMemoryCache = {};

app.get("/hello",(req, resp) => {
   inMemoryCache[unixTimeStamp] = {"foo":"bar"}
   resp.json({});
});

Isn't it? The size of the object inMemoryCache will keep on increasing with each request until it hits the ceiling and the heap size explodes.

What then is the best to implement in-memory-caches?

1 Answer 1

2

Manage the size of your cache somehow. For example, you could keep a fixed number of entries, and once the cache has reached its maximum size and a new entry comes in, delete the oldest (or least frequently used, or least recently used, or some other selection criteria).

Caching is harder than it seems :-)

Sign up to request clarification or add additional context in comments.

7 Comments

Yeah, I realized this recently. I was reading about weakmap While I am not clear enough, but does that help here?
WeakMaps don't keep their contents "alive" in the GC sense, i.e. if there are no other references to them, then the next GC cycle will clear them. Whether that's desirable behavior very much depends on what you want your cache to do.
I specifically want to cache the results from database calls. Like big JSON documents. What will you suggest?
I don't know enough about your situation to recommend something specific, so I can only recommend a generic strategy: try any one approach that seems reasonable to you, measure how well it works, and if it's not satisfactory, try something else. Repeat as needed.
Yeah, doing the same!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.