droid-fu icon indicating copy to clipboard operation
droid-fu copied to clipboard

Use app cache for caching

Open jjfster opened this issue 14 years ago • 10 comments

Droid-Fu uses the app data storage for caching images.

Any way to have it use the app's cache, so that users can manage (read: clear) the app cache without affecting settings, etc.?

jjfster avatar Jul 11 '11 17:07 jjfster

I probably just don't understand this fully (of course), but now when diskCacheIsEnabled, the app grows large very quickly.

Thanks.

jjfster avatar Jul 11 '11 17:07 jjfster

How do you measure the disk cache size? I'm going to the app settings and finding the cache is near zero, even though I see in the logs messages like "DISK cache hit for xxx".

gradha avatar Jul 12 '11 08:07 gradha

You can specify where you want to write the files using the AbstractCache.DISK_CACHE_INTERNAL and AbstractCache.DISK_CACHE_SDCARD fields.

ImageLoader's ImageCache caches to SD by default, unless no SD card is available.

mttkay avatar Jul 12 '11 15:07 mttkay

app cache is near zero because the cache is being registered in the app's data. This was my original question: How do we assign the cache to the app cache, so users can manage the cache in Manage Applications, and not have to delete the app data (which deletes all settings, databases, etc)?

jjfster avatar Jul 12 '11 16:07 jjfster

Hmmm, it actually should already do that. We use getApplicationContext().getCacheDir() to determine where to write cached files when DISK_CACHE_INTERNAL is requested as the caching mode. Here are the relevant parts:

public boolean enableDiskCache(Context context, int storageDevice) {
    Context appContext = context.getApplicationContext();

    String rootDir = null;
    if (storageDevice == DISK_CACHE_SDCARD
            && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        // SD-card available
        rootDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/"
                + appContext.getPackageName() + "/cache";
    } else {
        File internalCacheDir = appContext.getCacheDir();
        // apparently on some configurations this can come back as null
        if (internalCacheDir == null) {
            return (isDiskCacheEnabled = false);
        }
        rootDir = internalCacheDir.getAbsolutePath();
    }

...

I think there are actually helper functions for this nowadays, but we wanted to remain backwards compatible to Android 1.5. So to be honest: I'm not sure why this is not working, I'd have to debug this first.

mttkay avatar Jul 12 '11 16:07 mttkay

@jjfster: I am not sure I get your question. Are you asking how is it possible to wipe the files in "/Android/data/package/cache/" when the user chooses the clear the cache from the Settings? If so I am not sure how to do that, I thought the Android OS took care of that but it doesn't. Moreover: I tried to clear data but even when after that the files in that directory are still there.

futtetennista avatar Jul 13 '11 13:07 futtetennista

When the images are cached, they are cached to the app's data storage, not the app cache. When you go on your device to: Settings/Applications/Manage Applications/[your app], you can see that the app cache (the second item in the dialog to clear) does NOT grow, but the app data storage (the first item on the list to clear) DOES grow, when using Droid-Fu's abstract cache. Therefore, Droid-Fu is not using the app cache, but the data storage location, at least as the Android OS sees it (regardless of where it stores the actual file, i.e., sdcard, etc.). I was just hoping to be able to cache to the app cache rather than app data storage, so that if users want to clear the cache, they can do that by selecting "clear cache," rather than selecting "clear data" (which clears all the settings, databases, etc.). Hope this explains it a little more clearly. I guess if I had to I could add my own cache management buttons to the app, but that seems a little sillly. Thanks.

jjfster avatar Jul 13 '11 16:07 jjfster

Hm yeah that's definitely an issue if that's true. I remember vaguely that in one of the newer SDK versions Google added a standard helper function to get a reference to the standard app cache dir. However, these methods are unavailable before 2.2 or so, that's why we never used them.

mttkay avatar Jul 14 '11 09:07 mttkay

Ok, so I did a little playing around. It looks like no matter what I do, getExternalCacheDir() does not participate in the "app cache" helper feature. The "app cache" is only used and recognizes cache that is in the getCacheDir() directory.

Pretty silly, but I thought I didn't quite understand how they were handling app cache, and I just proved it!. Oh well, unless I'm still missing something?

jjfster avatar Jul 15 '11 00:07 jjfster

Which API level was that?

mttkay avatar Jul 18 '11 09:07 mttkay