何时才从urlcache的内存和磁盘中删除所有内容? [英] When exactly do things get removed from urlcache's memory and disk?

查看:180
本文介绍了何时才从urlcache的内存和磁盘中删除所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let memoryCapacity = 200 * 1024 * 1024
let diskCapacity = 1 * 1024 * 1024
let cache = URLCache(memoryCapacity: memoryCapacity, diskCapacity: diskCapacity, diskPath: "myDataPath")
URLCache.shared = cache

场景1

我将urlcache的内存设置为200mb,并将磁盘空间设置为1mb.然后,我下载一个图像.关闭互联网并强制退出该应用程序,通过点击它再次启动该应用程序,并触发呼叫(下载图像),但由于datanil

I'm setting urlcache's memory to 200mb and setting the disk space to 1mb. Then I download an image. Turn off internet and force quit the app, launch the app again by tapping it, and trigger a call (to download image) but it immediately crashes because data is nil

不用说,如果我关闭互联网,它仍会按预期从缓存中读取,并且不会发生崩溃.

Needless to say if I turn off internet, it will still read from cache as expected and no crash would happen.

场景2

如果将diskCapacity设置为200mb(let diskCapacity = 200 * 1024 * 1024) 即使将缓存从内存中清除掉,映像仍会保留在磁盘中;它会一直显示!

If I set the diskCapacity to 200mb (let diskCapacity = 200 * 1024 * 1024) then even though the cache is flushed out of memory, still the image is persisted in the disk; it will always show!

基于这种观察,我有以下问题:

Based on this observation, I have the following questions:

  • 您每次强制退出或由于内存警告/问题而导致应用崩溃时,会保持刷新状态,尽管它会使磁盘保持完整,这是正确的说法吗?
  • 还有其他原因可以导致缓存从内存中清除吗?
  • 何时可以删除存储在磁盘中的内容?

推荐答案

简短答案:

当应用程序终止时(或者可能在内存压力下),内存缓存也会丢失.磁盘缓存可在应用程序的多次调用中保留,但如果设备用尽持久性存储并且操作系统从缓存和临时文件夹中回收空间,则可以将其删除.

The memory cache is lost when the app terminates (or, likely, under memory pressure, too). The disk cache persists across multiple invocations of the app, though can be removed if the device runs out of persistent storage and the OS reclaims space from caches and temp folders.

长答案:

您问:

您每次强制退出或由于内存警告/问题而使应用程序崩溃时,会保持高速缓存,尽管它会使磁盘完整无缺,但这是正确的说法吗?

Is it correct to say, every time you force quit or your app crashes due to a memory warning/issue your cache will get flushed, though it leaves your disk intact?

很大.简单地说,应用终止后,与应用相关的所有内存都将被丢弃,并且当应用重新启动时,只能检索保存到永久性存储中的那些项目.

Largely. It may be more precise to say simply that all memory related to your app is discarded when the app terminates and that only those items saved to persistent storage can be retrieved when the app restarts.

还有其他可以从内存中清除缓存的地方吗?

Any other place where cache can get flushed out of memory?

当应用终止时,您将丢失内存缓存中的所有内容.显然,在某些其他情况下,可以删除项目:

You lose everything in the memory cache when the app terminates. There are obviously a few other situations in which case items can be removed:

  • 如果您从URLCache中手动删除响应.
  • 当您接近高速缓存的最大容量并尝试添加新项目时,会从缓存中删除较旧的单个项目,从而迫使较旧的项目退出.
  • 网络响应通常包含一个缓存策略(指示可以安全地缓存响应多长时间),因此很可能在那一刻将其删除.
  • 可以合理地根据内存压力(例如.UIApplicationDidReceiveMemoryWarning)清除内存缓存.
  • If you manually remove responses from the URLCache.
  • Older, individual items are removed from the cache as you approach the max capacity of the cache and you try to add new items, forcing older items out.
  • Network responses often include a cache policy (indicating how long the response can be safely cached), so it's likely that they're removed at that point.
  • The memory cache may reasonably be purged upon memory pressure (e.g. .UIApplicationDidReceiveMemoryWarning).

什么时候可以删除存储在磁盘中的内容?

When can things stored in disk get removed?

逻辑与先前的观点基本相同,不同之处在于(a)它可以在应用程序的调用中保留下来); (b)不会因内存压力而刷新,尽管当设备的持久性存储空间不足时可以将其删除.

The logic is largely the same as prior point, except that (a) it can survive across invocations of the app); and (b) it's not flushed upon memory pressure, though it can be removed when the device runs low on persistent storage.

这篇关于何时才从urlcache的内存和磁盘中删除所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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