Skip to content

Commit

Permalink
Caching questionnaires and their SM
Browse files Browse the repository at this point in the history
  • Loading branch information
aurangzaibumer committed Aug 23, 2024
1 parent b55d720 commit 8129afd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ constructor(
}
source.setParameter(Task.SP_PERIOD, period)
source.setParameter(ActivityDefinition.SP_VERSION, IntegerType(index))

//need to cache these SM too
val structureMap = fhirEngine.get<StructureMap>(IdType(action.transform).idPart)
structureMapUtilities.transform(
transformSupportServices.simpleWorkerContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.smartregister.fhircore.quest.ui.questionnaire

import androidx.collection.LruCache
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.hl7.fhir.r4.model.Resource

object ContentCache {
private val maxMemory: Int = (Runtime.getRuntime().maxMemory() / 1024).toInt()
private val cacheSize: Int = maxMemory / 8
private val cache = LruCache<String, Resource>(cacheSize)

suspend fun saveResource(resourceId: String, resource: Resource) =
withContext(Dispatchers.IO) { cache.put("${resource::class.simpleName}/$resourceId", resource) }

fun getResource(resourceId: String) = cache[resourceId]

suspend fun invalidate() = withContext(Dispatchers.IO) { cache.evictAll() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemComp
import org.hl7.fhir.r4.model.RelatedPerson
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.StructureMap
import org.smartregister.fhircore.engine.BuildConfig
import org.smartregister.fhircore.engine.configuration.ConfigType
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
Expand Down Expand Up @@ -146,7 +147,18 @@ constructor(
questionnaireConfig: QuestionnaireConfig,
): Questionnaire? {
if (questionnaireConfig.id.isEmpty() || questionnaireConfig.id.isBlank()) return null
return defaultRepository.loadResource<Questionnaire>(questionnaireConfig.id)
var questionnaire = ContentCache.getResource(ResourceType.Questionnaire.name + "/" + questionnaireConfig.id)?.copy()
if (questionnaire == null) {
questionnaire = defaultRepository.loadResource<Questionnaire>(questionnaireConfig.id)?.also { ques ->
ContentCache.saveResource(
questionnaireConfig.id,
ques.copy(),
)
}
}


return questionnaire as Questionnaire
}

/**
Expand Down Expand Up @@ -631,7 +643,7 @@ constructor(
transformSupportServices = transformSupportServices,
structureMapProvider = { structureMapUrl: String?, _: IWorkerContext ->
structureMapUrl?.substringAfterLast("/")?.let {
defaultRepository.loadResource(it)
fetchStructureMap(it)
}
},
),
Expand Down Expand Up @@ -661,6 +673,20 @@ constructor(
}
.getOrDefault(Bundle())


private suspend fun fetchStructureMap(structureMapUrl: String?): StructureMap? {
var structureMap: Resource? = null
structureMapUrl?.substringAfterLast("/")?.run {
structureMap = ContentCache.getResource(ResourceType.StructureMap.name + "/" + this) ?.let {
defaultRepository.loadResource<StructureMap>(this)?.also {
it.let { ContentCache.saveResource(this, it) }
}
}
}
return structureMap as StructureMap?
}


/**
* This function saves [QuestionnaireResponse] as draft if any of the [QuestionnaireResponse.item]
* has an answer.
Expand Down

0 comments on commit 8129afd

Please sign in to comment.