ExpirableDiskLruCache is a wrapper for DiskLruCache that allows expiring of key/value pairs by specifying evictionTimeSpan. It has very simple API. If required it allows you to encrypt cached data by simply enabling encryptionEnabled to true. By default, it uses Conceal by facebook for encryption. You can provide your custom encryption/decryption as well.
- During get it will check if currentTime > currentTime + evictionSpan. Cache entry is removed.
- When cache reaches the allocated size, oldest entry is removed from the cache(LRU). Eviction time is not considered at all.
ExpirableDiskLruCache uses the internal storage allocated to your app. Before you can do anything, you need to initialize ExpirableDiskLruCache with the cache size.
try {
ExpirableDiskLruCache.getInstance().init(this, 4096); //in bytes
} catch (Exception e) {
//failure
}
or enable encryption
try {
ExpirableDiskLruCache.getInstance().init(this, 4096, false, true); //in bytes
} catch (Exception e) {
//failure
}
Third parameter is for enable logging. And fourth parameter is for enabling encryption. If you may want to use your custom encryption/decryption, use:
try {
ExpirableDiskLruCache.getInstance().init(this, 4096, false, true, customEncrypterDecrypter); //in bytes
} catch (Exception e) {
//failure
}
To create custom encrypter/decrypter simply implement EncrypterDecrypter.java
The best place to do this would be in your application's onCreate()
method.
Since this library depends directly on DiskLruCache, you can refer that project for more info on the maximum size you can allocate etc.
You can put objects into ExpirableDiskLruCache synchronously:
try {
ExpirableDiskLruCache.getInstance().put("myKey",myObject, myEvictionTimeSpan);
} catch (Exception e) {
//failure;
}
Or asynchronously:
ExpirableDiskLruCache.getInstance().put("myKey",myObject, myEvictionTimeSpan, putCallback);
You can get stuff out of ExpirableDiskLruCache synchronously.
try {
ExpirableDiskLruCache.getInstance().get("myKey",MyClass.class);
} catch (Exception e) {
//failure
}
Or asynchronously:
ExpirableDiskLruCache.getInstance().get("myKey",MyClass.class, getCallback);
If you wish to know whether an object exists for the given key, you can use:
try {
boolean objectExists = ExpirableDiskLruCache.getInstance().contains("myKey");
} catch (Exception e) {}
You can remove stuff out of ExpirableDiskLruCache synchronously:
try {
ExpirableDiskLruCache.getInstance().remove("myKey");
} catch (Exception e) {
//failure
}
or asynchronously:
ExpirableDiskLruCache.getInstance().remove("myKey", deleteCallback);
try {
ExpirableDiskLruCache.getInstance().removeAll("myKey");
} catch (Exception e) {
//failure
}
gradle:
Step 1. Add the JitPack repository to your build file
repositories {
maven {
url "https://jitpack.io"
}
}
Step 2. Add the dependency in the form
dependencies {
compile 'com.github.vijayrawatsan:ExpirableDiskLruCache:0.2'
}
maven:
Step 1. Add the JitPack repository to your build file
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
Step 2. Add the dependency in the form
<dependency>
<groupId>com.github.vijayrawatsan</groupId>
<artifactId>ExpirableDiskLruCache</artifactId>
<version>0.2</version>
</dependency>
Anything that GSON can serialize.
Older objects will be removed in a LRU (Least Recently Used) order.
Contributions welcome via Github pull requests. Please write test cases as well.
ExpirableDiskLruCache is just a tiny little convenience wrapper around the following fantastic projects:
Thanks!
This project is licensed under the MIT License. Please refer the License.txt file.