Endpoint 过期策略
默认情况下,Reactive Data Client 使用 stale-while-revalidate。当数据可用时,组件会立即渲染它。如果数据足够旧,Data Client 会在后台重新获取它。
过期状态
dataExpiryLength响应在变为过时之前保持新鲜的时长(毫秒)。 了解更多新鲜(Fresh)
数据足够新。Data Client 不会获取它。
dataExpiryLength超过 dataExpiryLength(毫秒)后,缓存的响应变为过时:仍会显示,但会重新获取。 了解更多过时(Stale)
组件可以展示这些数据。当有组件需要它时,Data Client 会再次获取它。
ctrl.invalidate()立即将数据标记为无效(即使仍然新鲜),需要它的组件会重新获取。 了解更多无效(Invalid)
组件不能展示这些数据。需要它的组件会获取它(在获取完成之前,已挂载的组件会保留其数据)。如果没有组件需要这些数据,则什么也不会发生。
useSuspense() 会在挂载时以及参数变化时检查数据。如果数据已过时,它就会发起获取。
过期时间
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 会在组件展示之前数据的同时发起获取。即使数据是“新鲜”的,它也会获取。
<script setup lang="ts"> import { useController, useSuspense } from '@data-client/vue'; import { lastUpdated } from './api/lastUpdated'; const time = await useSuspense(lastUpdated, { id: '1' }); const ctrl = useController(); </script> <template> <div class="listItem"> <span class="meta">API time</span> <time>{{ time.updatedAt.toLocaleString('en-US', { timeStyle: 'long' }) }}</time> </div> <button @click="ctrl.fetch(lastUpdated, { id: '1' })">Refresh</button> </template>
刷新可见的 endpoint
Controller.expireAll() 会把所有与 testKey 匹配的响应的过期状态设置为过时。
<script setup lang="ts"> import { useController } from '@data-client/vue'; import { lastUpdated } from './api/lastUpdated'; import TimeList from './TimeList.vue'; const ctrl = useController(); </script> <template> <div> <TimeList /> <div class="buttonGroup"> <button @click="ctrl.expireAll(lastUpdated)">Expire All</button> <button @click="ctrl.fetch(lastUpdated, { id: '1' })"> Force Refresh First </button> </div> </div> </template>
使失效
失效的数据总是会重新获取,即使它仍然新鲜。组件的 setup 一旦运行,Vue 就无法再次挂起该组件,因此已挂载的组件会继续展示之前的数据,直到重新获取完成。在此期间,useCache() 返回 undefined,useDLE() 的 loading
为 true。在失效之后挂载的组件会挂起,直到新数据到达。
某个特定的 endpoint
在这个示例中,使 endpoint 失效会重新获取它,即使其数据仍然新鲜。
<script setup lang="ts"> import { useController } from '@data-client/vue'; import { lastUpdated } from './api/lastUpdated'; import TimeList from './TimeList.vue'; const ctrl = useController(); </script> <template> <div> <TimeList /> <div class="buttonGroup"> <button @click="ctrl.invalidateAll(lastUpdated)"> Invalidate All </button> <button @click="ctrl.invalidate(lastUpdated, { id: '1' })"> Invalidate First </button> </div> </div> </template>
包含某个 Entity 的任意 endpoint
Invalidate schema 会让响应中包含该 Entity 的_每个_ endpoint 失效。如果 endpoint 在 Array 中包含该 Entity,Data Client 会把它从该 Array 中移除。
<script setup lang="ts"> import { useController, useLoading } from '@data-client/vue'; import { InvalidateTimedEntity, deleteLastUpdated, } from './api/lastUpdated'; import ServerTime from './ServerTime.vue'; const ctrl = useController(); const [handleDelete, loadingDelete] = useLoading(() => ctrl.fetch(deleteLastUpdated, { id: '1' }), ); </script> <template> <div> <ServerTime id="1" /> <div class="buttonGroup"> <button @click="handleDelete" :aria-busy="loadingDelete"> Invalidate </button> <button @click="ctrl.setResponse(deleteLastUpdated, { id: '1' }, { id: '1' })" > Invalidate (without fetching DELETE) </button> <button @click="ctrl.set([InvalidateTimedEntity], [{ id: '1' }])"> Invalidate Entity with ctrl.set </button> </div> </div> </template>
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 };
}
}