facebook twitter hatena line email

「Android/DiskLruCache」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(公式?)
(公式?)
行11: 行11:
 
google
 
google
 
https://android.googlesource.com/platform/libcore/+/jb-mr2-release/luni/src/main/java/libcore/io/DiskLruCache.java
 
https://android.googlesource.com/platform/libcore/+/jb-mr2-release/luni/src/main/java/libcore/io/DiskLruCache.java
 +
 +
okhttp3
 +
https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/internal/cache/DiskLruCache.java
 +
 +
mopub
 +
https://github.com/mopub/mopub-android-sdk/blob/master/mopub-sdk/mopub-sdk-base/src/main/java/com/mopub/common/DiskLruCache.java
 +
==bitmapサンプル==
 +
https://developer.android.com/topic/performance/graphics/cache-bitmap#java
 +
 +
<pre>
 +
private DiskLruCache mDiskLruCache;
 +
private final Object mDiskCacheLock = new Object();
 +
private boolean mDiskCacheStarting = true;
 +
private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB
 +
private static final String DISK_CACHE_SUBDIR = "thumbnails";
 +
 +
@Override
 +
protected void onCreate(Bundle savedInstanceState) {
 +
    ...
 +
    // Initialize memory cache
 +
    ...
 +
    // Initialize disk cache on background thread
 +
    File cacheDir = getDiskCacheDir(this, DISK_CACHE_SUBDIR);
 +
    new InitDiskCacheTask().execute(cacheDir);
 +
    ...
 +
}
 +
 +
class InitDiskCacheTask extends AsyncTask<File, Void, Void> {
 +
    @Override
 +
    protected Void doInBackground(File... params) {
 +
        synchronized (mDiskCacheLock) {
 +
            File cacheDir = params[0];
 +
            mDiskLruCache = DiskLruCache.open(cacheDir, DISK_CACHE_SIZE);
 +
            mDiskCacheStarting = false; // Finished initialization
 +
            mDiskCacheLock.notifyAll(); // Wake any waiting threads
 +
        }
 +
        return null;
 +
    }
 +
}
 +
 +
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
 +
    ...
 +
    // Decode image in background.
 +
    @Override
 +
    protected Bitmap doInBackground(Integer... params) {
 +
        final String imageKey = String.valueOf(params[0]);
 +
 +
        // Check disk cache in background thread
 +
        Bitmap bitmap = getBitmapFromDiskCache(imageKey);
 +
 +
        if (bitmap == null) { // Not found in disk cache
 +
            // Process as normal
 +
            final Bitmap bitmap = decodeSampledBitmapFromResource(
 +
                    getResources(), params[0], 100, 100));
 +
        }
 +
 +
        // Add final bitmap to caches
 +
        addBitmapToCache(imageKey, bitmap);
 +
 +
        return bitmap;
 +
    }
 +
    ...
 +
}
 +
 +
public void addBitmapToCache(String key, Bitmap bitmap) {
 +
    // Add to memory cache as before
 +
    if (getBitmapFromMemCache(key) == null) {
 +
        mMemoryCache.put(key, bitmap);
 +
    }
 +
 +
    // Also add to disk cache
 +
    synchronized (mDiskCacheLock) {
 +
        if (mDiskLruCache != null && mDiskLruCache.get(key) == null) {
 +
            mDiskLruCache.put(key, bitmap);
 +
        }
 +
    }
 +
}
 +
 +
public Bitmap getBitmapFromDiskCache(String key) {
 +
    synchronized (mDiskCacheLock) {
 +
        // Wait while disk cache is started from background thread
 +
        while (mDiskCacheStarting) {
 +
            try {
 +
                mDiskCacheLock.wait();
 +
            } catch (InterruptedException e) {}
 +
        }
 +
        if (mDiskLruCache != null) {
 +
            return mDiskLruCache.get(key);
 +
        }
 +
    }
 +
    return null;
 +
}
 +
 +
