Skip to content

Commit

Permalink
feat: MissingGuard support Ttl for ClientSideCache (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Mar 18, 2024
1 parent 13c766b commit c6eb3fd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import me.ahoo.cache.Cache
interface ClientSideCache<V> : Cache<String, V> {
override fun get(key: String): V? {
val cacheValue = getCache(key) ?: return null
if (cacheValue.isMissingGuard) {
return null
}
if (cacheValue.isExpired) {
evict(key)
return null
}
if (cacheValue.isMissingGuard) {
return null
}
return cacheValue.value
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
package me.ahoo.cache.client

import me.ahoo.cache.CacheValue
import me.ahoo.cache.CacheValue.Companion.missingGuard
import me.ahoo.cache.CacheValue.Companion.missingGuardValue
import java.util.concurrent.ConcurrentHashMap

/**
Expand All @@ -34,10 +32,6 @@ class MapClientSideCache<V>(
if (value.isExpired) {
return
}
if (missingGuardValue<Any>() == value.value) {
cacheMap[key] = missingGuard()
return
}
cacheMap[key] = value
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import me.ahoo.cache.CacheValue
import me.ahoo.cache.TtlAt
import me.ahoo.cache.util.CacheSecondClock
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers
import org.hamcrest.Matchers.*
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -56,7 +56,7 @@ abstract class CodecExecutorSpec<V> {
val value = CacheValue.forever(createCacheValue())
codecExecutor.executeAndEncode(key, value)
val actual = codecExecutor.executeAndDecode(key, TtlAt.FOREVER)
assertThat(actual, Matchers.equalTo(value))
assertThat(actual, equalTo(value))
}

@Test
Expand All @@ -66,7 +66,8 @@ abstract class CodecExecutorSpec<V> {
val value = CacheValue(createCacheValue(), ttlAt)
codecExecutor.executeAndEncode(key, value)
val actual = codecExecutor.executeAndDecode(key, ttlAt)
assertThat(actual, Matchers.equalTo(value))
assertThat(actual.value, equalTo(value.value))
assertThat(actual.ttlAt.toDouble(), closeTo(value.ttlAt.toDouble(), 1.toDouble()))
}

@Test
Expand All @@ -75,7 +76,7 @@ abstract class CodecExecutorSpec<V> {
val value = CacheValue.missingGuard<CacheValue<V>>()
codecExecutor.executeAndEncode(key, value)
val actual = codecExecutor.executeAndDecode(key, TtlAt.FOREVER)
assertThat(actual, Matchers.equalTo(value))
assertThat(actual, equalTo(value))
}

@Test
Expand All @@ -84,6 +85,7 @@ abstract class CodecExecutorSpec<V> {
val value = CacheValue.missingGuard<CacheValue<V>>(100)
codecExecutor.executeAndEncode(key, value)
val actual = codecExecutor.executeAndDecode(key, 100)
assertThat(actual, Matchers.equalTo(value))
assertThat(actual.value, equalTo(value.value))
assertThat(actual.ttlAt.toDouble(), closeTo(value.ttlAt.toDouble(), 1.toDouble()))
}
}

0 comments on commit c6eb3fd

Please sign in to comment.