mskn73 / kache

Simple cache implemented in kotlin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

KACHE a Cache lib made with Kotlin

Build Status
codecov

Still in development

Add the dependency

  1. Add jitpack in your root build.gradle at the end of repositories
allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}
  1. Add the dependency
dependencies {
  compile "com.github.mskn73:kache:${kacheVersion}"
}

How to use

Kotlin example

  1. Implement the Kacheable interface for your class
internal inner class MyObject(override val key: String) : Kacheable

If you want to specifies the expiration time, annotates the class with `KacheLife:

@KacheLife(expiresTime = (60 * 1000).toLong())
internal inner class MyObject(override val key: String) : Kacheable
  1. Save and retrieve the object:
//Instantiate Kache
val k = Kache(applicationContext, cacheDir.toString())
//Save data
k.put(MyObject("fake@mail.com"))
//Get the data
val myKachedObject = k.get(MyObject::class.java, "fake@mail.com") as Kacheable

Java example

  1. Implement the Kacheable interface for your class
class MyObject implements Kacheable {
  private final String mail;

  public MyObject(String mail) {
    this.mail = mail;
  }

  @NotNull @Override public String getKey() {
    return mail;
  }
}

If you want to specifies the expiration time, annotates the class with `KacheLife:

@KacheLife(expiresTime = 60 * 1000)
class MyObject implements Kacheable {
  private final String mail;

  public MyObject(String mail) {
    this.mail = mail;
  }

  @NotNull @Override public String getKey() {
    return mail;
  }
}
  1. Save and retrieve the object:
//Instantiate Kache
Kache k = new Kache(getApplicationContext(), getCacheDir().toString());
//Save data
k.put(new MyObject("fake@mail.com"));
//Get the data
Kacheable myKachedObject = (Kacheable) k.get(MyObject.class, "fake@mail.com");

About

Simple cache implemented in kotlin

License:GNU General Public License v3.0


Languages

Language:Kotlin 88.9%Language:Java 11.1%