facebook twitter hatena line google mixi email

「Android/DiskLruCache」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(使い方サンプル)
(使い方サンプル)
行50: 行50:
 
sDiskLruCache.flush();
 
sDiskLruCache.flush();
 
editor.commit();
 
editor.commit();
public static void copyContent(final InputStream inputStream, final OutputStream outputStream) throws IOException {
+
 
 +
// 削除
 +
sDiskLruCache.delete();
 +
 
 +
public class Streams {
 +
    public static void copyContent(final InputStream inputStream, final OutputStream outputStream) throws IOException {
 
         if (inputStream == null || outputStream == null) {
 
         if (inputStream == null || outputStream == null) {
 
             throw new IOException("Unable to copy from or to a null stream.");
 
             throw new IOException("Unable to copy from or to a null stream.");
行59: 行64:
 
             outputStream.write(buffer, 0, length);
 
             outputStream.write(buffer, 0, length);
 
         }
 
         }
 +
    }
 
}
 
}
 
// 削除
 
sDiskLruCache.delete()
 
 
</pre>
 
</pre>
  

2018年11月29日 (木) 16:05時点における版

準備

以下追加

  • 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

準備

key部分のhogehogeはmd5やsha1などでhash化して使うと良い。

android/hash [ショートカット]

使い方サンプル

String UNIQUE_CACHE_NAME = "hogehoge-cache";
int VALUE_COUNT = 1;
int DISK_CACHE_INDEX = 0;
String cachePath = context.getCacheDir().getPath()
File cacheDirectory =  new File(cachePath + File.separator + UNIQUE_CACHE_NAME);
long diskCacheSizeBytes = 30 * 1024 * 1024; // 30 MB
// 初期化
DiskLruCache sDiskLruCache = DiskLruCache.open(
                        cacheDirectory,
                        APP_VERSION,
                        VALUE_COUNT,
                        diskCacheSizeBytes);
// 取得
DiskLruCache.Snapshot snapshot = sDiskLruCache.get("hogehoge");
final InputStream in = snapshot.getInputStream(DISK_CACHE_INDEX);

// 更新
DiskLruCache.Editor editor = sDiskLruCache.edit("hogehoge");
final OutputStream outputStream =
        new BufferedOutputStream(editor.newOutputStream(DISK_CACHE_INDEX));
Streams.copyContent(content, outputStream);
outputStream.flush();
outputStream.close();
sDiskLruCache.flush();
editor.commit();

// 削除
sDiskLruCache.delete();

public class Streams {
    public static void copyContent(final InputStream inputStream, final OutputStream outputStream) throws IOException {
        if (inputStream == null || outputStream == null) {
            throw new IOException("Unable to copy from or to a null stream.");
        }
        byte[] buffer = new byte[16384];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
    }
}

mopubに上記コードの詳細がある https://github.com/mopub/mopub-android-sdk/blob/master/mopub-sdk/mopub-sdk-base/src/main/java/com/mopub/common/CacheService.java

downloadしたものをcacheする

URL u = new URL(mUrl);
InputStream is = u.openStream();
HttpURLConnection huc = (HttpURLConnection)u.openConnection();
if (huc != null) {
    InputStream inputStream = new BufferedInputStream(huc.getInputStream());
    boolean diskPutResult = CacheService.putToDiskCache(sha1(mUrl), inputStream);
}

permission追加

保存場所はconstructのparamで指定するので、適宜記述となる。

mopubではcontext.getCacheDir()を使っておりAndroidManifestのpermissionは不要だった。 https://github.com/mopub/mopub-android-sdk/blob/master/mopub-sdk/mopub-sdk-base/src/main/java/com/mopub/common/CacheService.java

context.getExternalCacheDir()をもし使う場合はKitKatからpermission不要となったが、それ以前では必要なので以下のように追加する

<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />

http://wada811.blogspot.com/2014/09/storage-access-and-permission-in-android.html

https://developer.android.com/training/data-storage/files