TP-00000: Drive DG cache config from BE (#7659)

This commit is contained in:
Siddiboina Susai
2023-08-29 16:27:34 +05:30
committed by GitHub
parent 1a878986f9
commit 8f97ededfd
5 changed files with 45 additions and 8 deletions

View File

@@ -17,7 +17,6 @@ import com.navi.common.utils.TemporaryStorageHelper
import com.navi.common.viewmodel.BaseVM
import com.navi.gold.repo.DigitalGoldHomeRepo
import com.navi.naviwidgets.models.response.HomeProductWidget
import com.naviapp.common.navigator.NaviDeepLinkNavigator
import com.naviapp.home.dashboard.models.response.InvestmentsResponse
import com.naviapp.home.respository.GlobalRepo
import com.naviapp.home.respository.InvestmentsRepository
@@ -81,11 +80,13 @@ class InvestmentsVM @Inject constructor(
if (!TemporaryStorageHelper.SOFT_REF_CACHE.contains(GOLD)) {
coroutineScope.launch {
val response = goldHomeRepo.fetchDigitalGoldHomeDetails(emptyMap())
if (response.error.isNull() && response.errors.isNullOrEmpty()) {
if (response.error.isNull() && response.errors.isNullOrEmpty() && response.data != null) {
TemporaryStorageHelper.SOFT_REF_CACHE.add(
key = GOLD,
value = response.data as Any,
maxConsumptions = 3
maxTtl = response.data?.extraData?.cacheConfig?.ttl ?: 0,
maxConsumptions = response.data?.extraData?.cacheConfig?.maxConsumptions
?: 0
)
} else {
//no-op as this api call's response is not critical

View File

@@ -70,7 +70,14 @@ data class ExtraDataDetails(
@SerializedName("uiAttributes") val uiAttributes: UiAttributes? = null,
@SerializedName("launchToastInfo") val launchToastInfo: ToastInfo? = null,
@SerializedName("launchFullScreenDialog") val launchFullScreenDialog: WidgetResponse? = null,
@SerializedName("onHiddenUiTronActionList") val onHiddenUiTronActionList: List<String>? = null
@SerializedName("onHiddenUiTronActionList") val onHiddenUiTronActionList: List<String>? = null,
@SerializedName("cacheConfig") val cacheConfig: CacheConfig? = null
) : Parcelable
@Parcelize
data class CacheConfig(
val ttl: Long? = 0,
val maxConsumptions : Int? = 0
) : Parcelable
@Parcelize

View File

@@ -39,6 +39,11 @@ class SoftRefLruCache<K, V>(private val maxSize: Int = DEFAULT_CACHE_SIZE) {
maxTtl: Long = DEFAULT_TTL,
maxConsumptions: Int = DEFAULT_MAX_CONSUMPTIONS
) {
//if these values are less than zero, there is no point in storing them
if(maxTtl <= 0 || maxConsumptions <= 0)
return
// Create a CacheEntry with a SoftReference to the value
CacheEntry(
value = SoftReference(value),

View File

@@ -78,4 +78,26 @@ class SoftRefLruCacheTest {
assertNull(cache.get("key1"))
assertNull(cache.get("key2"))
}
@Test
fun testMaxTtlAndMaxConsumptions() {
val cache = SoftRefLruCache<Int, String>()
cache.add(1, "Value 1", maxTtl = 10000, maxConsumptions = 3)
assertTrue(cache.contains(1))
assertEquals("Value 1", cache.get(1))
}
@Test
fun testNegativeMaxTtlAndNegativeMaxConsumptions() {
val cache = SoftRefLruCache<Int, String>()
cache.add(1, "Value 1", maxTtl = 0, maxConsumptions = 3)
cache.add(2, "Value 2", maxTtl = 10000, maxConsumptions = 0)
// Entries with zero maxTtl or maxConsumptions should not be stored
assertFalse(cache.contains(1))
assertFalse(cache.contains(2))
}
}

View File

@@ -9,11 +9,12 @@ package com.navi.gold.viewmodels
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.androidx.lifecycle.SingleLiveEvent
import com.navi.base.utils.isNull
import com.navi.base.utils.orFalse
import com.androidx.lifecycle.SingleLiveEvent
import com.navi.common.model.common.WidgetBottomSheetData
import com.navi.common.model.common.WidgetResponse
import com.navi.common.utils.Constants.GOLD
import com.navi.common.utils.TemporaryStorageHelper
import com.navi.common.viewmodel.BaseVM
import com.navi.gold.model.AmountDataRequest
@@ -23,7 +24,6 @@ import com.navi.gold.repo.DigitalGoldHomeRepo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
import com.navi.common.utils.Constants.GOLD
@HiltViewModel
class DigitalGoldHomeVM @Inject constructor(private val goldHomeRepo: DigitalGoldHomeRepo) :
@@ -74,14 +74,16 @@ class DigitalGoldHomeVM @Inject constructor(private val goldHomeRepo: DigitalGol
val response = goldHomeRepo.fetchDigitalGoldHomeDetails(params)
if (response.error.isNull() && response.errors.isNullOrEmpty()) {
if (response.error.isNull() && response.errors.isNullOrEmpty() && response.data != null) {
if (params.isEmpty()) {
TemporaryStorageHelper.SOFT_REF_CACHE.add(
key = GOLD,
value = response.data as Any,
maxConsumptions = 3
maxTtl = response.data?.extraData?.cacheConfig?.ttl ?: 0,
maxConsumptions = response.data?.extraData?.cacheConfig?.maxConsumptions
?: 0
)
}