在缓存目录中的文件可以随时删除吗?

huangapple go评论135阅读模式
英文:

Can files in the cache directory be deleted at any time?

问题

关于Context.getCacheDir的文档指出,“系统将在设备需要释放磁盘空间时自动删除此目录中的文件”,并列出了一些首先被删除的文件的几个标准。我的问题是,是否有关于应用程序可以依赖的文件何时会被保留的任何保证?例如,如果应用程序正在运行并且打开了文件,是否会删除文件?

我怀疑答案是没有任何保证,文件可以随时消失,但我找不到任何相关确认。

额外问题:对于由Context.getExternalCacheDir返回的外部缓存目录,答案是否不同?

英文:

The documentation for Context.getCacheDir says that "the system will automatically delete files in this directory as disk space is needed elsewhere on the device", and lists a few criteria for which files will be deleted first. My question is, are there any guarantees about when files will be kept that an application can rely on? For example, is it the case that files won't be deleted if the application is running and has the file open?

I suspect that the answer is that there are no guarantees, and the files can disappear at any time, but I couldn't find any confirmation about that.

Bonus question: is the answer different for the external cache directory, as returned by Context.getExternalCacheDir?

答案1

得分: 1

在缓存目录中存在的文件将被删除。Android操作系统将始终首先删除较旧的文件,如File.lastModified()所报告的。Android操作系统将通过特殊的配额函数来优先确定缓存文件的删除顺序。如果缓存目录中所有文件的总大小超过了StorageManager.getCacheQuotaBytes(java.util.UUID)返回的字节数,当系统需要额外的空间时,您的应用程序将首先被删除其缓存文件。如果这个配额没有超过,那么您的应用程序将排在最后(有最小的机会)以删除其缓存文件。

有一个比缓存目录更好的选项,如果您想要存储文件,您可以使用File.getDir(String name, int mode)。除非用户清除存储,否则此目录不会被删除。如果用户清除存储,那么应用程序将重置自身。因此,当用户再次打开它时,它将从头开始运行。

如果您想要存储首选项,那么SharedPreferences是最好的选择。您可以存储大量的键-值对。查看这个

英文:

Sure, the files present in the cache directory are deleted. the Android OS will always delete older files first, as reported by File.lastModified(). The Android OS will prioritise the deletion order of cache files via a special Quota function. If the total size of all files in the cache directory exceeds bytes returned by StorageManager.getCacheQuotaBytes(java.util.UUID), your app will be first whose cache files will be deleted when the system needs additional space. If this quota doesn't exceed, then your app will be at last sort (have the least chance) that its cache files would be deleted.

There is a better option than a cache directory, If you want to store files, you can use File.getDir(String name, int mode). This directory will not be deleted unless the user clears storage. If the user clears storage, then the app will reset itself. Thus, when the user opens it again, it will run from the start.

If you want to store preferences then, SharedPreferences is the best option to go with. you can store tons of key-value pairs. Check out this.

答案2

得分: 0

如你所知,Android 操作系统通常会根据文件的 lastModified() 来开始删除文件。这意味着你已经在使用的文件将是最后被删除的文件(这使得这种情况成为在那一刻丢失文件的特殊情况)。

但是,如果你希望依赖于这个文件,我建议使用 app_data 文件夹。

getDir("data", Context.MODE_PRIVATE).path

这个文件将一直保留在那里,只要你需要,你可以在任何时候删除这个文件。

英文:

As you know, Android OS will always start to remove the file's based on the lastModified(). It means the file you're already using. It will be the last file to delete (which makes this an edge case of losing the file at that moment).
But, In case you want to rely on the file. I would suggest using the app_data folder.

getDir("data", Context.MODE_PRIVATE).path

The file will remain there as long as you need and you can delete the file when you want.

答案3

得分: -1

好的,以下是已翻译的部分:

"如果文件在应用程序内部被打开,那么从技术上讲,该文件正在使用并被锁定。因此,如果操作系统尝试删除该文件,它将无法执行此操作。

一旦您的应用程序释放了该文件,该文件将被存储在缓存中,如果应用程序不经常使用,那么该文件将被删除。

因此,结论是:“是的,这些文件将被删除”。"

英文:

Well, we all know that if a file is opened in the Application itself then that file is being used and is locked technically. So, if the OS tries to delete that file, it won't be able to do it.

Once, your app releases the file, that file is stored in the cache and will be deleted if the app is not frequently used.

So, the verdict is "Yes, the files will be deleted"

huangapple
  • 本文由 发表于 2023年3月3日 22:30:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定