Endpoint 过期策略
默认情况下,Reactive Data Client 使用 stale-while-revalidate。当数据可用时,组件会立即渲染它。如果数据足够旧,Data Client 会在后台重新获取它。
过期状态
dataExpiryLength响应在变为过时之前保持新鲜的时长(毫秒)。 了解更多新鲜(Fresh)
数据足够新。Data Client 不会获取它。
dataExpiryLength超过 dataExpiryLength(毫秒)后,缓存的响应变为过时:仍会显示,但会重新获取。 了解更多过时(Stale)
组件可以展示这些数据。当有组件需要它时,Data Client 会再次获取它。
ctrl.invalidate()立即将数据标记为无效(即使仍然新鲜),需要它的组件会重新获取。 了解更多无效(Invalid)
组件不能展示这些数据。需要它的组件会获取它并挂起。如果没有组件需要这些数据,则什么也不会发生。
useSuspense() 会在挂载时以及参数变化时检查数据。如果数据已过时,它就会发起获取。
使用 React Navigation 时,焦点事件也会为过时的数据触发获取。
过期时间
Endpoint.dataExpiryLength
Endpoint.dataExpiryLength 用于设置数据保持“新鲜”多少毫秒后变为“过时”。把它设置为很小的数字,比如 50,可以让数据几乎立即变为过时;设置为很大的数字,则可以让数据长时间保持新鲜。
在 'first' 和 'second' 之间切换会改变参数。在数据新鲜期间,旧的时间会保持不变,不会重新获取。
import { lastUpdated } from './api/lastUpdated'; export const getUpdated = lastUpdated.extend({ dataExpiryLength: 10000 });
@data-client/rest
较长的缓存寿命
import {
RestEndpoint,
RestGenerics,
resource,
} from '@data-client/rest';
// We can now use LongLivingEndpoint to create endpoints that will be cached for one hour
class LongLivingEndpoint<
O extends RestGenerics,
> extends RestEndpoint<O> {
dataExpiryLength = 60 * 60 * 1000; // one hour
}
const LongLivingResource = resource({
path: '/:id',
Endpoint: LongLivingEndpoint,
});
出错时从不重试
import {
RestEndpoint,
RestGenerics,
resource,
} from '@data-client/rest';
// We can now use NoRetryEndpoint to create endpoints that will be cached for one hour
class NoRetryEndpoint<
O extends RestGenerics,
> extends RestEndpoint<O> {
errorExpiryLength = Infinity;
}
const NoRetryResource = resource({
path: '/:id',
Endpoint: NoRetryEndpoint,
});
Endpoint.invalidIfStale
Endpoint.invalidIfStale 去掉了“过时”状态,使得过期的数据立即被视为“无效”。
在演示中,页面的数据一旦过时,页面就会挂起(导航会在进度条上等待)。在数据新鲜期间,页面会展示它。
import { lastUpdated } from './api/lastUpdated'; export const getUpdated = lastUpdated.extend({ invalidIfStale: true, dataExpiryLength: 5000, });
强制刷新
要在展示旧数据的同时获取新数据:
某个特定的 endpoint
Controller.fetch 会在组件展示之前数据的同时发起获取。即使数据是“新鲜”的,它也会获取。
import { useSuspense, useController } from '@data-client/react'; import { lastUpdated } from './api/lastUpdated'; function ShowTime() { const { updatedAt } = useSuspense(lastUpdated, { id: '1' }); const ctrl = useController(); return ( <> <div className="listItem"> <span className="meta">API time</span> <time>{updatedAt.toLocaleString('en-US', { timeStyle: 'long' })}</time> </div> <button onClick={() => ctrl.fetch(lastUpdated, { id: '1' })}> Refresh </button> </> ); } render(<ShowTime />);
刷新可见的 endpoint
Controller.expireAll() 会把所有与 testKey 匹配的响应的过期状态设置为过时。
import { useController } from '@data-client/react'; import { lastUpdated } from './api/lastUpdated'; import TimeList from './TimeList'; function Demo() { const ctrl = useController(); return ( <div> <TimeList /> <div className="buttonGroup"> <button onClick={() => ctrl.expireAll(lastUpdated)}> Expire All </button> <button onClick={() => ctrl.fetch(lastUpdated, { id: '1' })}> Force Refresh First </button> </div> </div> ); } render(<Demo />);
使失效(重新挂起)
某个特定的 endpoint
在这个示例中,使 endpoint 失效会显示加载 fallback,因为此时数据不允许展示。
import { useController } from '@data-client/react'; import { lastUpdated } from './api/lastUpdated'; import TimeList from './TimeList'; function Demo() { const ctrl = useController(); return ( <div> <TimeList /> <div className="buttonGroup"> <button onClick={() => ctrl.invalidateAll(lastUpdated)}> Invalidate All </button> <button onClick={() => ctrl.invalidate(lastUpdated, { id: '1' })}> Invalidate First </button> </div> </div> ); } render(<Demo />);
包含某个 Entity 的任意 endpoint
Invalidate schema 会让响应中包含该 Entity 的_每个_ endpoint 失效。如果 endpoint 在 Array 中包含该 Entity,Data Client 会把它从该 Array 中移除。
import { useController, useLoading } from '@data-client/react'; import { InvalidateTimedEntity, deleteLastUpdated, } from './api/lastUpdated'; import ServerTime from './ServerTime'; function ShowTime() { const ctrl = useController(); const [handleDelete, loadingDelete] = useLoading( () => ctrl.fetch(deleteLastUpdated, { id: '1' }), [], ); return ( <div> <ServerTime id="1" /> <div className="buttonGroup"> <button onClick={handleDelete} aria-busy={loadingDelete}> Invalidate </button> <button onClick={() => ctrl.setResponse(deleteLastUpdated, { id: '1' }, { id: '1' }) } > Invalidate (without fetching DELETE) </button> <button onClick={() => ctrl.set([InvalidateTimedEntity], [{ id: '1' }])} > Invalidate Entity with ctrl.set </button> </div> </div> ); } render(<ShowTime />);
Controller.fetch() 会同时更新服务器和 store。如果只想修改 store,请使用 Controller.setResponse() 或 Controller.set()。
根据数据有条件地失效
如果只想对部分响应进行失效,请从 Entity.process 返回 undefined。
class PriceLevel extends Entity {
price = 0;
amount = 0;
pk() {
return this.price;
}
static process(
input: [number, number],
parent: any,
key: string | undefined,
): any {
const [price, amount] = input;
if (amount === 0) return undefined;
return { price, amount };
}
}