0
0
mirror of https://github.com/thunderbird/thunderbird-android.git synced 2024-09-20 04:02:14 +02:00

Merge pull request #3520 from k9mail/fix_backend_manager_cache

Only reuse Backend instances when server settings haven't changed
This commit is contained in:
cketti 2018-07-22 13:33:15 +02:00 committed by GitHub
commit 5e9dfa3656
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,15 +5,26 @@ import com.fsck.k9.backend.api.Backend
import com.fsck.k9.mail.ServerSettings
class BackendManager(private val backendFactories: Map<String, BackendFactory>) {
private val backendCache = mutableMapOf<String, Backend>()
private val backendCache = mutableMapOf<String, BackendContainer>()
fun getBackend(account: Account): Backend {
synchronized (backendCache) {
return backendCache[account.uuid] ?: createBackend(account).also { backendCache[account.uuid] = it }
val container = backendCache[account.uuid]
return if (container != null && isBackendStillValid(container, account)) {
container.backend
} else {
createBackend(account).also { backend ->
backendCache[account.uuid] = BackendContainer(backend, account.storeUri, account.transportUri)
}
}
}
}
private fun isBackendStillValid(container: BackendContainer, account: Account): Boolean {
return container.storeUri == account.storeUri && container.transportUri == account.transportUri
}
fun removeBackend(account: Account) {
synchronized (backendCache) {
backendCache.remove(account.uuid)
@ -70,4 +81,7 @@ class BackendManager(private val backendFactories: Map<String, BackendFactory>)
throw IllegalArgumentException("Unsupported ServerSettings type")
}
private data class BackendContainer(val backend: Backend, val storeUri: String, val transportUri: String)
}