Skip to content

Commit

Permalink
Add our own version of an lru memory cache
Browse files Browse the repository at this point in the history
Allows us to remove our dependency on the
android support libs.
  • Loading branch information
Sam Judd committed Aug 8, 2013
1 parent 0d133b4 commit 76c7bc1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 57 deletions.
Binary file removed library/libs/android-support-v4.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions library/src/com/bumptech/glide/resize/ImageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.bumptech.glide.resize.cache.DiskCache;
import com.bumptech.glide.resize.cache.DiskCacheAdapter;
import com.bumptech.glide.resize.cache.DiskLruCacheWrapper;
import com.bumptech.glide.resize.cache.LruPhotoCache;
import com.bumptech.glide.resize.cache.LruMemoryCache;
import com.bumptech.glide.resize.cache.MemoryCache;
import com.bumptech.glide.resize.cache.MemoryCacheAdapter;
import com.bumptech.glide.util.Log;
Expand Down Expand Up @@ -294,7 +294,7 @@ public Thread newThread(Runnable runnable) {
}

if (memoryCache == null) {
memoryCache = new LruPhotoCache(getSafeMemoryCacheSize(context));
memoryCache = new LruMemoryCache(getSafeMemoryCacheSize(context));
}

if (diskCache == null) {
Expand Down
63 changes: 63 additions & 0 deletions library/src/com/bumptech/glide/resize/cache/LruMemoryCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2012. Bump Technologies Inc. All Rights Reserved.
*/

package com.bumptech.glide.resize.cache;

import android.graphics.Bitmap;

import java.util.LinkedHashMap;

/**
*/
public class LruMemoryCache implements MemoryCache {
private final LinkedHashMap<String, Bitmap> cache = new LinkedHashMap<String, Bitmap>(15, 0.75f, true);
private final int maxSize;
private ImageRemovedListener imageRemovedListener;
private int currentSize = 0;

private static int getSize(Bitmap bitmap) {
return bitmap.getHeight() * bitmap.getRowBytes();
}

public LruMemoryCache(int size) {
this.maxSize = size;
}

@Override
public boolean contains(String key) {
return cache.get(key) != null;
}

@Override
public Bitmap get(String key) {
return cache.get(key);
}

@Override
public Bitmap put(String key, Bitmap bitmap) {
currentSize += getSize(bitmap);
final Bitmap result = cache.put(key, bitmap);
evict();
return result;
}

@Override
public void setImageRemovedListener(ImageRemovedListener listener) {
this.imageRemovedListener = listener;
}

private void evict() {
LinkedHashMap.Entry<String, Bitmap> last;
while (currentSize > maxSize) {
last = cache.entrySet().iterator().next();
final Bitmap toRemove = last.getValue();
currentSize -= getSize(toRemove);
cache.remove(last.getKey());

if (imageRemovedListener != null) {
imageRemovedListener.onImageRemoved(toRemove);
}
}
}
}
55 changes: 0 additions & 55 deletions library/src/com/bumptech/glide/resize/cache/LruPhotoCache.java

This file was deleted.

0 comments on commit 76c7bc1

Please sign in to comment.