/ Cache

Disk LRU cache with persisted journal

Disk LRU cache with persisted journal

Disk LRU Cache

Disk LRU (least recently used) cache with persisted journal. This cache has specific capacity and location. Rarely requested files are evicted by actively used.

Lightweight and extremely easy to use.

Add dependency

implementation 'com.tomclaw.cache:cache:1.0'

Create DiskLruCache

long CACHE_SIZE = 500 * 1024; // Size in bytes
DiskLruCache cache = DiskLruCache.create(getCacheDir(), CACHE_SIZE);

Add file into cache

To manage some files by cache you just need to invoke put method like any Map.

Key - any string to request this file from cache.

File - file, that will be moved into cache.

String key = "some-key";
File file = File.createTempFile("random", ".dat");
cache.put(key, file);

Getting file from cache

To get file from cache, just invoke get method. Yes, also like any Map.

Key is the same you put this file into cache

This method will return File you put into cache or null, if file was evicted from cache.

String key = "some-key";
File file = cache.get(key);

Delete file from cache

To delete file from cache, just invoke delete method.

Key is the same you put this file into cache

File will be deleted from cache and from journal.

String key = "some-key";
cache.delete(key);

Clear cache

Sometime you may need to clear whole cache and drop all stored files.

cache().clearCache();

List keys in cache

To get all keys, managed by cache, invoke keySet() method.

This will return Set<String>.

List all keys in cache may be useful to check all files, stored in cache.

Set<String> keys = cache.keySet();

Get cache status information

There are some useful cache status information, that you can request.

cache.getCacheSize(); // Cache size in bytes, that you set up on cache creation.
cache.getUsedSpace(); // Size of all files, stored in cache.
cache.getFreeSpace(); // Free size in cache.
cache.getJournalSize(); // Internal cache journal size in bytes.

GitHub