Migrates to LruCache
This commit is contained in:
+9
-12
@@ -20,19 +20,21 @@
|
|||||||
*/
|
*/
|
||||||
package com.vitorpamplona.quartz.nip05.namecoin
|
package com.vitorpamplona.quartz.nip05.namecoin
|
||||||
|
|
||||||
|
import androidx.collection.LruCache
|
||||||
|
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
|
|
||||||
data class CachedResult(
|
data class CachedResult(
|
||||||
val result: NamecoinNostrResult?,
|
val result: NamecoinNostrResult?,
|
||||||
val timestamp: Long = System.currentTimeMillis(),
|
val timestamp: Long = TimeUtils.now(),
|
||||||
)
|
)
|
||||||
|
|
||||||
class NamecoinLookupCache(
|
class NamecoinLookupCache(
|
||||||
private val maxEntries: Int = 500,
|
private val maxEntries: Int = 500,
|
||||||
private val ttlMs: Long = 3_600_000L, // 1 hour
|
private val ttlSecs: Long = 3_600L, // 1 hour
|
||||||
) {
|
) {
|
||||||
private val cache = LinkedHashMap<String, CachedResult>(maxEntries, 0.75f, true)
|
private val cache = LruCache<String, CachedResult>(maxEntries)
|
||||||
private val mutex = Mutex()
|
private val mutex = Mutex()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,8 +46,8 @@ class NamecoinLookupCache(
|
|||||||
mutex.withLock {
|
mutex.withLock {
|
||||||
val key = cacheKey(identifier)
|
val key = cacheKey(identifier)
|
||||||
val entry = cache[key] ?: return null
|
val entry = cache[key] ?: return null
|
||||||
val age = System.currentTimeMillis() - entry.timestamp
|
val age = TimeUtils.now() - entry.timestamp
|
||||||
if (age > ttlMs) {
|
if (age > ttlSecs) {
|
||||||
cache.remove(key)
|
cache.remove(key)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@@ -57,12 +59,7 @@ class NamecoinLookupCache(
|
|||||||
result: NamecoinNostrResult?,
|
result: NamecoinNostrResult?,
|
||||||
) = mutex.withLock {
|
) = mutex.withLock {
|
||||||
val key = cacheKey(identifier)
|
val key = cacheKey(identifier)
|
||||||
if (cache.size >= maxEntries) {
|
cache.put(key, CachedResult(result))
|
||||||
// Remove eldest (LRU) entry
|
|
||||||
val eldest = cache.entries.firstOrNull()
|
|
||||||
if (eldest != null) cache.remove(eldest.key)
|
|
||||||
}
|
|
||||||
cache[key] = CachedResult(result)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun invalidate(identifier: String) =
|
suspend fun invalidate(identifier: String) =
|
||||||
@@ -72,6 +69,6 @@ class NamecoinLookupCache(
|
|||||||
|
|
||||||
suspend fun clear() =
|
suspend fun clear() =
|
||||||
mutex.withLock {
|
mutex.withLock {
|
||||||
cache.clear()
|
cache.evictAll()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user