7
A
回答
10
您可以使用CacheControl,ETAG - 按照下面的示例代碼
// In your jersey method
final EntityTag eTag = new EntityTag(resource.getId() + "_" +
resource.getLastModified().getTime());
final CacheControl cacheControl = new CacheControl();
cacheControl.setMaxAge(-1);
ResponseBuilder builder = request.evaluatePreconditions(
resource.getLastModified(), eTag);
// the resoruce's information was modified, return it
if (builder == null) {
builder = Response.ok(resource);
}
// the resource's information was not modified, return a 304
return builder.cacheControl(cacheControl).lastModified(
resource.getLastModified()).tag(eTag).build();
與資源實例替換resource
。
3
你可以與新澤西一起使用適用於標準的java任何緩存機制,像Ehcache。
您只需要注意驗證後端數據沒有變化。
下面是簡單的例子與Ehcache
:
@GET
@Path("{id}")
public List<Data> getData(@PathParam("id") Long id) {
Element element = CacheManager.getInstance().getCache("test").get(id);
if(element == null) {
Data value = fetchElementFromBackend(id);
CacheManager.getInstance().getCache("test").put(new Element(id, value));
return value;
}
return element.getObjectValue();
}
5
解決方案的概要:
Request作爲方法參數
接口:
@Path("myentity") public interface MyEntityResource @GET @Produces(MediaType.APPLICATION_JSON) public Response getMyEntity(@Context final Request request); }
實現:
public class MyEntityResourceImpl implements MyEntityResource @Override public Response getMyEntity(final Request request) { final MyEntity myEntity = ... // load entity final String eTagValue = ... // calclutate value of ETag final EntityTag eTag = new EntityTag(eTagValue); ResponseBuilder responseBuilder = request.evaluatePreconditions(eTag); if (responseBuilder == null) { return Response.ok(user).tag(eTag).build(); } return responseBuilder.build(); } }
缺點:
實現細節
Request
暴露返回類型
Reponse
是通用缺少返回類型的語法在WADL
client proxy不必要參數
Request
Request作爲實例變量
接口:
@Path("myentity") public interface MyEntityResource @GET @Produces(MediaType.APPLICATION_JSON) public Response getMyEntity(); }
實現:
public class MyEntityResourceImpl implements MyEntityResource @Context private Request request @Override public Response getMyEntity() { final MyEntity myEntity = ... // load entity final String eTagValue = ... // calclutate value of ETag final EntityTag eTag = new EntityTag(eTagValue); ResponseBuilder responseBuilder = request.evaluatePreconditions(eTag); if (responseBuilder == null) { return Response.ok(user).tag(eTag).build(); } return responseBuilder.build(); } }
缺點:
返回類型
Reponse
是通用缺少返回類型的語法WADL
依賴注入
@Context
是複雜的,看到https://stackoverflow.com/questions/33240443
ShallowEtagHeaderFilter如Web過濾
網絡。XML:
<filter> <filter-name>etagFilter</filter-name> <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class> </filter> <filter-mapping> <filter-name>etagFilter</filter-name> <url-pattern>/api/*</url-pattern> </filter-mapping>
接口:
@Path("myentity") public interface MyEntityResource @GET @Produces(MediaType.APPLICATION_JSON) public MyEntity getMyEntity(); }
實現:
public class MyEntityResourceImpl implements MyEntityResource @Override public MyEntity getMyEntity() { final MyEntity myEntity = ... // load entity return myEntity; } }
缺點:
定製WriterInterceptor爲JAX-RS Interceptor
攔截:
public class CustomInterceptor implements WriterInterceptor { @Context private Request request; @Override public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { OutputStream old = context.getOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { context.setOutputStream(buffer); context.proceed(); byte[] entity = buffer.toByteArray(); String etag = ... // calclutate value of ETag context.getHeaders().putSingle(HttpHeaders.ETAG, etag); ResponseBuilder responseBuilder = request.evaluatePreconditions(eTag); if (responseBuilder == null) { throw new WebApplicationException(responseBuilder.status(Response.Status.NOT_MODIFIED).header(HttpHeaders.ETAG, etag).build()); } old.write(entity); } finally { context.setOutputStream(old); } } }
另請參見:ServerCacheInterceptor(RestEasy的)
接口:
@Path("myentity") public interface MyEntityResource @GET @Produces(MediaType.APPLICATION_JSON) public MyEntity getMyEntity(); }
實現:
public class MyEntityResourceImpl implements MyEntityResource @Override public MyEntity getMyEntity() { final MyEntity myEntity = ... // load entity return myEntity; } }
缺點:
沒有預定義的攔截器澤西可用
壞服務器性能
不支持的weak ETag
醜陋的解決方法與
WebApplicationException
相關問題
- 1. 如何緩存JSON響應?
- 2. 如何緩存expressjs響應
- 3. 緩存響應
- 4. 如何緩存squpup.okhttp3中的響應
- 5. 如何刷新socket.gethostbyname響應的緩存?
- 6. 緩存JSON響應
- 7. GAE&響應緩存
- 8. 緩存HTTPS響應
- 9. 緩存XML響應
- 10. Retrofit響應緩存
- 11. WebSphere響應緩存
- 12. 緩存SOAP響應
- 13. 緩存API響應
- 14. 如何防止python urllib3緩存響應
- 15. 如何防止Alfresco webscript響應緩存
- 16. 如何垃圾緩存HTTP 302響應
- 17. 如何緩存php soapclient響應?
- 18. 如何緩存異步模塊響應
- 19. 如何停止org.springframework.web.client.RestTemplate緩存響應?
- 20. 如何緩存REST響應,Spring For Android
- 21. Angular $ http緩存:如何識別緩存響應vs實際$ http.get
- 22. webapp2如何刪除緩存控制:從響應頭無緩存?
- 23. renderBinary響應的緩存
- 24. 防止緩存ajax響應
- 25. 緩存asp.net頁面響應
- 26. JSF 2 AJAX響應緩存
- 27. 防止200(緩存)響應
- 28. 緩存ASHX圖像響應
- 29. 緩存響應消息
- 30. 可緩存響應認證
ü介意解釋爲什麼ü禁用緩存控制(無緩存)? 它就像說:沒有緩存可能,但看看這裏我有一個無用的Etag和Last-Modified頭。 – Orri 2013-11-12 17:50:05
這是說,它的永久緩存,但服務器可以說資源是否被修改基於電子標籤,因此客戶端/瀏覽器可以使用它從緩存 – TheWhiteRabbit 2013-11-13 09:18:12