Created
February 23, 2017 17:24
-
-
Save jbarr21/ef0fa0cd33712f89a461a8ad515f1d3b to your computer and use it in GitHub Desktop.
A Glide memory cache backed by a delegate Picasso LruCache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Bitmap; | |
import android.support.annotation.NonNull; | |
import com.bumptech.glide.load.Key; | |
import com.bumptech.glide.load.engine.Resource; | |
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; | |
import com.bumptech.glide.load.engine.cache.MemoryCacheAdapter; | |
import com.bumptech.glide.load.resource.bitmap.BitmapResource; | |
import com.squareup.picasso.LruCache; | |
public class GlideMemoryCache extends MemoryCacheAdapter { | |
private static final String GLIDE_PREFIX = "glide "; | |
@NonNull private LruCache picassoMemoryCache; | |
@NonNull private BitmapPool bitmapPool; | |
public GlideMemoryCache(@NonNull LruCache picassoMemoryCache, @NonNull BitmapPool bitmapPool) { | |
this.picassoMemoryCache = picassoMemoryCache; | |
this.bitmapPool = bitmapPool; | |
} | |
@Override | |
public Resource<?> put(Key key, Resource<?> resource) { | |
if (resource instanceof BitmapResource) { | |
Bitmap bitmap = ((BitmapResource) resource).get(); | |
picassoMemoryCache.set(keyToString(key), bitmap); | |
} | |
return resource; | |
} | |
@Override | |
public Resource<?> remove(Key key) { | |
Bitmap bitmap = picassoMemoryCache.get(keyToString(key)); | |
if (bitmap != null) { | |
return BitmapResource.obtain(bitmap, bitmapPool); | |
} | |
return null; | |
} | |
@Override | |
public int getCurrentSize() { | |
return picassoMemoryCache.size(); | |
} | |
@Override | |
public int getMaxSize() { | |
return picassoMemoryCache.maxSize(); | |
} | |
@Override | |
public void clearMemory() { | |
picassoMemoryCache.clear(); | |
} | |
private String keyToString(Key key) { | |
return GLIDE_PREFIX + key.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment