Skip to content

Commit

Permalink
[ci skip] update docs with new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kotlitecture committed Jun 24, 2024
1 parent bc8f109 commit 8f01238
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ The generated project will include a similar table in its README.MD file, but wi
| Dataflow | Facade Config API | [Link](docs/Dataflow/Facade%20Config%20API/overview.md) | - | [Link](docs/Dataflow/Facade%20Config%20API/usage.md) |
| Dataflow | SQLDelight | [Link](docs/Dataflow/SQLDelight/overview.md) | - | [Link](docs/Dataflow/SQLDelight/usage.md) |
| Dataflow | Multiplatform Settings | [Link](docs/Dataflow/Multiplatform%20Settings/overview.md) | - | [Link](docs/Dataflow/Multiplatform%20Settings/usage.md) |
| Dataflow | Basic In-Memory Cache API | [Link](docs/Dataflow/Basic%20In-Memory%20Cache%20API/overview.md) | - | [Link](docs/Dataflow/Basic%20In-Memory%20Cache%20API/usage.md) |
| Dataflow | Ktor Client | [Link](docs/Dataflow/Ktor%20Client/overview.md) | - | [Link](docs/Dataflow/Ktor%20Client/usage.md) |
| Dataflow | Cash App Paging Library | [Link](docs/Dataflow/Cash%20App%20Paging%20Library/overview.md) | - | [Link](docs/Dataflow/Cash%20App%20Paging%20Library/usage.md) |
| Userflow | Rail Navigation | [Link](docs/Userflow/Rail%20Navigation/overview.md) | - | [Link](docs/Userflow/Rail%20Navigation/usage.md) |
Expand Down
3 changes: 3 additions & 0 deletions docs/Dataflow/Basic In-Memory Cache API/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# About

Thread-safe API for storing and retrieving any in-memory data. Can be utilized as an L1 Cache when managing HTTP requests, offering an efficient means to present data without delays, but with the ability to update based on expiration and other conditions.
55 changes: 55 additions & 0 deletions docs/Dataflow/Basic In-Memory Cache API/usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Usage

## Overview

The API can be accessed through:
- `shared.data.datasource.cache.CacheSource` - facade interface at the core module level.
- `app.datasource.cache.AppCacheSource` - decorator class at the app level.

The difference is that the class serves as a **decorator** and can provide extra methods without impacting facade implementations.

Facade **CacheSource** provides the following methods:

- `getState(key: CacheKey<T>, valueProvider: suspend () -> T?): CacheState<T>` - Retrieves the state of a cache entry associated with the specified key.
- `get(key: CacheKey<T>, valueProvider: suspend () -> T?): T?` - Retrieves the value associated with the specified key from the cache.
- `invalidate(type: Class<K>)` - Invalidates all cache entries associated with the specified key type.
- `invalidate(key: K)` - Invalidates the cache entry associated with the specified key.
- `remove(type: Class<K>)` - Removes all cache entries associated with the specified key type.
- `remove(key: K)` - Removes the cache entry associated with the specified key.
- `put(key: CacheKey<T>, value: T)` - Associates the specified value with the specified key in the cache.
- `clear()` - Clears all entries from the cache.

## Example

Both the **facade** and **decorator** are pre-configured via dependency injection (DI) as singletons in `app.di.datasource.ProvidesCacheSource`.

To start using, just inject it to your DI managed class.

```kotlin
class BasicCacheViewModel(
private val cacheSource: CacheSource = get()
) : BaseViewModel() {

val cacheStore = StoreObject<String>()

override fun doBind() {
launchAsync {
val cacheKey = SimpleCacheKey()
val cacheState = cacheSource.getState(cacheKey, ::getDateAsFormattedString)
cacheState.changes().collectLatest(cacheStore::set)
}
}

private fun getDateAsFormattedString(): String {
val time = Clock.System.now()
return time.format(DateTimeComponents.Format {
byUnicodePattern("yyyy-MM-dd HH:mm:ss")
})
}

private data class SimpleCacheKey(
override val ttl: Long = CacheKey.TTL_10_SECONDS
) : CacheKey<String>

}
```

0 comments on commit 8f01238

Please sign in to comment.