Skip to content

Commit

Permalink
Externalized memory caching, only insert into memory if smaller than …
Browse files Browse the repository at this point in the history
…1/4 of cache size to reduce flooding
  • Loading branch information
DHuckaby committed Mar 5, 2013
1 parent 83f0da1 commit 5857b99
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.handlerexploit.common.utils;

import com.handlerexploit.prime.Configuration;
import com.handlerexploit.prime.utils.ImageManager;

import android.graphics.Bitmap;

public class BitmapMemoryLruCache extends LruCache<String, Bitmap> implements ImageManager.MemoryCache {

public BitmapMemoryLruCache() {
super(Configuration.MEM_CACHE_SIZE_KB * 1024);
}

@Override
public int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}

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

@Override
public Bitmap putCached(String key, Bitmap value) {
return put(key, value);
}
}
2 changes: 2 additions & 0 deletions library/src/com/handlerexploit/prime/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Properties;

import com.handlerexploit.common.utils.AssetsHelper;
import com.handlerexploit.common.utils.BitmapMemoryLruCache;
import com.handlerexploit.prime.factories.DefaultBitmapOptionsFactory;

public class Configuration {
Expand All @@ -32,6 +33,7 @@ public class Configuration {
public static final int ASYNC_THREAD_COUNT = (Integer) checkValue(sProperties.get("async.numThreads"), Runtime.getRuntime().availableProcessors() * 4);
public static final Location DOWNLOAD_LOCATION = (Location) checkValue(sProperties.get("cache.diskLocation"), Location.INTERNAL);
public static final String BITMAP_OPTIONS_FACTORY = (String) checkValue(sProperties.get("bitmapOptionsFactory"), DefaultBitmapOptionsFactory.class.getName());
public static final String MEMORY_CACHE = (String) checkValue(sProperties.get("bitmapMemoryCache"), BitmapMemoryLruCache.class.getName());

private static Properties loadProperties() {
Properties properties = new Properties();
Expand Down
32 changes: 17 additions & 15 deletions library/src/com/handlerexploit/prime/utils/ImageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public final class ImageManager {

private Handler mHandler = new Handler(Looper.getMainLooper());

private LruCache<String, Bitmap> mLruCache = newConfiguredLruCache();
private MemoryCache mMemoryCache;

private ExecutorService mNetworkExecutorService = newConfiguredThreadPool();

Expand All @@ -126,6 +126,7 @@ private ImageManager(Context context) {
File directory = new File(mCacheDirectory, "/images/");
mDiskLruCache = DiskLruCache.open(directory, 1, 1, Configuration.DISK_CACHE_SIZE_KB * 1024);
mBitmapOptionsFactory = (BitmapOptionsFactory) Class.forName(Configuration.BITMAP_OPTIONS_FACTORY).newInstance();
mMemoryCache = (MemoryCache) Class.forName(Configuration.MEMORY_CACHE).newInstance();
} catch (Throwable e) {
throw new RuntimeException(e);
}
Expand All @@ -138,8 +139,8 @@ public static synchronized ImageManager getInstance(Context context) {
return sInstance;
}

public synchronized void evictAll() {
mLruCache.evictAll();
public synchronized void evictAllFromMemory() {
mMemoryCache.evictAll();
}

/**
Expand Down Expand Up @@ -265,7 +266,7 @@ private String getMemoryCacheKey(String key, ExtendedRequest request) {
}

private Bitmap getBitmapFromMemory(String key, ExtendedRequest request) {
return mLruCache.get(getMemoryCacheKey(key, request));
return mMemoryCache.getCached(getMemoryCacheKey(key, request));
}

private ReentrantLock getLock(String key) {
Expand Down Expand Up @@ -318,8 +319,8 @@ private Bitmap getBitmapFromDisk(String key, ExtendedRequest request) {

if (bitmap != null) {
int bitmapByteSize = bitmap.getRowBytes() * bitmap.getHeight();
if (bitmapByteSize < mLruCache.maxSize()) {
mLruCache.put(getMemoryCacheKey(key, request), bitmap);
if (bitmapByteSize < mMemoryCache.maxSize() / 4) {
mMemoryCache.putCached(getMemoryCacheKey(key, request), bitmap);
}
}
} finally {
Expand Down Expand Up @@ -477,19 +478,20 @@ private ExecutorService newConfiguredThreadPool() {
return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, factory, handler);
}

private LruCache<String, Bitmap> newConfiguredLruCache() {
return new LruCache<String, Bitmap>(Configuration.MEM_CACHE_SIZE_KB * 1024) {
public static interface BitmapOptionsFactory {

@Override
public int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
};
public BitmapFactory.Options newOptionsFromStream(InputStream inputStream, int width, int height);
}

public static interface BitmapOptionsFactory {
public static interface MemoryCache {

public BitmapFactory.Options newOptionsFromStream(InputStream inputStream, int width, int height);
public int maxSize();

public void evictAll();

public Bitmap getCached(String key);

public Bitmap putCached(String key, Bitmap value);
}

/**
Expand Down

0 comments on commit 5857b99

Please sign in to comment.