GitHub - badrazizi/java-in-memory-cache: In-Memory Cache for Java(JVM), Asynchronous generic thread-safe in-memory cache

Asynchronous generic thread-safe in-memory cache

Features:

  • provides basic caching functionalities like: add, get and evict, etc.
  • time-based expiration of entries, measured since last access or last write.
  • result returned on asynchronous promise.
  • all operations on this cache are thread-safe.
  • does not block caller thread.

usage

  • add
fun <T : Any> add(key: String, value: T, lifeTime: Long = 0, unit: TimeUnit = TimeUnit.SECONDS): Future<Boolean>
val posts: Storage = Storage.newInstance()
posts.add(key, value, time, timeUnit).onComplete { ar ->
    if (ar.succeeded()) {
        // do something
    } else {
        // do something
    }
}
  • get by key
fun <T> get(key: String): Future<T>
val posts: Storage = Storage.newInstance()
posts.get<Post>(title).onComplete { ar ->
    if (ar.succeeded()) {
        // do something
    } else {
        // do something
    }
}
  • get by predicate
fun <T> get(predicate: (T) -> Boolean): Future<T>
val posts: Storage = Storage.newInstance()
posts.get<Post> { p -> p.title == title }.onComplete { ar ->
    if (ar.succeeded()) {
        // do something
    } else {
        // do something
    }
}
  • get multiple value as HashMap<String, Any>
fun get(vararg keys: String): Future<HashMap<String, Any>> 
val posts: Storage = Storage.newInstance()
posts.get(title1, title2, title3).onComplete { ar ->
    if (ar.succeeded()) {
        // do something
    } else {
        // do something
    }
}
  • get keys using regex
fun getKeys(vararg regexs: Regex): Future<List<String>>
val posts: Storage = Storage.newInstance()
posts.get("Posts\\.${id}".toRegex()).onComplete { ar ->
    if (ar.succeeded()) {
        // do something
    } else {
        // do something
    }
}
  • evict by key
fun evict(vararg keys: String): Future<Int>
val posts: Storage = Storage.newInstance()
posts.evict(title).onComplete { ar ->
    if (ar.succeeded()) {
        // do something
    } else {
        // do something
    }
}