缓存策略交互 — 最长使用期限和最长过期时间Cache Policy Interaction—Maximum Age and Maximum Staleness

为了帮助确保将最新鲜的内容返回给客户端应用程序,客户端缓存策略和服务器重新验证要求的交互始终会造成最保守的缓存策略。To help ensure that the freshest content is returned to the client application, the interaction of client cache policy and server revalidation requirements always results in the most conservative cache policy. 本主题中的所有示例阐明针对在 1 月 1 日缓存、1 月 4 日过期的资源的缓存策略。All the examples in this topic illustrate the cache policy for a resource that is cached on January 1 and expires on January 4.

在以下示例中,结合使用了最长过期时间值 (maxStale) 与最长使用时间 (maxAge):In the following examples, the maximum staleness value (maxStale) is used in conjunction with a maximum age (maxAge):

  • 如果缓存策略设置 maxAge = 5 天,且未指定 maxStale 值,根据 maxAge 值,此内容在 1 月 6 日前可用。If the cache policy sets maxAge = 5 days and does not specify a maxStale value, according to the maxAge value, the content is usable until January 6. 但是,根据服务器的重新验证要求,内容会在 1 月 4 日过期。However, according to the server's revalidation requirements, the content expires on January 4. 因为内容过期日期更保守(更早),所以它优先于 maxAge 策略。Because the content expiration date is more conservative (sooner), it takes precedence over the maxAge policy. 因此,即使尚未达到最长使用时间,此内容在 1 月 4 日便会过期,并且必须进行重新验证。Therefore, the content expires on January 4 and must be revalidated even though its maximum age has not been reached.

  • 如果缓存策略设置 maxAge = 5 天,maxStale = 3 天,根据 maxAge 值,此内容在 1 月 6 日前可用。If the cache policy sets maxAge = 5 days and maxStale = 3 days, according to the maxAge value, the content is usable until January 6. 根据 maxStale 值,此内容在 1 月 7 日前可用。According to the maxStale value, the content is usable until January 7. 因此,会在 1 月 6 日重新验证此内容。Therefore, the content gets revalidated on January 6.

  • 如果缓存策略设置 maxAge = 5 天,maxStale = 1 天,根据 maxAge 值,此内容在 1 月 6 日前可用。If the cache policy sets maxAge = 5 days and maxStale = 1 day, according to the maxAge value, the content is usable until January 6. 根据 maxStale 值,此内容在 1 月 5 日前可用。According to the maxStale value, the content is usable until January 5. 因此,会在 1 月 5 日重新验证此内容。Therefore, the content gets revalidated on January 5.

当内容的最长使用时间小于过期日期时,更保守的缓存行为占据优先级,且最长过期时间值不会有任何效果。When the maximum age is less than the content expiration date, the more conservative caching behavior always prevails and the maximum staleness value has no effect. 以下示例阐明了在内容过期之前到达最长使用时间 (maxAge) 时设置最长过期时间 (maxStale) 值的效果:The following examples illustrate the effect of setting a maximum staleness (maxStale) value when the maximum age (maxAge) is reached before the content expires:

  • 如果缓存策略设置 maxAge = 1 天,且未指定 maxStale 值,即使内容尚未过期,也会在 1 月 2 日重新验证此内容。If the cache policy sets maxAge = 1 day and does not specify a value for maxStale value, the content is revalidated on January 2 even though it has not expired.

  • 如果设缓存策略设置 maxAge = 1 天,maxStale = 3 天,会在 1 月 2 日重新验证此内容以强制实施更保守的策略设置。If the cache policy sets maxAge = 1 day and maxStale = 3 days, the content is revalidated on January 2 to enforce the more conservative policy setting.

  • 如果缓存策略设置 maxAge = 1 天,maxStale = 1 天,会在 1 月 2 日重新验证此内容。If the cache policy sets maxAge = 1 day and maxStale = 1 day, the content is revalidated on January 2.

请参阅See Also

网络应用程序的缓存管理Cache Management for Network Applications
缓存策略Cache Policy
基于位置的缓存策略Location-Based Cache Policies
基于时间的缓存策略Time-Based Cache Policies
在网络应用程序中配置缓存Configuring Caching in Network Applications
缓存策略交互 — 最长使用时间和最低新鲜度Cache Policy Interaction—Maximum Age and Minimum Freshness