InvalidateCache 政策

本页面适用于 ApigeeApigee Hybrid

查看 Apigee Edge 文档。

配置如何从缓存中清除已缓存的值。

此政策适用于通用短期缓存。它与 PopulateCache 政策(用于写入条目)和 LookupCache 政策(用于读取缓存条目)结合使用。

如需了解如何缓存后端资源的响应,请参阅 ResponseCache 政策

此政策是一项可扩展政策,使用此政策可能会影响费用或使用情况,具体取决于您的 Apigee 许可。如需了解政策类型和使用情况影响,请参阅政策类型

元素参考

下面列出了您可以在此政策中配置的元素。

<InvalidateCache async="false" continueOnError="false" enabled="true" name="policy-name">
    <DisplayName>Policy Name</DisplayName>
    <CacheKey>
        <Prefix>prefix_string</Prefix>
        <KeyFragment ref="variable_reference"/>
        <KeyFragment>fragment_string</KeyFragment>
    </CacheKey>
    <!-- Omit this element if you're using the included shared cache. -->
    <CacheResource>cache_to_use</CacheResource>
    <Scope>scope_enumeration</Scope>
    <CacheContext>
        <APIProxyName>application_that_added_the_entry</APIProxyName>
        <ProxyName>proxy_for_which_data_was_cached</ProxyName>
        <TargetName>endpoint_for_which_data_was_cached</TargetName>
    </CacheContext>
    <PurgeChildEntries>true_to_purge_all_child_entries</PurgeChildEntries>
</InvalidateCache>

<InvalidateCache> 属性

The following table describes attributes that are common to all policy parent elements:

Attribute Description Default Presence
name

The internal name of the policy. The value of the name attribute can contain letters, numbers, spaces, hyphens, underscores, and periods. This value cannot exceed 255 characters.

Optionally, use the <DisplayName> element to label the policy in the management UI proxy editor with a different, natural-language name.

N/A Required
continueOnError

Set to false to return an error when a policy fails. This is expected behavior for most policies.

Set to true to have flow execution continue even after a policy fails. See also:

false Optional
enabled

Set to true to enforce the policy.

Set to false to turn off the policy. The policy will not be enforced even if it remains attached to a flow.

true Optional
async

This attribute is deprecated.

false Deprecated

<DisplayName> element

Use in addition to the name attribute to label the policy in the management UI proxy editor with a different, natural-language name.

<DisplayName>Policy Display Name</DisplayName>
Default

N/A

If you omit this element, the value of the policy's name attribute is used.

Presence Optional
Type String

<CacheContext>/<APIProxyName> 元素

指定添加了缓存条目的应用的名称。

<APIProxyName>application_that_added_the_entry</APIProxyName>

属性

属性 说明 默认值 状态 类型
ref 包含应用名称的变量。 不适用 可选 字符串

<CacheContext> 元素

指定当未指定 Prefix 元素值时如何构造缓存键,或指定如何清除由其他 API 代理添加的缓存条目。

<CacheContext>
  <APIProxyName ref="variable_name">application_that_added_the_entry</APIProxyName>
  <TargetName ref="variable_name">endpoint_for_which_data_was_cached</TargetName>
  <ProxyName ref="variable_name">proxy_for_which_data_was_cached</ProxyName>
</CacheContext>

用于构造 CacheKey。当 CacheKey 前缀(即自定义前缀)不用于清除其他 API 代理添加的缓存条目时,必须提供 APIProxyName、ProxyName 和 TargetName 的值。

<CacheKey> 元素

配置存储在缓存中的数据块的唯一指针。

<CacheKey>
    <Prefix>string</Prefix>
    <KeyFragment ref="variable_name" />
    <KeyFragment>literal_string</KeyFragment>
</CacheKey>

默认:

不适用

状态:

需要

类型:

不适用

<CacheKey> 构造缓存中存储的每个数据块的名称。

在运行时,<KeyFragment> 值前面会附加 <Scope> 元素值或 <Prefix> 值。例如,以下命令会产生 UserToken__apiAccessToken__<value_of_client_id> 的缓存键:

<CacheKey>
    <Prefix>UserToken</Prefix>
    <KeyFragment>apiAccessToken</KeyFragment>
    <KeyFragment ref="request.queryparam.client_id" />
</CacheKey>

您可将 <CacheKey> 元素与 <Prefix><Scope> 搭配使用。如需了解详情,请参阅使用缓存键

<CacheResource> 元素

指定存储消息的缓存。

如果此政策(以及相应的 RePopupCache 和 LookupCache 政策)使用了所包含的共享缓存,请完全忽略此元素。

<CacheResource>cache_to_use</CacheResource>

默认:

不适用

状态:

可选

类型:

字符串

如需详细了解如何配置缓存,请参阅通用缓存

<CacheKey>/<KeyFragment> 元素

指定应在缓存键中包含的值。使用 ref 特性或固定值指定要取消引用的变量。

<KeyFragment ref="variable_name"/>
<KeyFragment>literal_string</KeyFragment>

默认:

不适用

状态:

可选

类型:

不适用

在运行时,Apigee 从 <Scope> 元素或 <Prefix> 元素获取值,并将其作为使用各 <KeyFragment> 元素解析值构成的串联的前缀,从而创建缓存键。如需了解详情,请参阅使用缓存键

