目前我正试图为我的平针织服务提供缓存服务。
所以出现了一些问题。
什么是entityTag的价值?它可以只是一个唯一生成的随机字符串吗?
当我从客户端向服务器发出请求时,我会使用实体标记返回响应。问题:如何缓存此问题以及如何知道我必须为下一个获取请求发送哪个缓存的entityTag?
在服务器端,我收到了被发送的entityTag。我如何将其与资源进行比较?因为我没有将entityTag附加到资源。
它只是比较entityTags。那么我什么时候需要最后修改的标头值?
很抱歉,很高兴为服务器端和客户端提供示例。我找不到这个问题。如何在请求中发送entityTags,如何在服务器端比较它们以及最后修改的内容。
答案 0 :(得分:8)
ETag为客户端缓存提供了一种机制,用于验证缓存的内容是否仍然是最新的。关于你的问题:
在服务器端,Jersey支持评估ETag并生成响应。例如。您的资源方法可能如下所示:
@GET
public Response doGet() {
EntityTag et = yourMethodForCalculatingEntityTagForThisResource();
// the following method call will result in Jersey checking the headers of the
// incoming request, comparing them with the entity tag generated for
// the current version of the resource generates "304 Not Modified" response
// if the same. Otherwise returns null.
ResponseBuilder rb = request.evaluatePreconditions(new EntityTag("1"));
if (rb != null) {
// Jersey generated 304 response - return it
return rb.build();
}
// return the current version of the resource with the corresponding tag
return Response.ok(getCurrentVersion(), "text/plain").tag(et).build();
}
为最后修改的标题以及etag和last-modified提供了相同的支持。
这篇维基百科文章提供了对ETag的一个很好的概述:http://en.wikipedia.org/wiki/HTTP_ETag