// Creates a unique subdirectory of the designated app cache directory. Tries to use external
 +
// but if not mounted, falls back on internal storage.
 +
public static File getDiskCacheDir(Context context, String uniqueName) {
 +
    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
 +
    // otherwise use internal cache dir
 +
    final String cachePath =
 +
            Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
 +
                    !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath() :
 +
                            context.getCacheDir().getPath();
 +
 +
    return new File(cachePath + File.separator + uniqueName);
 +
}
 +
</pre>

2018年11月27日 (火) 18:10時点における版

準備

以下追加

  • app/build.gradle
dependencies {
    implementation 'com.jakewharton:disklrucache:2.0.2'
}

公式?

https://github.com/JakeWharton/DiskLruCache

google https://android.googlesource.com/platform/libcore/+/jb-mr2-release/luni/src/main/java/libcore/io/DiskLruCache.java

okhttp3 https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/internal/cache/DiskLruCache.java

mopub https://github.com/mopub/mopub-android-sdk/blob/master/mopub-sdk/mopub-sdk-base/src/main/java/com/mopub/common/DiskLruCache.java

bitmapサンプル

https://developer.android.com/topic/performance/graphics/cache-bitmap#java

private DiskLruCache mDiskLruCache;
private final Object mDiskCacheLock = new Object();
private boolean mDiskCacheStarting = true;
private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB
private static final String DISK_CACHE_SUBDIR = "thumbnails";

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    // Initialize memory cache
    ...
    // Initialize disk cache on background thread
    File cacheDir = getDiskCacheDir(this, DISK_CACHE_SUBDIR);
    new InitDiskCacheTask().execute(cacheDir);
    ...
}

class InitDiskCacheTask extends AsyncTask<File, Void, Void> {
    @Override
    protected Void doInBackground(File... params) {
        synchronized (mDiskCacheLock) {
            File cacheDir = params[0];
            mDiskLruCache = DiskLruCache.open(cacheDir, DISK_CACHE_SIZE);
            mDiskCacheStarting = false; // Finished initialization
            mDiskCacheLock.notifyAll(); // Wake any waiting threads
        }
        return null;
    }
}

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
    ...
    // Decode image in background.
    @Override
    protected Bitmap doInBackground(Integer... params) {
        final String imageKey = String.valueOf(params[0]);

        // Check disk cache in background thread
        Bitmap bitmap = getBitmapFromDiskCache(imageKey);

        if (bitmap == null) { // Not found in disk cache
            // Process as normal
            final Bitmap bitmap = decodeSampledBitmapFromResource(
                    getResources(), params[0], 100, 100));
        }

        // Add final bitmap to caches
        addBitmapToCache(imageKey, bitmap);

        return bitmap;
    }
    ...
}

public void addBitmapToCache(String key, Bitmap bitmap) {
    // Add to memory cache as before
    if (getBitmapFromMemCache(key) == null) {
        mMemoryCache.put(key, bitmap);
    }

    // Also add to disk cache
    synchronized (mDiskCacheLock) {
        if (mDiskLruCache != null && mDiskLruCache.get(key) == null) {
            mDiskLruCache.put(key, bitmap);
        }
    }
}

public Bitmap getBitmapFromDiskCache(String key) {
    synchronized (mDiskCacheLock) {
        // Wait while disk cache is started from background thread
        while (mDiskCacheStarting) {
            try {
                mDiskCacheLock.wait();
            } catch (InterruptedException e) {}
        }
        if (mDiskLruCache != null) {
            return mDiskLruCache.get(key);
        }
    }
    return null;
}

// Creates a unique subdirectory of the designated app cache directory. Tries to use external
// but if not mounted, falls back on internal storage.
public static File getDiskCacheDir(Context context, String uniqueName) {
    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
    // otherwise use internal cache dir
    final String cachePath =
            Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
                    !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath() :
                            context.getCacheDir().getPath();

    return new File(cachePath + File.separator + uniqueName);
}