简体   繁体   English

我可以阻止HTTPResponseCache在Cache-Control标头方面表现得像共享缓存吗?

[英]Can I stop HTTPResponseCache behaving like a shared cache in regard to Cache-Control headers?

I am trying to use the built in HTTPResponseCache in my app (making requests via the HTTPURLConnection API) but am having problems trying to get it to cache any responses that were requested with an Authorization header included. 我正在尝试在我的应用程序中使用内置的HTTPResponseCache (通过HTTPURLConnection API发出请求)但是在尝试使用包含的Authorization标头缓存任何响应时遇到问题。

The only way I can get it to cache the response at all is to explicitly put 'public' in the Cache-Control response header on the server ( s-maxage might work too, haven't tried, but explicitly putting private results in no caching); 我可以让它来缓存所有响应的唯一方法就是明确提出“公共”的Cache-Control响应报头(在服务器上的s-maxage可能工作太,没试过,但明确地把private的结果中没有高速缓存); but this will mean that any intermediate proxies will cache the response to serve to other clients, which is not what I want. 但这意味着任何中间代理都会缓存响应以服务于其他客户端,这不是我想要的。

My understanding is that a user agent cache would cache responses requested with Authorization headers by default or with a private header. 我的理解是,用户代理缓存会默认使用Authorization标头或使用private标头缓存请求的响应。 It seems like the HTTPResponseCache is acting like a shared cache in how it is interpreting the headers, rather than a user agent cache. 似乎HTTPResponseCache在解释标头方面就像共享缓存一样,而不是用户代理缓存。 Or is my understanding of the caching standards not correct? 或者我对缓存标准的理解不正确?

Is there any way I can get the cache to act like a user agent HTTP cache? 有什么办法可以让缓存像用户代理HTTP缓存一样工作吗?

This in my install code: 这在我的安装代码中:

public static void setupCache(Context context, long httpCacheSize){
  File httpCacheDir = new File(context.getCacheDir(),"http");
  HttpResponseCache.install(httpCacheDir, httpCacheSize);
}

Do I need to do something different here? 我需要在这里做些不同的事吗? Or perhaps I need to include some user agent information in my requests? 或许我需要在我的请求中包含一些用户代理信息?

Whilst I found no solution to this specific issue, I worked around my problem by refactoring my HTTP client code to use Volley ( http://developer.android.com/training/volley/index.html ) rather than HTTPURLConnection. 虽然我找不到这个特定问题的解决方案,但我通过重构我的HTTP客户端代码来使用Volley( http://developer.android.com/training/volley/index.html )而不是HTTPURLConnection来解决我的问题。 The caching facilities in Volley are implemented separately to HTTPResponseCache and implement handling of cache control headers as expected for a user agent cache. Volley中的缓存工具分别实现到HTTPResponseCache,并实现对用户代理缓存的预期缓存控制头的处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM