-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Caching questionnaires and their SM (#3461)
* Caching questionnaires and their SM * spotless ran * spotless ran * updated tests * WIP tests updated * resolved feedback * spotess ran * fixed imports issue * fixed imports issue * updated tests * Feedback resolved * spotless ran * Refactor Cache Content + Fix build * Refactor ContentCache to be used through DefaultRepository * Fix failing `FhirCarePlanGeneratorTest` tests * Use mutex to invalidate cache * Fix broken test in RegisterScreenTest * [wip] Log quest tests ran in ci --------- Co-authored-by: Martin Ndegwa <[email protected]> Co-authored-by: Benjamin Mwalimu <[email protected]> Co-authored-by: L≡ZRS <[email protected]> Co-authored-by: Peter Lubell-Doughtie <[email protected]>
- Loading branch information
1 parent
e8020c5
commit 5a9981e
Showing
14 changed files
with
306 additions
and
16 deletions.
There are no files selected for viewing
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
android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/ContentCache.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 2021-2024 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.smartregister.fhircore.engine.data.local | ||
|
||
import androidx.collection.LruCache | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import kotlinx.coroutines.withContext | ||
import org.hl7.fhir.r4.model.Resource | ||
import org.hl7.fhir.r4.model.ResourceType | ||
import org.smartregister.fhircore.engine.util.DispatcherProvider | ||
|
||
@Singleton | ||
class ContentCache @Inject constructor(private val dispatcherProvider: DispatcherProvider) { | ||
private val maxMemory: Int = (Runtime.getRuntime().maxMemory() / 1024).toInt() | ||
private val cacheSize: Int = maxMemory / 8 | ||
private val cache = LruCache<String, Resource>(cacheSize) | ||
private val mutex = Mutex() | ||
|
||
suspend fun <T : Resource> saveResource(resource: T): T { | ||
val key = "${resource.resourceType.name}/${resource.idPart}" | ||
return withContext(dispatcherProvider.io()) { | ||
mutex.withLock { cache.put(key, resource.copy()) } | ||
@Suppress("UNCHECKED_CAST") | ||
getResource(resource.resourceType, resource.idPart)!! as T | ||
} | ||
} | ||
|
||
fun getResource(type: ResourceType, id: String) = cache["$type/$id"]?.copy() | ||
|
||
suspend fun invalidate() = | ||
withContext(dispatcherProvider.io()) { mutex.withLock { cache.evictAll() } } | ||
} |
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
27 changes: 27 additions & 0 deletions
27
.../src/main/java/org/smartregister/fhircore/engine/data/local/DefaultRepositoryComponent.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,27 @@ | ||
/* | ||
* Copyright 2021-2024 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.smartregister.fhircore.engine.data.local | ||
|
||
import dagger.Component | ||
import javax.inject.Singleton | ||
import org.smartregister.fhircore.engine.di.DispatcherModule | ||
|
||
@Singleton | ||
@Component(modules = [DispatcherModule::class]) | ||
interface DefaultRepositoryComponent { | ||
fun inject(defaultRepository: DefaultRepository) | ||
} |
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
84 changes: 84 additions & 0 deletions
84
...oid/engine/src/test/java/org/smartregister/fhircore/engine/data/local/ContentCacheTest.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,84 @@ | ||
/* | ||
* Copyright 2021-2024 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.smartregister.fhircore.engine.data.local | ||
|
||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.runTest | ||
import org.hl7.fhir.r4.model.Questionnaire | ||
import org.hl7.fhir.r4.model.Resource | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Assert.assertNull | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.smartregister.fhircore.engine.robolectric.RobolectricTest | ||
import org.smartregister.fhircore.engine.rule.CoroutineTestRule | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
@HiltAndroidTest | ||
class ContentCacheTest : RobolectricTest() { | ||
|
||
@get:Rule(order = 0) val hiltRule = HiltAndroidRule(this) | ||
|
||
@get:Rule(order = 1) val coroutineTestRule = CoroutineTestRule() | ||
|
||
@Inject lateinit var contentCache: ContentCache | ||
|
||
private val resourceId = "123" | ||
private val mockResource: Resource = Questionnaire().apply { id = resourceId } | ||
|
||
@Before | ||
fun setUp() { | ||
hiltRule.inject() | ||
} | ||
|
||
@Test | ||
fun `saveResource should store resource in cache`() = runTest { | ||
contentCache.saveResource(mockResource) | ||
|
||
val cachedResource = contentCache.getResource(mockResource.resourceType, mockResource.idPart) | ||
assertNotNull(cachedResource) | ||
assertEquals(mockResource.idPart, cachedResource?.idPart) | ||
} | ||
|
||
@Test | ||
fun `getResource should return the correct resource from cache`() = runTest { | ||
contentCache.saveResource(mockResource) | ||
|
||
val result = contentCache.getResource(mockResource.resourceType, mockResource.idPart) | ||
assertEquals(mockResource.idPart, result?.idPart) | ||
} | ||
|
||
@Test | ||
fun `getResource should return null if resource does not exist`() = runTest { | ||
val result = contentCache.getResource(mockResource.resourceType, "non_existing_id") | ||
assertNull(result) | ||
} | ||
|
||
@Test | ||
fun `invalidate should clear all resources from cache`() = runTest { | ||
contentCache.saveResource(mockResource) | ||
contentCache.invalidate() | ||
|
||
val result = contentCache.getResource(mockResource.resourceType, mockResource.idPart) | ||
assertNull(result) | ||
} | ||
} |
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
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
Oops, something went wrong.