Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cached value loading strategy #7

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ makeevrserg.java.ktarget=17
# Project
makeevrserg.project.name=KStorage
makeevrserg.project.group=ru.astrainteractive.klibs
makeevrserg.project.version.string=2.1.0
makeevrserg.project.version.string=2.2.0
makeevrserg.project.description=Kotlin wrapper for key-value storage libraries
makeevrserg.project.developers=makeevrserg|Makeev Roman|makeevrserg@gmail.com
makeevrserg.project.url=https://github.com/makeevrserg/klibs.kstorage/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ import ru.astrainteractive.klibs.kstorage.api.provider.ValueSaver

/**
* This [DefaultMutableKrate] can be used with delegation
*
* @param requireInstantLoading - if true, will load value into [cachedValue] directly from [loader].
* If false will put [factory] value into [cachedValue]
*/
class DefaultMutableKrate<T>(
private val factory: ValueFactory<T>,
private val saver: ValueSaver<T> = ValueSaver.Empty(),
private val loader: ValueLoader<T>,
private val requireInstantLoading: Boolean = true
) : MutableKrate<T> {
private var _cachedValue: T = loader.loadAndGet() ?: factory.create()

private var _cachedValue: T = when {
requireInstantLoading -> loader.loadAndGet() ?: factory.create()
else -> factory.create()
}

override val cachedValue: T
get() = _cachedValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ import ru.astrainteractive.klibs.kstorage.api.provider.ValueSaver

/**
* This [DefaultStateFlowMutableKrate] can be used with delegation
*
* @param requireInstantLoading - if true, will load value into [cachedValue] directly from [loader].
* If false will put [factory] value into [cachedValue]
*/
class DefaultStateFlowMutableKrate<T>(
private val factory: ValueFactory<T>,
private val saver: ValueSaver<T> = ValueSaver.Empty(),
private val loader: ValueLoader<T>,
requireInstantLoading: Boolean = true
) : StateFlowMutableKrate<T> {
private val _stateFlow = MutableStateFlow(loader.loadAndGet() ?: factory.create())

private val _stateFlow = MutableStateFlow(
value = when {
requireInstantLoading -> loader.loadAndGet() ?: factory.create()
else -> factory.create()
}
)

override val cachedStateFlow: StateFlow<T> = _stateFlow.asStateFlow()

Expand Down
Loading