更正 CacheControl 配置以指示不应缓存资源表示?
Correct CacheControl configuration to indicate that a resource representation should NEVER be cached?
private static final CacheControl NEVER;
static
{
NEVER = new CacheControl();
NEVER.setNoCache(true);
NEVER.setMaxAge(-1);
NEVER.setMustRevalidate(true);
NEVER.setNoStore(true);
NEVER.setProxyRevalidate(true);
NEVER.setSMaxAge(-1);
}
我想配置一个 CacheControl
指令,该指令将向所有客户端和代理指示资源表示应该 永远不会 出于任何原因被缓存。这是我从 my research 和阅读 JavaDocs 中发现的。
上述配置中是否还缺少其他设置?
您的配置看起来不错,但是我经常使用 max-age
和 s-maxage
set 以及 Cache-Control
中的 0
(-1
应该也可以)。
您可能还想添加一个 Expires
header set to 0
, in case the recipient doesn't support Cache-Control
. From the RFC 7234:
If a response includes a Cache-Control
field with the max-age
directive, a recipient MUST ignore the Expires
field. Likewise, if a response includes the s-maxage
directive, a shared cache recipient MUST ignore the Expires
field. In both these cases, the value in Expires
is only intended for recipients that have not yet implemented the Cache-Control
field.
在 JAX-RS 中,您可以使用过滤器将这样的 headers 添加到响应中,并使用名称绑定注释将过滤器绑定到特定的资源方法或资源 class .
首先定义名称绑定注解:
@NameBinding
@Retention(RetentionPolicy.RUNTIME)
public @interface NoCache {}
然后创建一个过滤器以将 headers 添加到响应中并使用上面定义的 @NoCache
注释对其进行注释:
@NoCache
@Provider
public class NoCacheFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext request,
ContainerResponseContext response) {
CacheControl cacheControl = new CacheControl();
cacheControl.setNoStore(true);
cacheControl.setNoCache(true);
cacheControl.setMustRevalidate(true);
cacheControl.setProxyRevalidate(true);
cacheControl.setMaxAge(0);
cacheControl.setSMaxAge(0);
response.getHeaders().add(HttpHeaders.CACHE_CONTROL, cacheControl.toString());
response.getHeaders().add(HttpHeaders.EXPIRES, 0);
}
}
然后使用 @NoCache
:
将上面定义的过滤器绑定到您的端点
@Path("/foo")
public class MyResource() {
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public String wontCache() {
...
}
}
如果你想要一个全局过滤器,你不需要定义 @NoCache
注释。
private static final CacheControl NEVER;
static
{
NEVER = new CacheControl();
NEVER.setNoCache(true);
NEVER.setMaxAge(-1);
NEVER.setMustRevalidate(true);
NEVER.setNoStore(true);
NEVER.setProxyRevalidate(true);
NEVER.setSMaxAge(-1);
}
我想配置一个 CacheControl
指令,该指令将向所有客户端和代理指示资源表示应该 永远不会 出于任何原因被缓存。这是我从 my research 和阅读 JavaDocs 中发现的。
上述配置中是否还缺少其他设置?
您的配置看起来不错,但是我经常使用 max-age
和 s-maxage
set 以及 Cache-Control
中的 0
(-1
应该也可以)。
您可能还想添加一个 Expires
header set to 0
, in case the recipient doesn't support Cache-Control
. From the RFC 7234:
If a response includes a
Cache-Control
field with themax-age
directive, a recipient MUST ignore theExpires
field. Likewise, if a response includes thes-maxage
directive, a shared cache recipient MUST ignore theExpires
field. In both these cases, the value inExpires
is only intended for recipients that have not yet implemented theCache-Control
field.
在 JAX-RS 中,您可以使用过滤器将这样的 headers 添加到响应中,并使用名称绑定注释将过滤器绑定到特定的资源方法或资源 class .
首先定义名称绑定注解:
@NameBinding
@Retention(RetentionPolicy.RUNTIME)
public @interface NoCache {}
然后创建一个过滤器以将 headers 添加到响应中并使用上面定义的 @NoCache
注释对其进行注释:
@NoCache
@Provider
public class NoCacheFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext request,
ContainerResponseContext response) {
CacheControl cacheControl = new CacheControl();
cacheControl.setNoStore(true);
cacheControl.setNoCache(true);
cacheControl.setMustRevalidate(true);
cacheControl.setProxyRevalidate(true);
cacheControl.setMaxAge(0);
cacheControl.setSMaxAge(0);
response.getHeaders().add(HttpHeaders.CACHE_CONTROL, cacheControl.toString());
response.getHeaders().add(HttpHeaders.EXPIRES, 0);
}
}
然后使用 @NoCache
:
@Path("/foo")
public class MyResource() {
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public String wontCache() {
...
}
}
如果你想要一个全局过滤器,你不需要定义 @NoCache
注释。