-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/issue/12967-retry-pagination-error' into iss…
…ue/13061-integrate-api-change
- Loading branch information
Showing
5 changed files
with
334 additions
and
581 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...main/kotlin/com/woocommerce/android/ui/woopos/home/items/variations/VariationsLRUCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.woocommerce.android.ui.woopos.home.items.variations | ||
|
||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
|
||
class VariationsLRUCache<K, V>(private val maxSize: Int) { | ||
|
||
companion object { | ||
private const val LOAD_FACTOR = 0.75f | ||
} | ||
private val cache = object : LinkedHashMap<K, V>(maxSize, LOAD_FACTOR, true) { | ||
override fun removeEldestEntry(eldest: Map.Entry<K, V>): Boolean { | ||
return size > maxSize | ||
} | ||
} | ||
|
||
private val mutex = Mutex() | ||
|
||
suspend fun get(key: K): V? { | ||
return mutex.withLock { | ||
cache[key] | ||
} | ||
} | ||
|
||
suspend fun put(key: K, value: V) { | ||
mutex.withLock { | ||
cache[key] = value | ||
} | ||
} | ||
|
||
suspend fun remove(key: K) { | ||
mutex.withLock { | ||
cache.remove(key) | ||
} | ||
} | ||
|
||
suspend fun clear() { | ||
mutex.withLock { | ||
cache.clear() | ||
} | ||
} | ||
|
||
suspend fun containsKey(key: K): Boolean { | ||
return mutex.withLock { | ||
cache.containsKey(key) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.