First of all, I am aware that react-cache package is highly experimental and unstable, but I still would like to experiment and express my thoughts on the subject. (skip to the end for the actual question)
I've tried using it in several fun projects, and in my experience current version works pretty well with anything static, e.g. resources that need to be loaded only once, or can be loaded once and updated separately.
However, not all resources are like that, and some of them need cache invalidation. This #14783 (comment) mentions that there is no API for invalidation, but you can actually invalidate the resource, using the clever hash function. I've put together a little demo.
The problem with that approach is that invalidations happens "soundly" and will trigger the suspense.
This could be solved by busting the cache only after the preload of the next entry already happened, but there's unfortunately no way to track the preload state of the next entry, since the preload method actually doesn't return the promise it called;
Another way to go around this issue is to have a side effect inside the async resource, that would cause a re-render when completed, thus giving the ability to invalidate the old entry only after the new entry has been completed;
Lastly there’s the ability to create a invalidation method, wrapping a read method in a try-catch block and have custom logic of resolving with a result / awaiting a suspender. This however makes the invalidation async, but it's probably fine, since we only need to invalidate the cache after we already have the initial result and can update the result when the invalidation has already happened.
Since the first approach is basically impossible with current cache package and later two required a lot of custom logic, one would probably be better of writing an entirely custom suspender and cache provider with a required strategy.
Sorry for the long read, thus comes the question:
TLDR: Is react-cache being developed and presented as a low-level API for writing custom resource caching logic OR as an example of writing custom suspenders and cache strategies?
First of all, I am aware that
react-cachepackage is highly experimental and unstable, but I still would like to experiment and express my thoughts on the subject. (skip to the end for the actual question)I've tried using it in several fun projects, and in my experience current version works pretty well with anything static, e.g. resources that need to be loaded only once, or can be loaded once and updated separately.
However, not all resources are like that, and some of them need cache invalidation. This #14783 (comment) mentions that there is no API for invalidation, but you can actually invalidate the resource, using the clever hash function. I've put together a little demo.
The problem with that approach is that invalidations happens "soundly" and will trigger the suspense.
This could be solved by busting the cache only after the
preloadof the next entry already happened, but there's unfortunately no way to track thepreloadstate of the next entry, since thepreloadmethod actually doesn't return the promise it called;Another way to go around this issue is to have a side effect inside the async resource, that would cause a re-render when completed, thus giving the ability to invalidate the old entry only after the new entry has been completed;
Lastly there’s the ability to create a invalidation method, wrapping a
readmethod in a try-catch block and have custom logic of resolving with a result / awaiting a suspender. This however makes the invalidation async, but it's probably fine, since we only need to invalidate the cache after we already have the initial result and can update the result when the invalidation has already happened.Since the first approach is basically impossible with current cache package and later two required a lot of custom logic, one would probably be better of writing an entirely custom suspender and cache provider with a required strategy.
Sorry for the long read, thus comes the question:
TLDR: Is
react-cachebeing developed and presented as a low-level API for writing custom resource caching logic OR as an example of writing custom suspenders and cache strategies?