Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 5e9dfa36 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #3520 from k9mail/fix_backend_manager_cache

Only reuse Backend instances when server settings haven't changed
parents 92e11f0f c91b482d
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -5,14 +5,25 @@ 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) {
@@ -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)
}