9 ms·
Is the cache global?
by ocfx 9y ago
Is the cache global?
- danabramov 9y agoIt's up to you. I intentionally kept moving parts to the minimum in my demo, but here's the real thing I was using behind the scenes: https://github.com/facebook/react/tree/master/packages/simple-cache-provider https://github.com/facebook/react/tree/master/packages/simpl.... So in that sense it's not global, you can create as many as you like and then isolate them (e.g. per subtree). The `simple-cache-provider` is just a reference implementation though. It's somewhat naïve, e.g. it doesn't have invalidation strategy and just lets you replace the whole cache with a new one if you want to invalidate. We'll publish more details on lower level API and how to write your own cache in the future.
- btown 9y agoIt seems from https://github.com/facebook/react/blob/master/packages/simple-cache-provider/src/SimpleCacheProvider.js#L256 https://github.com/facebook/react/blob/master/packages/simpl... that in the live version you'll be specifying which cache to use; this could be global, provided via React's old or new Context APIs, or passed down through props. And you could always write your own fetcher factory if you want to do something really custom; there's nothing special about the SimpleCacheProvider, and it doesn't use any React internals.
- danabramov 9y agoFWIW my demo code looked like export let cache; function initCache() { cache = createCache(initCache); } initCache(); export function createFetcher(fetch) { const res = createResource(fetch); return { read(...args) { return res(cache, ...args); }, }; }
- deleted 9y ago[deleted]