-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Custom Cache Support #212
Comments
Do you think it's possible to support for async cache (maybe multilayer cache as well)? I've been thinking for a while and have some ideas to extend those with the same API. So for async (multlayer) cache,
|
I like your idea for async cache, I think having a sync way to read cache it's always better to avoid running an effect to get the currently cached data, and since we know the cache key it could be possible to run mutate and fill the cache correctly.
I'm not sure about that, since the result it's probably going to be an object or array in most APIs the check will always return |
@sergiodxa I think we have to rely on deep comparison. |
Add the option to specify your own comparator? |
@aequasi if you implement your own cache you could use your own comparator, this way a cache could be faster because it doesn't use deep comparison or could be more correct because it does it, so you could pick between two similar options based on that, depending on your requirements. |
What about not offering "cache" support and instead exposing a few simple lifecycle methods and events for the following:
With these exposed then there will be the possibility for everyone to create their own implementation or cache how they wish. |
@shuding Are there docs anywhere for using the cache? Thanks! |
@nandorojo there is no doc yet, but you can read the code https://github.com/zeit/swr/blob/master/src/cache.ts, it’s short |
@sergiodxa Looks like the cache isn't async, is there a way to use it with something like React Native's |
Any update on this? I'm looking to use React Native's AsyncStorage to persist the cache as well |
Goal
Enable SWR users to control the cache.
This is based on the Cache collection PR and other issues such as #4, #16, #158, #161.
Background
Right now there is no way to control the cache used by SWR, this means:
Proposal
I propose an API to support changing the cache through the SWRConfig component.
With this,
App
and any component inside it callinguseSWR
will use this cache implementation.The custom cache should only be customized with
SWRConfig
and not in a peruseSWR
instance, if you want to use a custom cache in a certain instance the parent should wrap the component inSWRConfig
, this way the components are not aware of the cache mechanism used.Using multiple caches
This could also allow using multiple cache implementations in different parts of the app, you could render another
SWRConfig
deep inside the component tree of App to change the cache in that branch of the tree.When this could be useful? Maybe we now certain parts should be cached offline (e.g with localStorage or IndexedDB) and others should be cached in-memory, we will be able to customize it.
Default Cache
By default SWR will come with a cache implementation, e.g. it could use the one implemented in #92, this will allow most projects to don't care about the cache implementation.
Testing
If we are testing components calling
useSWR
we could wrap the render of the component inSWRConfig
with their own cache instance.Now every test will use their on in-memory cache and will not share previously fetched data between them.
Using a new cache instance per test will simplify running tests in parallel without causing any issue because we clear the shared cache before another test finished first.
Controlling the Cached Data
Since we could create the cache instance in their own module we should be able to import it anywhere and control it.
Cache Interface
The cache should be an object, similar to a JS Map, including events to let instances of SWR subscribe to it. The exposed interface could be something like this:
Any custom cache should implement that interface, internally they could use any caching mechanism, e.g. a localStorage based cache which will store everything there forever.
The text was updated successfully, but these errors were encountered: