Skip to content

Commit dd83d70

Browse files
committed
Merge remote-tracking branch 'origin/caching_resources' into experiment-perf-branch
2 parents b609d96 + ae166a4 commit dd83d70

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

android/engine/src/main/java/org/smartregister/fhircore/engine/task/FhirCarePlanGenerator.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ constructor(
214214
}
215215
source.setParameter(Task.SP_PERIOD, period)
216216
source.setParameter(ActivityDefinition.SP_VERSION, IntegerType(index))
217-
217+
// need to cache these SM too
218218
val structureMap = fhirEngine.get<StructureMap>(IdType(action.transform).idPart)
219219
structureMapUtilities.transform(
220220
transformSupportServices.simpleWorkerContext,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2021-2024 Ona Systems, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.smartregister.fhircore.quest.ui.questionnaire
18+
19+
import androidx.collection.LruCache
20+
import kotlinx.coroutines.Dispatchers
21+
import kotlinx.coroutines.withContext
22+
import org.hl7.fhir.r4.model.Resource
23+
import timber.log.Timber
24+
25+
object ContentCache {
26+
private val maxMemory: Int = (Runtime.getRuntime().maxMemory() / 1024).toInt()
27+
private val cacheSize: Int = maxMemory / 8
28+
private val cache = LruCache<String, Resource>(cacheSize)
29+
30+
suspend fun saveResource(resourceId: String, resource: Resource) =
31+
withContext(Dispatchers.IO) {
32+
cache.put("${resource::class.simpleName}/$resourceId", resource)
33+
Timber.i("ContentCache:saveResource: $resourceId")
34+
}
35+
36+
fun getResource(resourceId: String): Resource? {
37+
return cache[resourceId]?.also { Timber.i("ContentCache:getResource: $resourceId") }
38+
}
39+
40+
suspend fun invalidate() = withContext(Dispatchers.IO) { cache.evictAll() }
41+
}

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

+24-3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemComp
6060
import org.hl7.fhir.r4.model.RelatedPerson
6161
import org.hl7.fhir.r4.model.Resource
6262
import org.hl7.fhir.r4.model.ResourceType
63+
import org.hl7.fhir.r4.model.StructureMap
6364
import org.smartregister.fhircore.engine.BuildConfig
6465
import org.smartregister.fhircore.engine.configuration.ConfigType
6566
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
@@ -146,7 +147,20 @@ constructor(
146147
questionnaireConfig: QuestionnaireConfig,
147148
): Questionnaire? {
148149
if (questionnaireConfig.id.isEmpty() || questionnaireConfig.id.isBlank()) return null
149-
return defaultRepository.loadResource<Questionnaire>(questionnaireConfig.id)
150+
var questionnaire =
151+
ContentCache.getResource(ResourceType.Questionnaire.name + "/" + questionnaireConfig.id)
152+
?.copy()
153+
if (questionnaire == null) {
154+
questionnaire =
155+
defaultRepository.loadResource<Questionnaire>(questionnaireConfig.id)?.also { ques ->
156+
ContentCache.saveResource(
157+
questionnaireConfig.id,
158+
ques.copy(),
159+
)
160+
}
161+
}
162+
163+
return questionnaire as Questionnaire
150164
}
151165

152166
/**
@@ -634,8 +648,15 @@ constructor(
634648
StructureMapExtractionContext(
635649
transformSupportServices = transformSupportServices,
636650
structureMapProvider = { structureMapUrl: String?, _: IWorkerContext ->
637-
structureMapUrl?.substringAfterLast("/")?.let {
638-
defaultRepository.loadResource(it)
651+
structureMapUrl?.substringAfterLast("/")?.let { smID ->
652+
ContentCache.getResource(ResourceType.StructureMap.name + "/" + smID)?.let {
653+
it as StructureMap
654+
}
655+
?: run {
656+
defaultRepository.loadResource<StructureMap>(smID)?.also {
657+
ContentCache.saveResource(smID, it)
658+
}
659+
}
639660
}
640661
},
641662
),

0 commit comments

Comments
 (0)