如何自动清除排球缓存? [英] How to clear the volley cache automatically?

查看:30
本文介绍了如何自动清除排球缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想每 30 分钟清除一次请求队列.

I want to clear the request queue each 30 minutes for example.

那么自动清除排球缓存的最佳方法是什么?

So What is the best way to clear volley cache automatically?

通过扩展 volley 缓存类来覆盖方法?

Override methods by extending the volley cache class?

或者建立一个计时器,每次我需要时都会清除缓存?

Or build a timer which will clear the cache every times i need?

推荐答案

Google Volley 提供了 2 种方法来清除缓存中的项目:

Google Volley provides 2 ways to clear an item from the Cache:

AppController.getInstance().getRequestQueue().getCache().remove(key);

AppController.getInstance().getRequestQueue().getCache().invalidate(key, fullExpire);

移除意味着您要移除实际缓存的数据.

Remove means you are removing the actual cached data.

Invalidate 意味着您只是将数据标记为无效.所以 volley 会与服务器核对数据是否仍然有效.full expire 决定是否在 volley 与服务器验证数据之前使用数据.

Invalidate means you are just marking the data as invalid. So volley will check with the server whether the data is still valid. The full expire determines whether to use the data before volley has validated it with the server.

要每 30 分钟清除一次缓存,请使用以下代码:-

To clear cache in each 30 minutes use below code:-

您可以使用 volley 的 serverDate 来获取最初收到响应的日期

you can use volley's serverDate to get the date for when the response was originally received as

AppController.getInstance().getRequestQueue().getCache().get(url).serverDate

所以在你的代码中使用 getMinutesDifference 函数作为

So in your code use getMinutesDifference function as

  public static long getMinutesDifference(long timeStart,long timeStop){
            long diff = timeStop - timeStart;
            long diffMinutes = diff / (60 * 1000);

            return  diffMinutes;
        }

并在您的代码中将此函数调用为

and Call this function in your code as

Calendar calendar = Calendar.getInstance();
long serverDate = AppController.getInstance().getRequestQueue().getCache().get(url).serverDate;
if(getMinutesDifference(serverDate, calendar.getTimeInMillis()) >=30){
   AppController.getInstance().getRequestQueue().getCache().invalidate(url, true);
}

如果之前的 url 响应 >=30 分钟,它将使缓存无效.

It will invalidate the cache,if previous url response >=30 minutes.

这篇关于如何自动清除排球缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