https://github.com/jeziellago/kachej
An alternative to building cache easily backed by Kotlin coroutines 📦.
https://github.com/jeziellago/kachej
cache coroutines file files flow hacktoberfest kotlin
Last synced: 3 months ago
JSON representation
An alternative to building cache easily backed by Kotlin coroutines 📦.
- Host: GitHub
- URL: https://github.com/jeziellago/kachej
- Owner: jeziellago
- License: apache-2.0
- Created: 2020-05-05T01:30:45.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-07-27T01:40:00.000Z (about 4 years ago)
- Last Synced: 2025-03-31T07:51:13.763Z (7 months ago)
- Topics: cache, coroutines, file, files, flow, hacktoberfest, kotlin
- Language: Kotlin
- Homepage:
- Size: 159 KB
- Stars: 26
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README

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