Skip to content

Instantly share code, notes, and snippets.

@jbarr21
Created February 23, 2017 17:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbarr21/ef0fa0cd33712f89a461a8ad515f1d3b to your computer and use it in GitHub Desktop.
Save jbarr21/ef0fa0cd33712f89a461a8ad515f1d3b to your computer and use it in GitHub Desktop.
A Glide memory cache backed by a delegate Picasso LruCache
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