当清除缓存目录中的Andr​​oid? [英] When to clear the cache dir in Android?

查看:110
本文介绍了当清除缓存目录中的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从互联网(展示设计师作品)显示图片的应用程序。我开始我的缓存在内部缓存目录中的内容,但应用程序的内容可能需要大约150 MB高速缓存的大小。什么android的文档说:

I have an application that displays pictures from the internet (showcase for designer work). I start caching my content in the internal cache directory, but the app content could take about 150 MB in cache size. And what android docs says :

您应该始终保持缓存文件自己,留在一个   空间的合理限度消耗,如1MB。当用户   卸载应用程序,这些文件将被删除。

You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your application, these files are removed.

于是我拿出来一看电流应用(Galaxy Nexus的)和缓存大小的应用程序是110 MB。但是,什么是奇怪的是,像谷歌电流和放大器的应用;谷歌地图缓存叫什么内容(USB存储器中的数据):

So I took a look at the Currents app (Galaxy Nexus) and the cache size for the application is 110 MB. But what's weird is that applications like Google Currents & Google Maps cache the content in something called (USB Storage Data) :

那么,什么是这个'USB存储器中的数据的previous应用程序使用。如果你在你的应用程序中实现缓存,你遍历所有在你的应用程序文件缓存每一个你需要插入一些东西,然后比较和清除时间得到的大小?还是你保持高速缓存的内容,直到Android的决定的时间来清理一些应用程序缓存目录?

So what is this 'USB Storage Data' that the previous application uses. And if you implement caching in your application, Do you loop over all your application files in cache to get the size every time you need to insert something and then compare and clear it? Or do you keep caching the content until Android decides its time to clean some application cache directory ?

我真的想知道什么是Android的高速缓存管理的流程,或者至少是其他应用程序与大型内容缓存做。

I'm really interested to know what is the flow of managing cache in Android, or at least what other applications do with large content to cache.

推荐答案

在我得到你的问题,以下是两种存储类型的简要说明:

Before I get to your question, here's a brief explanation of the two storage types:

这是文件系统上的一个应用程序特定的目录。这样做的目的此目录是存储临时数据的应用程序可能需要保持周围会话之间,但未必是至关重要的,让他们永远。通常,您可以访问该目录中的<$c$c>Context.getCacheDir().这将显示为高速缓存在你的应用程序设置。

This is an app-specific directory on the filesystem. The intent for this directory is store temporary data your application may need to keep around between sessions, but may not be vital to keep them forever. You typically access this directory with Context.getCacheDir(). This will show up as "Cache" on your app settings.

像缓存目录,你的应用程序也有拿着文件的应用程序特定的目录。此目录中的文件将一直存在,直到应用程序明确地删除它们,或应用程序被卸载。通常,您可以访问该目录中的<$c$c>Context.getFilesDir().这可以显示为应用程序的信息屏幕上的各种事情,但在你的屏幕截图,这是USB存储器中的数据。

Like the cache directory, your app also has an app-specific directory for holding files. Files in this directory will exist until the app explicitly deletes them or the app is uninstalled. You typically access this directory with Context.getFilesDir(). This can show up as various things on the app info screen, but in your screenshot this is "USB Storage Data".

注意:如果您想明确地放置在外部媒体(通常是SD卡),你可以使用<一个href="http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir%28java.lang.String%29"><$c$c>Context.getExternalFilesDir(String型) 。

NOTE: If you want to explicitly place on external media (typically SD card), you can use Context.getExternalFilesDir(String type).

这两个目录仅特定于应用程序的(其他应用程序不能访问)。一项所述的高速缓存和文件目录之间的差异是,如果系统得到低上存储,它是将释放资源首先是从缓存目录。该系统将不会从文件目录中清除所有数据。另一个区别是,高速缓存目录通常可以手动从该应用信息屏清除。该文件目录通常可以为好,但清理的文件目录也将清除缓存目录。

Both directories are specific only to your application (other apps do not have access). One of the differences between the cache and files directory is that if the system gets low on storage, the first place it is going to free resources is from your cache directory. The system will not clear any data from the files directory. Another difference is that the cache directory can typically be cleared manually from the app info screen. The files directory typically can as well, but clearing the files directory will also clear the cache directory.

有取决于如何至关重要的数据进行比较,你的应用程序的生命周期。如果你只需要一个会话数据和你怀疑你永远需要再次使用这些数据,那么就不要使用两种。只要保持在内存中,直到你不需要它。如果你怀疑你需要重复使用多个会话间的数据,但你不知道的有无的保持硬拷贝,使用缓存目录。如果你必须有这个数据不管是什么,或者如果它是相当需要持久存储,使用文件目录的大型数据。下面是一些例子我能想到的:

