提交 e777bc05 编写于 作者: S Sam

Merge pull request #430 from Tolriq/cacheFactory

Extends glide disk cache factories
...@@ -19,6 +19,7 @@ import android.widget.ImageView; ...@@ -19,6 +19,7 @@ import android.widget.ImageView;
import com.bumptech.glide.load.DecodeFormat; import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.Engine; import com.bumptech.glide.load.engine.Engine;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.cache.MemoryCache; import com.bumptech.glide.load.engine.cache.MemoryCache;
import com.bumptech.glide.load.engine.prefill.BitmapPreFiller; import com.bumptech.glide.load.engine.prefill.BitmapPreFiller;
import com.bumptech.glide.load.engine.prefill.PreFillType; import com.bumptech.glide.load.engine.prefill.PreFillType;
...@@ -79,10 +80,7 @@ import java.util.List; ...@@ -79,10 +80,7 @@ import java.util.List;
* {@link MemoryCache}. * {@link MemoryCache}.
*/ */
public class Glide { public class Glide {
/** 250 MB of cache. */
static final int DEFAULT_DISK_CACHE_SIZE = 250 * 1024 * 1024;
private static final String DEFAULT_DISK_CACHE_DIR = "image_manager_disk_cache";
private static final String TAG = "Glide"; private static final String TAG = "Glide";
private static volatile Glide glide; private static volatile Glide glide;
...@@ -110,7 +108,7 @@ public class Glide { ...@@ -110,7 +108,7 @@ public class Glide {
* @param context A context. * @param context A context.
*/ */
public static File getPhotoCacheDir(Context context) { public static File getPhotoCacheDir(Context context) {
return getPhotoCacheDir(context, DEFAULT_DISK_CACHE_DIR); return getPhotoCacheDir(context, DiskLruCacheFactory.DEFAULT_DISK_CACHE_DIR);
} }
/** /**
......
...@@ -189,7 +189,7 @@ public class GlideBuilder { ...@@ -189,7 +189,7 @@ public class GlideBuilder {
} }
if (diskCacheFactory == null) { if (diskCacheFactory == null) {
diskCacheFactory = new InternalCacheDiskCacheFactory(context, Glide.DEFAULT_DISK_CACHE_SIZE); diskCacheFactory = new InternalCacheDiskCacheFactory(context);
} }
if (engine == null) { if (engine == null) {
......
...@@ -13,6 +13,11 @@ public interface DiskCache { ...@@ -13,6 +13,11 @@ public interface DiskCache {
* An interface for lazily creating a disk cache. * An interface for lazily creating a disk cache.
*/ */
interface Factory { interface Factory {
/** 250 MB of cache. */
int DEFAULT_DISK_CACHE_SIZE = 250 * 1024 * 1024;
String DEFAULT_DISK_CACHE_DIR = "image_manager_disk_cache";
/** /**
* Returns a new disk cache, or {@code null} if no disk cache could be created. * Returns a new disk cache, or {@code null} if no disk cache could be created.
*/ */
......
package com.bumptech.glide.load.engine.cache;
import java.io.File;
/**
* Creates an {@link com.bumptech.glide.disklrucache.DiskLruCache} based disk cache in the specified disk cache
* directory.
* <p/>
* If you need to make I/O access before returning the cache directory use
* the {@link DiskLruCacheFactory#DiskLruCacheFactory(CacheDirectoryGetter, int)} constructor variant.
*/
public class DiskLruCacheFactory implements DiskCache.Factory {
private final int diskCacheSize;
private final CacheDirectoryGetter cacheDirectoryGetter;
/**
* Interface called out of UI thread to get the cache folder.
*/
public interface CacheDirectoryGetter {
File getCacheDirectory();
}
public DiskLruCacheFactory(final String diskCacheFolder, int diskCacheSize) {
this(new CacheDirectoryGetter() {
@Override
public File getCacheDirectory() {
return new File(diskCacheFolder);
}
}, diskCacheSize);
}
public DiskLruCacheFactory(final String diskCacheFolder, final String diskCacheName, int diskCacheSize) {
this(new CacheDirectoryGetter() {
@Override
public File getCacheDirectory() {
return new File(diskCacheFolder, diskCacheName);
}
}, diskCacheSize);
}
/**
* When using this constructor {@link CacheDirectoryGetter#getCacheDirectory()} will be called out of UI thread,
* allowing to do I/O access without performance impacts.
*
* @param cacheDirectoryGetter Interface called out of UI thread to get the cache folder.
* @param diskCacheSize Desired max bytes size for the LRU disk cache.
*/
public DiskLruCacheFactory(CacheDirectoryGetter cacheDirectoryGetter, int diskCacheSize) {
this.diskCacheSize = diskCacheSize;
this.cacheDirectoryGetter = cacheDirectoryGetter;
}
@Override
public DiskCache build() {
File cacheDir = cacheDirectoryGetter.getCacheDirectory();
if (cacheDir == null) {
return null;
}
if (!cacheDir.mkdirs() && (!cacheDir.exists() || !cacheDir.isDirectory())) {
return null;
}
return DiskLruCacheWrapper.get(cacheDir, diskCacheSize);
}
}
package com.bumptech.glide.load.engine.cache;
import android.content.Context;
import java.io.File;
/**
* Creates an {@link com.bumptech.glide.disklrucache.DiskLruCache} based disk cache in the external disk cache
* directory.
* <p/>
* <b>Images can be read by everyone when using external disk cache.</b>
*/
public final class ExternalCacheDiskCacheFactory extends DiskLruCacheFactory {
public ExternalCacheDiskCacheFactory(Context context) {
this(context, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR, DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE);
}
public ExternalCacheDiskCacheFactory(Context context, int diskCacheSize) {
this(context, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR, diskCacheSize);
}
public ExternalCacheDiskCacheFactory(final Context context, final String diskCacheName, int diskCacheSize) {
super(new CacheDirectoryGetter() {
@Override
public File getCacheDirectory() {
File cacheDirectory = context.getExternalCacheDir();
if (cacheDirectory == null) {
return null;
}
if (diskCacheName != null) {
return new File(cacheDirectory, diskCacheName);
}
return cacheDirectory;
}
}, diskCacheSize);
}
}
...@@ -2,47 +2,35 @@ package com.bumptech.glide.load.engine.cache; ...@@ -2,47 +2,35 @@ package com.bumptech.glide.load.engine.cache;
import android.content.Context; import android.content.Context;
import com.bumptech.glide.Glide;
import java.io.File; import java.io.File;
/** /**
* Creates an {@link com.bumptech.glide.disklrucache.DiskLruCache} based disk cache in the internal disk cache * Creates an {@link com.bumptech.glide.disklrucache.DiskLruCache} based disk cache in the internal disk cache
* directory. * directory.
*/ */
public final class InternalCacheDiskCacheFactory implements DiskCache.Factory { public final class InternalCacheDiskCacheFactory extends DiskLruCacheFactory {
private final Context context;
private final String diskCacheName;
private final int diskCacheSize;
public InternalCacheDiskCacheFactory(Context context, int diskCacheSize) { public InternalCacheDiskCacheFactory(Context context) {
this(context, null /*diskCacheName*/, diskCacheSize); this(context, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR, DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE);
} }
public InternalCacheDiskCacheFactory(Context context, String diskCacheName, int diskCacheSize) { public InternalCacheDiskCacheFactory(Context context, int diskCacheSize) {
this.context = context; this(context, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR, diskCacheSize);
this.diskCacheName = diskCacheName;
this.diskCacheSize = diskCacheSize;
} }
@Override public InternalCacheDiskCacheFactory(final Context context, final String diskCacheName, int diskCacheSize) {
public DiskCache build() { super(new CacheDirectoryGetter() {
DiskCache diskCache = null; @Override
final File cacheDir; public File getCacheDirectory() {
File cacheDirectory = context.getCacheDir();
if (diskCacheName != null) { if (cacheDirectory == null) {
cacheDir = Glide.getPhotoCacheDir(context, diskCacheName); return null;
} else { }
cacheDir = Glide.getPhotoCacheDir(context); if (diskCacheName != null) {
} return new File(cacheDirectory, diskCacheName);
}
if (cacheDir != null) { return cacheDirectory;
diskCache = DiskLruCacheWrapper.get(cacheDir, diskCacheSize); }
} }, diskCacheSize);
if (diskCache == null) {
diskCache = new DiskCacheAdapter();
}
return diskCache;
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册