Skip to content

Commit

Permalink
Fix in-memory cache implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
NovaFox161 committed Sep 9, 2023
1 parent 0eed2a7 commit 9d598ba
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class JdkCacheRepository<K : Any, V>(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
}
Expand All @@ -41,6 +41,6 @@ class JdkCacheRepository<K : Any, V>(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) }
}
}

0 comments on commit 9d598ba

Please sign in to comment.