S
S
SagePtr2016-11-04 00:54:38
Node.js
SagePtr, 2016-11-04 00:54:38

Cache without race condition on node.js?

One of the big caching problems, in my opinion, is when two asynchronous functions access the cache at the same time for the same value that is not in the cache. As a result, they both begin to calculate this value without a cache, and then put it into the cache at the end.
Are there any ready-made modules on nodejs that resolve this situation?
I need something like this behavior:

Алиса: get x (id=5, callback=callback1) --> кеш запоминает, что id=5 неудачно запрашивалось и скоро будет записано, вызывает callback1(false)
Алиса начинает вычислять этот x(id=5)
Боб: get x (id=5, callback=callback2) --> кеш знает, что Алиса скоро вычислит x(id=5) и ничего не возвращает Бобу
Карл: get x(id=5, callback=callback3) --> аналогичное поведение, как на предыдущем шаге
Алиса: set x(id=5, result=123) --> кеш запоминает результат для id=5, и возвращает результат в колбэки, которые скопились в очереди (callback2, callback3).
Боб: получает callback2(result=123)
Карл: получает callback3(result=123)

And also the ability to expire the wait if there has not been a request to write to the cache for a long time (to return false to the first callback from the queue, to wait further for the next).

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question