Skip to content

Instantly share code, notes, and snippets.

@jayjaykim
Last active August 6, 2016 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayjaykim/4d411ad71df18286f7229c962c3d10b4 to your computer and use it in GitHub Desktop.
Save jayjaykim/4d411ad71df18286f7229c962c3d10b4 to your computer and use it in GitHub Desktop.
LruCache Sample code
package com.jayjaylab.android.test.plainjava;
import android.support.v4.util.LruCache;
import android.util.Log;
/**
* Created by jayjay on 2016. 8. 6..
*/
public class TestLruCache {
static final String TAG = TestLruCache.class.getSimpleName();
static final int CACHE_SIZE = 50;
public static void test() {
String msg1 = "Caching or hoardin";
String msg2 = "A cache or hoard";
String msg3 = "Treasure trove";
String msg4 = "Geocaching, an out";
String msg5 = "Bear cache, a bear-safe";
Log.d(TAG, "length(msg1 + msg2 + msg3) : " + (msg1.length() + msg2.length()
+ msg3.length()));
Log.d(TAG, "length(msg4) : " + msg4.length());
LruCache<Integer, String> lruCache = new LruCache<Integer, String>(CACHE_SIZE) {
@Override
protected int sizeOf(Integer key, String value) {
return value.length();
}
};
lruCache.put(msg1.hashCode(), msg1);
lruCache.put(msg2.hashCode(), msg2);
lruCache.put(msg3.hashCode(), msg3);
showSnapshot(lruCache);
lruCache.put(msg4.hashCode(), msg4);
showSnapshot(lruCache);
Log.d(TAG, "from cache : " + lruCache.get(msg1.hashCode()));
Log.d(TAG, "from cache : " + lruCache.get(msg2.hashCode()));
lruCache.put(msg5.hashCode(), msg5);
showSnapshot(lruCache);
}
static void showSnapshot(LruCache<?, ?> lruCache) {
Log.d(TAG, "createCount : " + lruCache.createCount() +
", evictionCount : " + lruCache.evictionCount() +
", hitCount : " + lruCache.hitCount() +
", maxSize : " + lruCache.maxSize() +
", missCount : " + lruCache.missCount() +
", putCount : " + lruCache.putCount());
Log.d(TAG, "snapshot : " + lruCache.snapshot());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment