Expirable Disk Lru Cache is a secure(with encryption) wrapper for [DiskLruCache](https://github.com/JakeWharton/DiskLruCache) that allows expiring of key/value pairs by specifying evictionTimeSpan. It has very simple API.

Overview

ExpirableDiskLruCache

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.

Eviction Logic

  • 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.

Usage

Initialize

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.

Put stuff

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);

Get Stuff

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);

Check for existence

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) {}

Remove Stuff

You can remove stuff out of ExpirableDiskLruCache synchronously:

try {
    ExpirableDiskLruCache.getInstance().remove("myKey");
} catch (Exception e) {
        //failure
}

or asynchronously:

    ExpirableDiskLruCache.getInstance().remove("myKey", deleteCallback);

Remove All

try {
    ExpirableDiskLruCache.getInstance().removeAll("myKey");
} catch (Exception e) {
        //failure
}

Including in your project

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>

FAQs

What kind of objects can I add to ExpirableDiskLruCache?

Anything that GSON can serialize.

What happens if my cache size is exceeded?

Older objects will be removed in a LRU (Least Recently Used) order.

Contributing

Contributions welcome via Github pull requests. Please write test cases as well.

Credits

ExpirableDiskLruCache is just a tiny little convenience wrapper around the following fantastic projects:

Thanks!

License

This project is licensed under the MIT License. Please refer the License.txt file.

You might also like...
A simple Android utils library to write any type of data into cache files and read them later.

CacheUtilsLibrary This is a simple Android utils library to write any type of data into cache files and then read them later, using Gson to serialize

An easy-to-use, cross-platform measurement tool that pulls data out of CD pipelines and analysis the four key metrics for you.
An easy-to-use, cross-platform measurement tool that pulls data out of CD pipelines and analysis the four key metrics for you.

Maintained by SEA team, ThoughtWorks Inc. Read this in other languages: English, 简体中文 Table of Contents About the Project Usage How to Compute Contrib

 nestegg - Very simple Kotlin caching library
nestegg - Very simple Kotlin caching library

nestegg - Very simple Kotlin caching library

Wrapper around the android Camera class that simplifies its usage

EasyCamera Wrapper around the android Camera class that simplifies its usage (read more about the process) Usage: // the surface where the preview wil

🪁 Android Resources Wrapper Library
🪁 Android Resources Wrapper Library

Kite Android Resource Wrapper Library. TLDR Fed up with typing ContextCompat, resources and context all over your apps to access your resources? Say n

React Native wrapper to bridge our iOS and Android SDK
React Native wrapper to bridge our iOS and Android SDK

React Native wrapper to bridge our iOS and Android SDK

This project is an add-on for the excellent J2V8 Project. It allows users to debug JS running in V8 using Chrome DevTools. Uses Stetho for communication with Chrome DevTools.

J2V8-Debugger This project is an add-on for the excellent J2V8 Project. It allows users to debug JS running in V8 using Chrome DevTools. Uses Stetho f

adds an option to the Android Sharesheet that allows you to save files to your device.
adds an option to the Android Sharesheet that allows you to save files to your device.

Save On Device adds an option to the Android Sharesheet that allows you to save files to your device. Download Get the app from the Google Play Store

Allows usage of iCalendar files with the Android calendar provider

ical4android ical4android is a library for Android that brings together iCalendar and Android. It's a framework for parsing and generating iCalendar r

Releases(0.2)
Owner
Vijay Rawat
Vijay Rawat
Java implementation of a Disk-based LRU cache which specifically targets Android compatibility.

Disk LRU Cache A cache that uses a bounded amount of space on a filesystem. Each cache entry has a string key and a fixed number of values. Each key m

Jake Wharton 5.7k Dec 31, 2022
Keep data as a linked list on disk. A alternative way to reduce redundant operation for DiskLruCache

DiskLinkedList Keep data as a linked list on disk. An alternative way to reduce redundant operation for DiskLruCache Use-case Android have build-in Di

Cuong V. Nguyen 6 Oct 29, 2021
✔️ Secure, simple key-value storage for Android

Hawk 2.0 Secure, simple key-value storage for android Important Note This version has no backward compatibility with Hawk 1+ versions. If you still wa

Orhan Obut 3.9k Dec 20, 2022
DiskCache - Simple and readable disk cache for kotlin and android applications

DiskCache Simple and readable disk cache for kotlin and android applications (with journaled lru strategy) This is a simple lru disk cache, based on t

Giovanni Corte 14 Dec 2, 2022
Android Secure SharedPreferences Using Facebook Conceal Encryption

SharedChamber Android Project : SharedChamber on top of SharedPreferences using Facebook Conceal Description Conceal provides a set of Java APIs to pe

Hafiq 95 Nov 25, 2022
Secure Preference Manager for android. It uses various Encryption to protect your application's Shared Preferences.

Secure-Pref-Manager ##Secure Preference Manager is a simple Library to help you protect your Shared Preferences. Secure Preference Manager for android

Prashant Solanki 72 Nov 25, 2022
Reactor is key value database and is a great alternative to Shared Preferences.

Reactor Reactor is a fast and secure key-value library for Android, and has an embedded database based on the JSON structure and is a great alternativ

mr amir abbas 37 Oct 30, 2022
Android Shared preference wrapper than encrypts the values of Shared Preferences. It's not bullet proof security but rather a quick win for incrementally making your android app more secure.

Secure-preferences - Deprecated Please use EncryptedSharedPreferences from androidx.security in preferenced to secure-preference. (There are no active

Scott Alexander-Bown 1.5k Dec 24, 2022
Very easy to use wrapper library for Android SharePreferences

Treasure English document Treasure是一个Android平台上基于SharePreferences的偏好存储库,只需要定义接口,无需编写实现,默认支持Serializable和Parcelable。运行时0反射,不仅使用方便而且性能和原生写法几乎无差别。 使用方法 1

星一 507 Nov 12, 2022
a simple cache for android and java

ASimpleCache ASimpleCache 是一个为android制定的 轻量级的 开源缓存框架。轻量到只有一个java文件(由十几个类精简而来)。 1、它可以缓存什么东西? 普通的字符串、JsonObject、JsonArray、Bitmap、Drawable、序列化的java对象,和 b

Michael Yang 3.7k Dec 14, 2022