删除外部缓存目录定期 [英] Deleting External Cache Directory Periodically

查看:194
本文介绍了删除外部缓存目录定期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的应用我存储在外部存储缓存地图图像,减少到我使用的API调用。由于这个数据是地图数据,它受随着时间而改变。其结果是这些图像应每那么经常更新。

For my app I store map images in a cache on the external storage as to reduce calls to the API that I am using. Since this data is map data, it is subject to change over time. As a result these images should be updated every-so-often.

如何以编程定期删除缓存目录?比方说,每星期。

How can I programmatically delete the cache directory periodically? Say for example, every week.

一些额外的想法:
也许是删除整个目录不是去它的最好方式。也许我可以检查每个图像的新鲜度,并删除旧的?

Some extra thoughts: Perhaps deleting the entire directory is not the best way to go about it. Perhaps I can check the "freshness" of each image, and delete the old ones?

推荐答案

是的,它通常是一个好主意,用一些方法或其他限​​制你的缓存。一些缓存有固定的大小,一旦超出您的高速缓存大小旧内容被删除。这个尺寸可以是项目的数量或在磁盘空间的大小的计数。某些高速缓存具有的TTL每个项目(或所有项相同的TTL)等项目在一定时间之后过期。有些缓存可能永远不会过期。或许它的尺寸由将在第一时间进行缓存可能项目数的限制。任何这些方法是有效的,尽管有些人可能更适合于某些情况下比其他

Yes, it's usually a good idea to limit your cache with some method or another. Some caches have fixed sizes, and older content is removed once your cache size is exceeded. This size could be a count of the number of items or a size in disk space. Some caches have TTLs for each item (or the same TTL for all items) and so items expire after a certain time. Some caches may never expire. Perhaps it is bounded in size by the number of possible items that would be cached in the first place. Any of these approaches are valid, though some may be more appropriate for certain scenarios than others.

的新鲜度的想法可能是一个方法,我会考虑。这是一样的TTL。基本上你想设置的时间,你的缓存项将活的长度。存储与高速缓存数据沿着这个缓存时间,然后检查其读取缓存中的数据时。如果是过去的到期日期,你可以删除缓存文件,并再次检索来自API的地图数据(和高速缓存)。你也许可以做到只读取文件的创建时间过的东西,如果你不希望单独存储日期和在应用程序中的固定期限硬codeD或可配置的。

The "freshness" idea is probably an approach I'd consider. It's the same as the TTL. Basically you want to set a length of time that your cache item will live for. Store this cache time along with the cache data, then check it whenever reading the cache data. If it's past the expiration date, you can delete that cache file and retrieve the map data from the API again (and cache that). You could probably do something with just reading the file creation time too if you don't want to store a date separately and have a fixed lifespan hard-coded or configurable in your application.

更新,以解决意见:

我使用哈希的URL作为文件名前了。要知道,有碰撞的可能(高度依赖于你的哈希算法,当然你的数据集)。另外,如果你经历了很多网址,你的哈希算法的性能可能无所谓了。

I've used hashing the URL as the filename before too. Just be aware that there's a possibility of collisions (highly dependent on your hash algorithm and your data set of course). Also, if you're going through a lot of URLs, the performance of your hash algorithm might matter too.

存储在一个文本文件高速缓存元数据是好的,特别是如果你没有一吨的URL。你要小心你如何更新,虽然该文本文件。你可以很容易损坏,如果你不小心,并从多个线程访问它没有适当的同步。如果你有大量的数据的话,可以考虑另一种选择是使用一个数据库。如果你这样做高速缓存元数据存储在一个文件 - 无论是数据库或文本文件 - 你可以避开所有散列的问题,通过使用您的文件名不同的方案。你可以只增加十六进制或基36的例子。

Storing that cache metadata in a text file is fine, especially if you don't have a ton of URLs. You'll want to be careful about how you update that text file though. You could easily corrupt it if you're not careful and access it from multiple threads without proper synchronization. If you have a lot of data, another option you can consider is using a database. If you do store this cache metadata in a file--whether database or text file--you can avoid all the problems with hashing by using a different scheme for your filenames. You could just increment in hex or base 36 for example.

这篇关于删除外部缓存目录定期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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