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

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

问题描述

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

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

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

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

通过扩展截击缓存类来覆盖方法吗?

Override methods by extending the volley cache class?

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

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

推荐答案

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

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 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);
}

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

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

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

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