reverting back to using an array for the circular cache, makes preloading and setting the value simpler

- adds unit tests to show it working
This commit is contained in:
Adam Brown 2021-10-14 13:12:16 +01:00
parent 0f07629547
commit fc793c442b
3 changed files with 80 additions and 4 deletions

View file

@ -20,15 +20,19 @@ package im.vector.app.features.notifications
* A FIFO circular buffer of T * A FIFO circular buffer of T
* This class is not thread safe * This class is not thread safe
*/ */
class CircularCache<T>(cacheSize: Int) { class CircularCache<T : Any>(cacheSize: Int, factory: (Int) -> Array<T?>) {
private val cache = ArrayList<T>(initialCapacity = cacheSize) companion object {
inline fun <reified T : Any> create(cacheSize: Int) = CircularCache(cacheSize) { Array<T?>(cacheSize) { null } }
}
private val cache = factory(cacheSize)
private var writeIndex = 0 private var writeIndex = 0
fun contains(key: T): Boolean = cache.contains(key) fun contains(key: T): Boolean = cache.contains(key)
fun put(key: T) { fun put(key: T) {
if (writeIndex == cache.size - 1) { if (writeIndex == cache.size) {
writeIndex = 0 writeIndex = 0
} }
cache[writeIndex] = key cache[writeIndex] = key

View file

@ -89,7 +89,7 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
* Acts as a notification debouncer to stop already dismissed push notifications from * Acts as a notification debouncer to stop already dismissed push notifications from
* displaying again when the /sync response is delayed. * displaying again when the /sync response is delayed.
*/ */
private val seenEventIds = CircularCache<String>(cacheSize = 25) private val seenEventIds = CircularCache.create<String>(cacheSize = 25)
/** /**
Should be called as soon as a new event is ready to be displayed. Should be called as soon as a new event is ready to be displayed.

View file

@ -0,0 +1,72 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.notifications
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
class CircularCacheTest {
@Test
fun `when putting more than cache size then cache is limited to cache size`() {
val (cache, internalData) = createIntCache(cacheSize = 3)
cache.putInOrder(1, 1, 1, 1, 1, 1)
internalData shouldBeEqualTo arrayOf(1, 1, 1)
}
@Test
fun `when putting more than cache then acts as FIFO`() {
val (cache, internalData) = createIntCache(cacheSize = 3)
cache.putInOrder(1, 2, 3, 4)
internalData shouldBeEqualTo arrayOf(4, 2, 3)
}
@Test
fun `given empty cache when checking if contains key then is false`() {
val (cache, _) = createIntCache(cacheSize = 3)
val result = cache.contains(1)
result shouldBeEqualTo false
}
@Test
fun `given cached key when checking if contains key then is true`() {
val (cache, _) = createIntCache(cacheSize = 3)
cache.put(1)
val result = cache.contains(1)
result shouldBeEqualTo true
}
private fun createIntCache(cacheSize: Int): Pair<CircularCache<Int>, Array<Int?>> {
var internalData: Array<Int?>? = null
val factory: (Int) -> Array<Int?> = {
Array<Int?>(it) { null }.also { array -> internalData = array }
}
return CircularCache(cacheSize, factory) to internalData!!
}
private fun CircularCache<Int>.putInOrder(vararg keys: Int) {
keys.forEach { put(it) }
}
}