|
| 1 | +# CachedFlow |
| 2 | + |
| 3 | +**A lightweight Kotlin Multiplatform caching library** designed to work using a key-based strategy system and flexible caching policies. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Simple key-based cache access using typed keys |
| 10 | +- Customizable caching strategies |
| 11 | +- Supports suspending and flow-based data operations |
| 12 | +- Pluggable storage backend (`Store`) for full platform flexibility |
| 13 | +- Built-in logging interface |
| 14 | +- Fully testable and decoupled from Android dependencies |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +> Add the library to your Kotlin Multiplatform project (available soon via Maven Central / GitHub Packages). |
| 21 | +
|
| 22 | +<details> |
| 23 | +<summary>Gradle (Kotlin DSL)</summary> |
| 24 | + |
| 25 | +```kotlin |
| 26 | +dependencies { |
| 27 | + implementation("com.dapadz:cachedflow:<version>") |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +</details> |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## Getting Started |
| 36 | + |
| 37 | +### 1. Create your own `Store` implementation |
| 38 | + |
| 39 | +The `Store` interface abstracts the storage layer: |
| 40 | + |
| 41 | +```kotlin |
| 42 | +interface Store { |
| 43 | + suspend fun <T: Any> get(key: StoreKey<T>): Flow<T?> |
| 44 | + suspend fun <T: Any> save(key: StoreKey<T>, value: T) |
| 45 | + suspend fun <T: Any> delete(key: StoreKey<T>) |
| 46 | + suspend fun clear() |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +Implement it using your platform’s local storage (e.g. `DataStore`, `SharedPreferences`, `NSUserDefaults`, file system, etc). |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +### 2. Initialize the Cache |
| 55 | + |
| 56 | +```kotlin |
| 57 | +fun main() { |
| 58 | + val store: Store = MyMultiplatformStore() |
| 59 | + Cache.initialize(store) |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +Optionally, provide a custom `Logger`: |
| 64 | + |
| 65 | +```kotlin |
| 66 | +Cache.initialize(store, logger = MyLogger()) |
| 67 | +``` |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +### 3. Define and Use Cache Keys |
| 72 | + |
| 73 | +Use built-in key helpers or define your own: |
| 74 | + |
| 75 | +```kotlin |
| 76 | +val userKey = stringCacheKey("user_profile") |
| 77 | +val ageKey = integerCacheKey("user_age") |
| 78 | +``` |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +### 4. Cache a Flow with Strategy |
| 83 | + |
| 84 | +```kotlin |
| 85 | +flow { emit(fetchUserProfileFromApi()) } |
| 86 | + .cache(userKey, CacheStrategyType.IF_HAVE) |
| 87 | + .collect { user -> println("User: $user") } |
| 88 | +``` |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## Cache Strategies |
| 93 | + |
| 94 | +Choose how the cache behaves during a Flow emission: |
| 95 | + |
| 96 | +| Strategy | Description | |
| 97 | +|---------------------|--------------------------------------------------------------| |
| 98 | +| `ONLY_CACHE` | Always use the cache. Throws if no value exists. | |
| 99 | +| `ONLY_REQUEST` | Always skip cache. Fetch fresh and optionally cache it. | |
| 100 | +| `IF_HAVE` *(default)* | Use cache if available, otherwise fallback to the flow. | |
| 101 | + |
| 102 | +#### Extend with Your Own CacheStrategy |
| 103 | + |
| 104 | +Implement the `CacheStrategy<T>` interface for full control: |
| 105 | + |
| 106 | +```kotlin |
| 107 | +abstract class CacheStrategy <T> ( |
| 108 | + protected val key: Key<T>, |
| 109 | + protected val cachedAfterLoad : Boolean |
| 110 | +) { |
| 111 | + abstract suspend fun execute(currentFlow: Flow<T>): Flow<T> |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## Keys |
| 118 | + |
| 119 | +Use the following factory functions to quickly define typed keys for common primitive types: |
| 120 | + |
| 121 | +| Key Type | Factory Function | Example | |
| 122 | +|----------|--------------------------|------------------------------------------| |
| 123 | +| `String` | `stringCacheKey(name)` | `val key = stringCacheKey("username")` | |
| 124 | +| `Int` | `integerCacheKey(name)` | `val key = integerCacheKey("user_age")` | |
| 125 | +| `Float` | `floatCacheKey(name)` | `val key = floatCacheKey("user_score")` | |
| 126 | +| `Boolean`| `booleanCacheKey(name)` | `val key = booleanCacheKey("is_logged")` | |
| 127 | + |
| 128 | +These keys inherit from `Key<T>` and include built-in logic to handle type-safe caching operations. |
| 129 | + |
| 130 | +#### Custom Key Example |
| 131 | + |
| 132 | +You can also define your own cache key for complex or custom types: |
| 133 | + |
| 134 | +```kotlin |
| 135 | +class MyKey(name: String): Key<MyType>(name) { |
| 136 | + override fun isTypeOf(valueClass: KClass<*>) = valueClass == MyType::class |
| 137 | + override suspend fun getFromStore(store: Store): Flow<MyType?> = ... |
| 138 | + override suspend fun saveToStore(item: MyType, store: Store) = ... |
| 139 | +} |
| 140 | +``` |
0 commit comments