From 9d598bad692767d0631a463f779eb9b7be30cb37 Mon Sep 17 00:00:00 2001 From: NovaFox161 Date: Sat, 9 Sep 2023 12:46:37 -0500 Subject: [PATCH] Fix in-memory cache implementation --- .../dreamexposure/discal/core/cache/JdkCacheRepository.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/kotlin/org/dreamexposure/discal/core/cache/JdkCacheRepository.kt b/core/src/main/kotlin/org/dreamexposure/discal/core/cache/JdkCacheRepository.kt index 944c5dc3b..0ad4c84e8 100644 --- a/core/src/main/kotlin/org/dreamexposure/discal/core/cache/JdkCacheRepository.kt +++ b/core/src/main/kotlin/org/dreamexposure/discal/core/cache/JdkCacheRepository.kt @@ -16,12 +16,12 @@ class JdkCacheRepository(override val ttl: Duration) : CacheReposito } override suspend fun put(key: K, value: V) { - cache[key] = Pair(Instant.now(), value) + cache[key] = Pair(Instant.now().plus(ttl), value) } override suspend fun get(key: K): V? { val cached = cache[key] ?: return null - if (Instant.now().isAfter(cached.first)) { + if (cached.first.isExpiredTtl()) { evict(key) return null } @@ -41,6 +41,6 @@ class JdkCacheRepository(override val ttl: Duration) : CacheReposito } private fun evictOld() { - cache.forEach { (key, pair) -> if (Duration.between(pair.first, Instant.now()) >= ttl) cache.remove(key) } + cache.forEach { (key, pair) -> if (pair.first.isExpiredTtl()) cache.remove(key) } } }