feat: adding a flow wrapper for persistent cache#12
feat: adding a flow wrapper for persistent cache#12ethan-berg-kr wants to merge 2 commits intokrogerco:alphafrom
Conversation
…o a single cached value.
| import kotlinx.coroutines.launch | ||
|
|
||
| /** | ||
| * MIT License |
There was a problem hiding this comment.
The license headers should be the very first line of each source file. If you rebase this on main, I just added some tooling that will help with checking and applying these headers.
| @@ -0,0 +1,97 @@ | |||
| package com.kroger.cache.internal | |||
There was a problem hiding this comment.
If this is meant to be public API, it shouldn't go in the internal package.
| public class CacheFlowWrapper<T>( | ||
| private val cache: SnapshotPersistentCache<T>, | ||
| private val scope: CoroutineScope, | ||
| ) { |
There was a problem hiding this comment.
The API seems like it could be improved by making this class implement MutableStateFlow directly. That would allow for an asFlow() extension on SnapshotPersistentCache:
fileCache.asFlow(scope).collect { it }
public fun <T> SnapshotPersistentCache<T>.asFlow(scope: CoroutineScope): MutableStateFlow<T> {
return CacheFlowWrapper<T>(this, scope)
}And all of the MutableStateFlow members could be implemented by delegating to the internal _cacheValueState
| /** | ||
| * A Wrapper class for a SnapshotPersistentCache that exposes changes to the cache via a flow. | ||
| * | ||
| * **Note this works best when used as a singleton |
There was a problem hiding this comment.
works best when used as a singleton
This note needs to be more specific. Does using two wrappers on a cache expose you to data correctness issues? Or is this just about performance?
| import org.junit.jupiter.api.Test | ||
|
|
||
| @OptIn(ExperimentalCoroutinesApi::class) | ||
| class CacheFlowWrapperTest { |
There was a problem hiding this comment.
Some of these tests were failing when I ran them locally 🫤
Adds a wrapper to observe changes to a cached value