属性

属性 说明 默认值 状态 类型
ref 要从中获取值的变量。如果此元素包含字面量值,则不应使用此属性。 不适用 可选 字符串

<CacheKey>/<Prefix> 元素

指定要用作缓存键前缀的值。

<Prefix>prefix_string</Prefix>

默认:

不适用

状态:

可选

类型:

字符串

<Prefix> 元素将替换任何 <Scope> 元素。

在运行时,Apigee 从 <Scope> 元素或 <Prefix> 元素获取值,并将其作为使用各 <KeyFragment> 元素解析值构成的串联的前缀,从而创建缓存键。如需了解详情,请参阅使用缓存键

<CacheContext>/<ProxyName> 元素

指定为其缓存数据的代理的名称。

<ProxyName>proxy_for_which_data_was_cached</ProxyName>

默认:

不适用

状态:

可选

类型:

字符串

属性

属性 说明 默认值 状态 类型
ref 要从中获取值的变量。如果此元素包含字面量值,则不应使用此属性。 不适用 可选 字符串

<PurgeChildEntries> 元素

true 用于清除缓存条目,这些缓存条目共享为此政策配置的 <Prefix> 值,即使将这些项加载到缓存中的 PopulateCache 政策实例也使用了各种 <KeyFragment> 元素时也是如此。

通过使具有同一前缀值的所有缓存条目失效,您就可以同时完全清除多个相关条目。

<PurgeChildEntries>true_to_purge_child_entries</PurgeChildEntries>

默认:

false

状态:

可选

类型:

布尔值

<Scope> 元素

<CacheKey> 元素中未提供 <Prefix> 元素时,用于构造缓存键前缀的枚举。

<Scope>scope_enumeration</Scope>

默认:

“专有”

状态:

可选

类型:

字符串

<Scope> 设置确定根据 <Scope> 值前置的缓存键。例如,将范围设置为 Exclusive 时,缓存键将采用以下格式:

orgName__envName__applicationName__proxy|TargetName__ [ serializedCacheKey ]。

如果 <CacheKey> 中存在 <Prefix> 元素,则它会取代 <Scope> 元素值。<Scope> 元素的有效值如下所示。

如需了解详情,请参阅使用缓存键

可接受的值

范围值 说明
Global

缓存密钥可在环境中部署的所有 API 代理之间共享。缓存键的前置格式为 orgName __ envName __。

如果您使用 <KeyFragment> apiAccessToken 和 <Global> 范围定义 <CacheKey> 条目,每个条目都将存储为 orgName__envName__apiAccessToken,后跟访问令牌的序列化值。对于部署在名为“apifactory”的组织中名为“test”的环境中的 API 代理,访问令牌将存储在以下缓存键下:apifactory__test__apiAccessToken

Application

API 代理名称用作前缀。

缓存键的前置格式为 orgName__envName__applicationName

Proxy

ProxyEndpoint 配置用作前缀。

缓存键的前置格式为 orgName__envName__applicationName__proxyEndpointName

Target

TargetEndpoint 配置用作前缀。

缓存键的前置格式为 orgName__envName__applicationName__targetEndpointName

Exclusive

默认值。这是最具体的,因此在给定缓存中,可将命名空间冲突降至最低。

前缀采用以下两种格式之一:

  • 如果将政策附加到 ProxyEndpoint 流,则前缀格式为 ApiProxyName_ProxyEndpointName
  • 如果将政策附加在 TargetEndpoint,则前缀格式为 ApiProxyName_TargetName

缓存键的前置格式为 orgName__envName__applicationName__proxyNameITargetName

例如,完整的字符串可能如下所示:

apifactory__test__weatherapi__default__apiAccessToken

<CacheContext>/<TargetName> 元素

指定为其缓存数据的目标端点的名称。

<TargetName>endpoint_for_which_data_was_cached</TargetName>

默认:

不适用

状态:

可选

类型:

字符串

属性

属性 说明 默认值 状态 类型
ref 要从中获取值的变量。如果此元素包含字面量值,则不应使用此属性。 不适用 可选 字符串

使用说明

采用 PopulateCache 政策LookupCache 政策和 InvalidateCache 政策的通用缓存使用您配置的缓存或默认添加的共享缓存。在大多数情况下,底层共享缓存应满足您的需求。如需使用此缓存,只需省略 <CacheResource> 元素即可。

如需详细了解如何配置缓存,请参阅通用缓存。如需详细了解底层数据存储区,请参阅缓存内部

错误代码

本部分介绍当此政策触发错误时所设置的错误消息和流变量。在为代理开发故障规则时,请务必了解此信息。如需了解详情,请参阅您需要了解的有关政策错误的信息处理故障

错误代码前缀

不适用

运行时错误

此政策不会抛出任何运行时错误。

部署错误

在您部署包含此政策的代理时,可能会发生这些错误。

错误名称 原因 修复
InvalidCacheResourceReference 如果将 InvalidateCache 政策中的 <CacheResource> 元素设置为部署 API 代理的环境中不存在的名称,则会出现此错误。
CacheNotFound 如果尚未在特定的消息处理器组件上创建错误消息中提及的特定缓存,就会发生此错误。

故障变量

不适用

错误响应示例

不适用