LruCache
hotchemi
62
Visit GitHub Repo
Caching

LruCache

Build Status

A tiny, thread safe memory cache implementation which uses a LRU policy.

This implementation gives priority to simplicity of API. If you want to use a rich API, Use the LruCache in Android framework.

How to use

Cache

Cache is a interface.

It provides six methods as bellow.

  • V get(K key) - Gets an value for the specified key.
  • V put(K key, V value) - Puts an value in the cache for the specified key.
  • V remove(K key) - Removes the entry for key.
  • void clear() - Clears all the entries in the cache.
  • int getMaxMemorySize() - Returns the max memory size of the cache.
  • int getMemorySize() - Returns the current memory size of the cache.

LruCache

LruCache's API is definitely simple.

Cache<String, String> cache = new LruCache<>();
cache.put("a", "A");

Note

  • Default cache item capacity is ten. You can change the capacity via constructor.
  • Please override the getValueSize if you control memory size correctly.

BitmapLruCache

BitmapLruCache is the class that specialized in caching Bitmap.

private static final Bitmap A = Bitmap.createBitmap(1, 1, ALPHA_8);

Cache cache = new BitmapLruCache();
cache.put("a", A);

Test

mvn clean test

Support

Java 1.7 or greater.

Become Pro in Android by watching videos

OUR LEARNERS WORK AT