Skip to content

Commit

Permalink
Updated the cache key for the cache used in site factory. (#4911)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/1200581511062568/1208075248978457/f

### Description
Updated the cache key for the cache used in site factory.

### Steps to test this PR
See test case (Issue section) in the description of
https://app.asana.com/0/1200581511062568/1208075248978457/f

### NO UI changes
  • Loading branch information
anikiki authored Aug 20, 2024
1 parent a511b38 commit ef7c8b3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ class BrowserTabViewModel @Inject constructor(
buildingSiteFactoryJob?.cancel()
}
val externalLaunch = stillExternal ?: false
site = siteFactory.buildSite(url, title, httpsUpgraded, externalLaunch)
site = siteFactory.buildSite(url, tabId, title, httpsUpgraded, externalLaunch)
onSiteChanged()
buildingSiteFactoryJob = viewModelScope.launch {
site?.let {
Expand Down Expand Up @@ -2976,7 +2976,7 @@ class BrowserTabViewModel @Inject constructor(
// when navigating from one page to another it can happen that errors are recorded before pageChanged etc. are
// called triggering a buildSite.
if (url != site?.url) {
site = siteFactory.buildSite(url)
site = siteFactory.buildSite(url = url, tabId = tabId)
}
Timber.d("recordErrorCode $error in ${site?.url}")
site?.onErrorDetected(error)
Expand All @@ -2989,7 +2989,7 @@ class BrowserTabViewModel @Inject constructor(
// when navigating from one page to another it can happen that errors are recorded before pageChanged etc. are
// called triggering a buildSite.
if (url != site?.url) {
site = siteFactory.buildSite(url)
site = siteFactory.buildSite(url = url, tabId = tabId)
}
Timber.d("recordHttpErrorCode $statusCode in ${site?.url}")
updateHttpErrorCount(statusCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ class SiteFactoryImpl @Inject constructor(
@AnyThread
override fun buildSite(
url: String,
tabId: String,
title: String?,
httpUpgraded: Boolean,
externalLaunch: Boolean,
): Site {
val cachedSite = siteCache.get(url)
val cacheKey = "$tabId|$url"
val cachedSite = siteCache.get(cacheKey)
return if (cachedSite == null) {
SiteMonitor(
url,
Expand All @@ -73,7 +75,7 @@ class SiteFactoryImpl @Inject constructor(
dispatcherProvider,
RealBrokenSiteContext(duckDuckGoUrlDetector),
).also {
siteCache.put(url, it)
siteCache.put(cacheKey, it)
}
} else {
cachedSite.upgradedHttps = httpUpgraded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class TabDataRepository @Inject constructor(
skipHome: Boolean,
): String = withContext(dispatchers.io()) {
val tabId = generateTabId()
add(tabId, buildSiteData(url), skipHome = skipHome, isDefaultTab = false)
add(tabId, buildSiteData(url, tabId), skipHome = skipHome, isDefaultTab = false)
return@withContext tabId
}

Expand All @@ -99,7 +99,7 @@ class TabDataRepository @Inject constructor(

add(
tabId = tabId,
data = buildSiteData(url),
data = buildSiteData(url, tabId),
skipHome = skipHome,
isDefaultTab = false,
sourceTabId = sourceTabId,
Expand All @@ -113,7 +113,7 @@ class TabDataRepository @Inject constructor(

add(
tabId = tabId,
data = buildSiteData(null),
data = buildSiteData(url = null, tabId = tabId),
skipHome = false,
isDefaultTab = true,
)
Expand All @@ -123,10 +123,10 @@ class TabDataRepository @Inject constructor(

private fun generateTabId() = UUID.randomUUID().toString()

private fun buildSiteData(url: String?): MutableLiveData<Site> {
private fun buildSiteData(url: String?, tabId: String): MutableLiveData<Site> {
val data = MutableLiveData<Site>()
url?.let {
val siteMonitor = siteFactory.buildSite(it)
val siteMonitor = siteFactory.buildSite(url = it, tabId = tabId)
data.postValue(siteMonitor)
}
return data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface SiteFactory {
@AnyThread
fun buildSite(
url: String,
tabId: String,
title: String? = null,
httpUpgraded: Boolean = false,
externalLaunch: Boolean = false,
Expand Down

0 comments on commit ef7c8b3

Please sign in to comment.