It depends on how vital that data is compared to the lifetime of your app. If you only need data for one session and you doubt you'll ever need to use that data again, then don't use either. Just keep it in memory until you don't need it. If you suspect you'll need to reuse the data between multiple sessions, but you don't have to keep a hard copy, use the cache directory. If you must have this data no matter what, or if it's rather large data that needs persistent storage, use the files directory. Here's some examples I can think of:

  • 缓存 - 一个最近打开的电子邮件
    • 一旦打开,缓存数据,以便当用户想再次读取该电子邮件,它加载瞬间,而利用网络再次检索相同的数据。我并不需要保持这样永远的,因为最终用户将电子邮件完成。
    • Cache - A recently opened email
      • Once opened, cache the data so when the user wants to read that email again, it loads instantly rather using the network again to retrieve the same data. I don't need to keep this forever, because eventually the user will be finished with the email.
      • 这是谁在说用户的动作:我想保留这个数据,所以我可以拉回来了,每当我需要它。因此,把它放在文件的目录,我再也不要直到用户希望它删除,删除此文件。

      Context.getCacheDir()的javadoc:

      请注意:你不应该依赖系统中删除这些文件,你的;   你应该始终有一个合理的最大值,如1 MB,对于   占用使用缓存文件的空间量,并清理这些文件   超过了空当。

      Note: you should not rely on the system deleting these files for you; you should always have a reasonable maximum, such as 1 MB, for the amount of space you consume with cache files, and prune those files when exceeding that space.

      有将使用1个MB的例子,但是这可能是也可能不是合理你的应用程序。无论如何,你需要设置一个硬最大。这样做的原因简单地归结为设计负责的应用程序。所以,当你应该检查?我会建议您检查您想放的东西在缓存目录中每一次。这里是一个非常简单的缓存管理器:

      It uses the example of 1 MB, but that may or may not be reasonable for your app. Regardless, you need to set a hard maximum. The reason for this simply comes down to designing a responsible app. So when should you check? I would recommend checking every time you want to put something in the cache directory. Here's a very simple cache manager:

      public class CacheManager {
      
          private static final long MAX_SIZE = 5242880L; // 5MB
      
          private CacheManager() {
      
          }
      
          public static void cacheData(Context context, byte[] data, String name) throws IOException {
      
              File cacheDir = context.getCacheDir();
              long size = getDirSize(cacheDir);
              long newSize = data.length + size;
      
              if (newSize > MAX_SIZE) {
                  cleanDir(cacheDir, newSize - MAX_SIZE);
              }
      
              File file = new File(cacheDir, name);
              FileOutputStream os = new FileOutputStream(file);
              try {
                  os.write(data);
              }
              finally {
                  os.flush();
                  os.close();
              }
          }
      
          public static byte[] retrieveData(Context context, String name) throws IOException {
      
              File cacheDir = context.getCacheDir();
              File file = new File(cacheDir, name);
      
              if (!file.exists()) {
                  // Data doesn't exist
                  return null;
              }
      
              byte[] data = new byte[(int) file.length()];
              FileInputStream is = new FileInputStream(file);
              try {
                  is.read(data);
              }
              finally {
                  is.close();
              }
      
              return data;
          }
      
          private static void cleanDir(File dir, long bytes) {
      
              long bytesDeleted = 0;
              File[] files = dir.listFiles();
      
              for (File file : files) {
                  bytesDeleted += file.length();
                  file.delete();
      
                  if (bytesDeleted >= bytes) {
                      break;
                  }
              }
          }
      
          private static long getDirSize(File dir) {
      
              long size = 0;
              File[] files = dir.listFiles();
      
              for (File file : files) {
                  if (file.isFile()) {
                      size += file.length();
                  }
              }
      
              return size;
          }
      }
      

      当然,这可能是一个昂贵的操作,所以你应该计划缓存在后台线程。

      Of course, this could be an expensive operation, so you should plan on caching on a background thread.

      此外,你需要它,这可能是因为复杂。在我的例子中,我假设被放置在高速缓存目录的根目录下的所有缓存文件,所以我不检查潜在的子目录。该例程用于删除文件也可以变得更复杂,如由最旧的访问日期删除文件

      Also, this could be as complicated as you need it to be. In my example, I'm assuming all cached files are placed at the root of the cache directory, so I don't check for potential sub-directories. The routine for deleting files can also become more sophisticated, such as deleting files by oldest access date.

      一件事决定缓存中的数据时,要记住的是,你需要时刻计划,你的缓存数据不存在的情况下。总有例行的地方获取外部手段的数据时,缓存没有它的存储空间。同样地,经常检查你的缓存之前获取数据到外部。缓存的目的是为了减少网络活动,长期的过程,并在你的应用程序提供一个负责任的UI。所以用它负责:)

      One thing to keep in mind when deciding to cache data is that you need to always plan for the case that your cached data no longer exists. Always have a routine in place to retrieve data by external means when your cache doesn't have it in storage. Likewise, always check your cache before retrieve data externally. The purpose of the cache is to cut down on network activity, long processes, and provide a responsive UI in your app. So use it responsibly :)

      这篇关于当清除缓存目录中的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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