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