-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added caches to business logic
- Loading branch information
Showing
8 changed files
with
166 additions
and
47 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
api/core/src/commonMain/kotlin/cz/lastaapps/api/core/domain/FlowParametrizedCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2025, Petr Laštovička as Lasta apps, All rights reserved | ||
* | ||
* This file is part of Menza. | ||
* | ||
* Menza is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Menza is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Menza. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package cz.lastaapps.api.core.domain | ||
|
||
import arrow.core.None | ||
import arrow.core.Option | ||
import arrow.core.Some | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.shareIn | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
// Now I would really love to have traits | ||
class FlowParametrizedCache<T, Param>( | ||
private val coroutineContext: CoroutineContext = Dispatchers.Default, | ||
) { | ||
private var cacheLastParam: Option<Triple<Param, Flow<T>, CoroutineScope>> = None | ||
private val cacheMutex: Mutex = Mutex() | ||
|
||
suspend operator fun invoke( | ||
params: Param, | ||
block: suspend (param: Param) -> Flow<T>, | ||
): Flow<T> = | ||
cacheMutex.withLock { | ||
when (val param = cacheLastParam) { | ||
is Some if (param.value.first == params) -> param.value.second | ||
else -> { | ||
param.getOrNull()?.third?.cancel() | ||
val scope = CoroutineScope(coroutineContext) | ||
block(params) | ||
.shareIn(scope, SharingStarted.Eagerly, replay = 1) | ||
.also { data -> | ||
cacheLastParam = Some(Triple(params, data, scope)) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
api/main/src/commonMain/kotlin/cz/lastaapps/api/main/domain/usecase/GetTodayRawDishListUC.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2025, Petr Laštovička as Lasta apps, All rights reserved | ||
* | ||
* This file is part of Menza. | ||
* | ||
* Menza is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Menza is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Menza. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package cz.lastaapps.api.main.domain.usecase | ||
|
||
import cz.lastaapps.api.core.domain.FlowParametrizedCache | ||
import cz.lastaapps.api.core.domain.model.MenzaType | ||
import cz.lastaapps.api.core.domain.model.dish.DishCategory | ||
import cz.lastaapps.api.core.domain.repo.TodayDishRepo | ||
import cz.lastaapps.api.core.domain.sync.getData | ||
import cz.lastaapps.core.domain.UCContext | ||
import cz.lastaapps.core.domain.UseCase | ||
import kotlinx.collections.immutable.ImmutableList | ||
import kotlinx.coroutines.flow.Flow | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.get | ||
import org.koin.core.parameter.parametersOf | ||
|
||
internal class GetTodayRawDishListUC internal constructor( | ||
context: UCContext, | ||
private val getRequestParamsUC: GetRequestParamsUC, | ||
) : UseCase(context), | ||
KoinComponent { | ||
private val cache = FlowParametrizedCache<ImmutableList<DishCategory>, MenzaType>() | ||
|
||
suspend operator fun invoke(menza: MenzaType): Flow<ImmutableList<DishCategory>> = | ||
launch { | ||
cache(menza) { menza -> | ||
get<TodayDishRepo> { parametersOf(menza) } | ||
.getData(getRequestParamsUC()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters