Skip to content

Commit dd4c527

Browse files
committed
use concurrent hash map instead synchronized block
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 1868159 commit dd4c527

1 file changed

Lines changed: 4 additions & 12 deletions

File tree

app/src/main/java/com/nextcloud/client/network/WalledCheckCache.kt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,40 @@
22
* Nextcloud - Android Client
33
*
44
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
5-
* SPDX-FileCopyrightText: 2022 Álvaro Brey <alvaro@alvarobrey.com>
6-
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH
7-
* SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
86
*/
97
package com.nextcloud.client.network
108

119
import com.nextcloud.client.core.ClockImpl
10+
import java.util.concurrent.ConcurrentHashMap
1211
import javax.inject.Inject
1312
import javax.inject.Singleton
1413

1514
@Singleton
1615
class WalledCheckCache @Inject constructor() {
1716
private val clock = ClockImpl()
1817

19-
private var connectivityCache = mutableMapOf<ConnectivityKey, Connectivity>()
20-
private val walledStatusCache = mutableMapOf<ConnectivityKey, Pair<Long, Boolean>>()
18+
private val connectivityCache = ConcurrentHashMap<ConnectivityKey, Connectivity>()
19+
private val walledStatusCache = ConcurrentHashMap<ConnectivityKey, Pair<Long, Boolean>>()
2120

22-
@Synchronized
2321
fun setValue(key: ConnectivityKey, isWalled: Boolean) {
2422
walledStatusCache[key] = Pair(clock.currentTime, isWalled)
2523
}
2624

27-
@Synchronized
2825
fun clear(key: ConnectivityKey) {
2926
walledStatusCache.remove(key)
3027
}
3128

32-
@Synchronized
3329
fun getValue(key: ConnectivityKey): Boolean? {
3430
val entry = walledStatusCache[key] ?: return null
3531
val isExpired = (clock.currentTime - entry.first) >= CACHE_TIME_MS
3632
return if (isExpired) null else entry.second
3733
}
3834

39-
@Synchronized
4035
fun putConnectivityValue(key: ConnectivityKey, connectivity: Connectivity) {
4136
connectivityCache[key] = connectivity
4237
}
4338

44-
@Synchronized
45-
fun getConnectivity(key: ConnectivityKey): Connectivity? = connectivityCache[key]
46-
4739
companion object {
4840
private const val CACHE_TIME_MS = 10 * 60 * 1000
4941
}

0 commit comments

Comments
 (0)