Write objects as files (to cache purpose) backed by Kotlin coroutines.
- This tool is an alternative to build cache without intermediate format (as JSON, XML or other).
- Why not? 😎
Transform objects in files to cache them, to restore after, or make anything.
// Create Serializable object
data class User(val name: String, val lastName: String) : Serializable
val user = User("Jeziel", "Lago")
Create Cache instance:
val cache = Cache.of(
parentDir = File("/cache/users"),
timeToLive = 60,
liveUnit = TimeUnit.MINUTES
)
// create file "user_123"
cache.put("user_123", user)
.catch { error -> /* error */ }
.collect { /* success */ }
// create file "user_collection"
val userCollection = cacheOf(user1, user2, ...)
cache.put("user_collection", userCollection)
.catch { error -> /* error */ }
.collect { /* success */ }
// create file "user_map"
val userMap = cacheOf(
"bob" to user1,
"ana" to user2
)
cache.put("user_map", userMap)
.catch { error -> /* error */ }
.collect { /* success */ }
cache.get<User>("user_123")
.catch { error -> /* error */ }
.collect { user -> /* success */ }
cache.get<CacheableList<User>>("user_collection")
.catch { error -> /* error */ }
.collect { list -> /* success */ }
cache.get<CacheableMap>("user_map")
.catch { error -> /* error */ }
.collect { map -> /* success */ }
cache.clear("user_collection")
.catch { error -> /* error */ }
.collect { /* success */ }
or
cache.clearAll()
.catch { error -> /* error */ }
.collect { /* success */ }
- Project
build.gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- Module
build.gradle
dependencies {
implementation 'com.github.jeziellago:kachej:VERSION`
}