|
2 | 2 | * Nextcloud - Android Client |
3 | 3 | * |
4 | 4 | * 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 |
8 | 6 | */ |
9 | 7 | package com.nextcloud.client.network |
10 | 8 |
|
11 | 9 | import com.nextcloud.client.core.ClockImpl |
| 10 | +import java.util.concurrent.ConcurrentHashMap |
12 | 11 | import javax.inject.Inject |
13 | 12 | import javax.inject.Singleton |
14 | 13 |
|
15 | 14 | @Singleton |
16 | 15 | class WalledCheckCache @Inject constructor() { |
17 | 16 | private val clock = ClockImpl() |
18 | 17 |
|
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>>() |
21 | 20 |
|
22 | | - @Synchronized |
23 | 21 | fun setValue(key: ConnectivityKey, isWalled: Boolean) { |
24 | 22 | walledStatusCache[key] = Pair(clock.currentTime, isWalled) |
25 | 23 | } |
26 | 24 |
|
27 | | - @Synchronized |
28 | 25 | fun clear(key: ConnectivityKey) { |
29 | 26 | walledStatusCache.remove(key) |
30 | 27 | } |
31 | 28 |
|
32 | | - @Synchronized |
33 | 29 | fun getValue(key: ConnectivityKey): Boolean? { |
34 | 30 | val entry = walledStatusCache[key] ?: return null |
35 | 31 | val isExpired = (clock.currentTime - entry.first) >= CACHE_TIME_MS |
36 | 32 | return if (isExpired) null else entry.second |
37 | 33 | } |
38 | 34 |
|
39 | | - @Synchronized |
40 | 35 | fun putConnectivityValue(key: ConnectivityKey, connectivity: Connectivity) { |
41 | 36 | connectivityCache[key] = connectivity |
42 | 37 | } |
43 | 38 |
|
44 | | - @Synchronized |
45 | | - fun getConnectivity(key: ConnectivityKey): Connectivity? = connectivityCache[key] |
46 | | - |
47 | 39 | companion object { |
48 | 40 | private const val CACHE_TIME_MS = 10 * 60 * 1000 |
49 | 41 | } |
|
0 commit comments