LruCache

General

Category
Free
Tag
Caching
License
N/A
Registered
Sep 3, 2015
Favorites
1
Link
https://github.com/hotchemi/LruCache
See also
ObjectCache
CacheManage
RxCache
ReactiveCache
carbonite

Additional

Language
Java
Version
N/A
Created
Jan 11, 2015
Updated
Mar 11, 2018 (Retired)
Owner
Shintaro Katafuchi (hotchemi)
Contributor
Shintaro Katafuchi (hotchemi)
1
Activity
Badge
Generate
Download
Source code

LruCache

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.