【问题标题】:Which url needs to be invalidate for clearing volley cache?清除 volley 缓存需要使哪个 url 无效?
【发布时间】:2016-10-14 18:11:24
【问题描述】:

实际上,我对凌空缓存管理有点困惑。假设我有一个带有 BASE URL http://example.com 的 API,但是当我调用我的 api 时,我附加了一个布局参数,所以最后它看起来像 http://example.com?sort_by=distance

因此,如果我想清除此 url 的缓存,我应该使用哪个键 http://example.comhttp://example.com?sort_by=distance

如果答案是第二个,那么我是否需要为由不同 url 参数创建的所有 url 独立清除缓存。

【问题讨论】:

  • 这在我看来就像您正在访问一个不缓存数据的网络服务...
  • 我的网站让我缓存 url 数据。但有时当我收到更新标志时,我需要清除缓存并再次调用 api。
  • Request.java /** * Returns the cache key for this request. By default, this is the URL. */ public String getCacheKey() { return getUrl(); } 然后public Request(int method, String url, Response.ErrorListener listener) { mMethod = method; mUrl = url; mErrorListener = listener; setRetryPolicy(new DefaultRetryPolicy()); }

标签: android caching android-volley


【解决方案1】:

volley(我猜是所有其他缓存系统)使用您调用的确切详细 url 作为缓存数据的键。 如果你想删除一个缓存条目,你需要通过确切的完整链接来完成。 链接“www.example.com/amir?id=1”与“www.example.com/amir?id=2”不同。否则缓存会比效率更麻烦。

【讨论】:

    【解决方案2】:

    顺便说一句,您不需要删除缓存。考虑到某些因素,这是凌空处理的事情。

    如果您需要调用跳过客户端和服务器中所有缓存系统的链接,最简单的方法是在 API 调用的末尾放置一个时间戳。 如果您的链接是 www.example.com/amir/id/2,则将您的链接设为这样 => www.example.com/amir/id/2?timestamp=54461

    通过Java的System.currentTimeMillis()生成timestamp前面的数字。您可以将它除以一个数字,以使缓存值在一小部分时间内有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-04
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 2013-01-08
      相关资源
      最近更新 更多