Are JavaScript browser Cache objects made by CacheStorage threadsafe?

227 Views Asked by At

If I use CacheStorage.open() to get the same Cache in the UI thread as in a Worker thread, and begin reading/writing from/to it on both sides, is it safe? Or can there be race conditions? I'm hoping the Cache API is thread safe (I would assume that it is, like all else in JS).

1

There are 1 best solutions below

4
On

is it safe?

Yes. All methods return Promises, as the Cache might be managed by another thread, the Promise then resolves if the other thread performed the operation. There cannot be concurrent modifications or other such weird things.

Or can there be race conditions?

Whenever there are multiple threads there can be race conditions, thats in the nature of things. That means that if you add() a cache entry while trying to retrieve it woth get() from another WebWorker in parallel might or might not get you a result.