如何自动清除齐射缓存?

编程入门 行业动态 更新时间:2024-10-24 15:16:15
本文介绍了如何自动清除齐射缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

例如,我想每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.

更多推荐

如何自动清除齐射缓存?

本文发布于:2023-11-16 14:24:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605093.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!