AndroidCache Library

Android LRU Based Disk Caching Utility

License

License

GroupId

GroupId

com.himanshuvirmani
ArtifactId

ArtifactId

androidcache
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

AndroidCache Library
Android LRU Based Disk Caching Utility
Project URL

Project URL

https://github.com/himanshuvirmani/AndroidDiskCache
Source Code Management

Source Code Management

https://github.com/himanshuvirmani/AndroidDiskCache

Download androidcache

How to add to project

<!-- https://jarcasting.com/artifacts/com.himanshuvirmani/androidcache/ -->
<dependency>
    <groupId>com.himanshuvirmani</groupId>
    <artifactId>androidcache</artifactId>
    <version>1.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.himanshuvirmani/androidcache/
implementation 'com.himanshuvirmani:androidcache:1.0.2'
// https://jarcasting.com/artifacts/com.himanshuvirmani/androidcache/
implementation ("com.himanshuvirmani:androidcache:1.0.2")
'com.himanshuvirmani:androidcache:jar:1.0.2'
<dependency org="com.himanshuvirmani" name="androidcache" rev="1.0.2">
  <artifact name="androidcache" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.himanshuvirmani', module='androidcache', version='1.0.2')
)
libraryDependencies += "com.himanshuvirmani" % "androidcache" % "1.0.2"
[com.himanshuvirmani/androidcache "1.0.2"]

Dependencies

compile (2)

Group / Artifact Type Version
com.google.code.gson : gson jar 2.3
com.jakewharton : disklrucache jar 2.0.2

Project Modules

There are no modules declared in this project.

Android Disk Cache

Introduction

Cache plays an important role in android as there is a crunch of memory and consistent network connectivity on mobile device. Hitting server again and again for the rarely changing data also takes a toll on server resources. No one likes to see loading icon till the data is being fetched from server. AndroidDiskCache library is there to make your job easy when working towards resolving above mentioned use cases.

Android disk cache is an LRU based Disk Cache which persists your android pojo/data objects in a key value format in your phone's internal memory. It uses the outstanding DiskLruCache library) of JSON representations of your Objects (using the superb GSON library) and an in-memory, runtime cache of your Objects. You can optionally specify a time when those cache entries expire, and the goodness of cache-rush-mitigation is baked right into the crust.

Original credit for the base of this project goes out to iainconnor/ObjectCache. This project did most things right but did not use enough Java Generics standard to make it easy to use and integrate with your project. Also added is the debug logs to help you test and identify if you are going in the right direction and are getting the desired results from cache.

Installing in Gradle

  1. Add the repository to your build.gradle file;

    repositories {
    	mavenCentral()
    }
  2. And add the dependency;

    dependencies {
    	compile 'com.himanshuvirmani:androidcache:1.0.1'
    }

Installing in other build tools

  1. Download the .jar for the latest version from this repository.
  2. Add it to your project.
  3. If you're building for Android, beg your boss to give you the time to switch to Gradle.

Usage

First, you'll need to create an instance of global cache manager instance in your application class DiskCache ( File cacheDirectory, int appVersion, int cacheSizeKb ). For an Android application, this is simple;

  private void prepareCache() {
    final String cachePath = getCacheDir().getPath();
    final File cacheFile = new File(cachePath + File.separator + BuildConfig.APPLICATION_ID);
    try {
      Cache diskCache = new DiskCache(cacheFile, BuildConfig.VERSION_CODE, CACHE_SIZE);
      cacheManager = CacheManager.getInstance(diskCache);
      cacheManager.setDebug(true); //Do this if you want to see logs from cachemanager
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static CacheManager getCacheManager() {
    return cacheManager;
  }

You can get the CacheManager singleton instance by creating a getter in Application class;

CacheManager cacheManager = MainApplication.getCacheManager();

Insert an Object to be cached;

MyObject myObject = new MyObject("foo");
cacheManager.put("myKey", myObject);

MyExpiryObject myExpiryObject = new MyExpiryObject("bar");
cacheManager.put("myKeyExpiry", myExpiryObject, CacheManager.ExpiryTimes.ONE_WEEK.asSeconds());

And retrieve it;

MyObject myObject = cacheManager.get("myKey", MyObject.class);
if ( myObject != null ) {
	// Object was found!
} else {
	// Object was not found, or was expired.
	// You should re-generate it and trigger a `.put()`.
}

If you're on Android, these operations can be run off the main thread;

cacheManager.putAsync("myKeyExpiry", myExpiryObject, CacheManager.ExpiryTimes.ONE_WEEK.asSeconds(), new PutCallback() {
    @Override
    public void onSuccess () {

    }

    @Override
    public void onFailure ( Exception e ) {

    }
});

cacheManager.getAsync("myKeyExpiry", ExpiryObject.class, new GetCallback<ExpiryObject>() {
    @Override
    public void onSuccess ( Result<ExpiryObject> myObject ) {
	if ( myObject.getCachedObject() != null ) {
        	// Object was found!
        } else {
        	if ( myObject.isExpired()) {
        	    // Object is expired
        	} else {
        	    // Object was never added to cache.
        	}
        }
    }

    @Override
    public void onFailure ( Exception e ) {

    }
});

If you want to clear the cache manually, you can use;

diskCache.clearCache();

ToDo

To add in-memory cache option for cases where you do not want to maintain data in disk.

Contact

Would love to hear from you for any suggestions and extensions to this. [email protected].

Versions

Version
1.0.2
1.0.1
1.0.0