下面列出了怎么用com.bumptech.glide.disklrucache.DiskLruCache的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public File get(Key key) {
String safeKey = safeKeyGenerator.getSafeKey(key);
File result = null;
try {
//It is possible that the there will be a put in between these two gets. If so that shouldn't be a problem
//because we will always put the same value at the same key so our input streams will still represent
//the same data
final DiskLruCache.Value value = getDiskCache().get(safeKey);
if (value != null) {
result = value.getFile(0);
}
} catch (IOException e) {
if (Log.isLoggable(TAG, Log.WARN)) {
Log.w(TAG, "Unable to get from disk cache", e);
}
}
return result;
}
@Override
public void put(Key key, Writer writer) {
String safeKey = safeKeyGenerator.getSafeKey(key);
writeLocker.acquire(key);
try {
DiskLruCache.Editor editor = getDiskCache().edit(safeKey);
// Editor will be null if there are two concurrent puts. In the worst case we will just silently fail.
if (editor != null) {
try {
File file = editor.getFile(0);
if (writer.write(file)) {
editor.commit();
}
} finally {
editor.abortUnlessCommitted();
}
}
} catch (IOException e) {
if (Log.isLoggable(TAG, Log.WARN)) {
Log.w(TAG, "Unable to put to disk cache", e);
}
} finally {
writeLocker.release(key);
}
}
/**
* 获取是否有某张原图的缓存
* 缓存模式必须是:DiskCacheStrategy.SOURCE 才能获取到缓存文件
*/
public static File getGlideCacheFile(Context context, String url) {
try {
url = QtalkStringUtils.findRealUrl(url);
OriginalKey originalKey = new OriginalKey(url, EmptySignature.obtain());
SafeKeyGenerator safeKeyGenerator = new SafeKeyGenerator();
String safeKey = safeKeyGenerator.getSafeKey(originalKey);
File file = new File(context.getCacheDir(), DiskCache.Factory.DEFAULT_DISK_CACHE_DIR);
DiskLruCache diskLruCache = DiskLruCache.open(file, 1, 1, DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE);
DiskLruCache.Value value = diskLruCache.get(safeKey);
if (value != null) {
return value.getFile(0);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 无法同步判断
* 参见:https://github.com/bumptech/glide/issues/639
*
* 4.0以上可用:
* val file: File? = try {
Glide.with(view.context).downloadOnly().load(url).apply(RequestOptions().onlyRetrieveFromCache(true)).submit().get()
} catch (e: Exception) {
e.printStackTrace()
null
}
*https://github.com/bumptech/glide/issues/2972
*
* @param url
* @return
*/
@Override
public boolean isCached(String url) {
OriginalKey originalKey = new OriginalKey(url, EmptySignature.obtain());
SafeKeyGenerator safeKeyGenerator = new SafeKeyGenerator();
String safeKey = safeKeyGenerator.getSafeKey(originalKey);
try {
DiskLruCache diskLruCache = DiskLruCache.open(new File(GlobalConfig.context.getCacheDir(), DiskCache.Factory.DEFAULT_DISK_CACHE_DIR), 1, 1, DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE);
DiskLruCache.Value value = diskLruCache.get(safeKey);
if (value != null && value.getFile(0).exists() && value.getFile(0).length() > 30) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 无法同步判断
* 参见:https://github.com/bumptech/glide/issues/639
*
* 4.0以上可用:
* val file: File? = try {
Glide.with(view.context).downloadOnly().load(url).apply(RequestOptions().onlyRetrieveFromCache(true)).submit().get()
} catch (e: Exception) {
e.printStackTrace()
null
}
*https://github.com/bumptech/glide/issues/2972
*
* @param url
* @return
*/
@Override
public boolean isCached(String url) {
OriginalKey originalKey = new OriginalKey(url, EmptySignature.obtain());
SafeKeyGenerator safeKeyGenerator = new SafeKeyGenerator();
String safeKey = safeKeyGenerator.getSafeKey(originalKey);
try {
DiskLruCache diskLruCache = DiskLruCache.open(new File(GlobalConfig.context.getCacheDir(), DiskCache.Factory.DEFAULT_DISK_CACHE_DIR), 1, 1, DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE);
DiskLruCache.Value value = diskLruCache.get(safeKey);
if (value != null && value.getFile(0).exists() && value.getFile(0).length() > 30) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public File getCacheFile(String url) {
OriginalKey originalKey = new OriginalKey(url, EmptySignature.obtain());
SafeKeyGenerator safeKeyGenerator = new SafeKeyGenerator();
String safeKey = safeKeyGenerator.getSafeKey(originalKey);
try {
DiskLruCache diskLruCache = DiskLruCache.open(new File(GlobalConfig.context.getCacheDir(), DiskCache.Factory.DEFAULT_DISK_CACHE_DIR), 1, 1, DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE);
DiskLruCache.Value value = diskLruCache.get(safeKey);
if (value != null) {
return value.getFile(0);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public File getCacheFile(String url) {
OriginalKey originalKey = new OriginalKey(url, EmptySignature.obtain());
SafeKeyGenerator safeKeyGenerator = new SafeKeyGenerator();
String safeKey = safeKeyGenerator.getSafeKey(originalKey);
try {
DiskLruCache diskLruCache = DiskLruCache.open(new File(GlobalConfig.context.getCacheDir(), DiskCache.Factory.DEFAULT_DISK_CACHE_DIR), 1, 1, DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE);
DiskLruCache.Value value = diskLruCache.get(safeKey);
if (value != null) {
return value.getFile(0);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public synchronized DiskLruCache getDiskCache() throws IOException {
if (diskLruCache == null) {
diskLruCache = DiskLruCache.open(directory, APP_VERSION, VALUE_COUNT, maxSize);
}
return diskLruCache;
}