Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BET-6062] Open up the LruNormalizedCache class #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import kotlin.reflect.KClass
*
* A common configuration is to have secondary SQL cache.
*/
class LruNormalizedCache internal constructor(evictionPolicy: EvictionPolicy) : NormalizedCache() {
open class LruNormalizedCache constructor(evictionPolicy: EvictionPolicy) : NormalizedCache() {

private val lruCache: Cache<String, Record> =
CacheBuilder.newBuilder().apply {
Expand Down Expand Up @@ -97,4 +97,6 @@ class LruNormalizedCache internal constructor(evictionPolicy: EvictionPolicy) :
put(this@LruNormalizedCache::class, lruCache.asMap())
putAll(nextCache?.dump().orEmpty())
}

fun getFromCacheIfPresent(key: String): Record? = lruCache.getIfPresent(key)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.apollographql.apollo.cache.normalized.NormalizedCache
import com.apollographql.apollo.cache.normalized.Record
import com.apollographql.apollo.cache.normalized.RecordFieldJsonAdapter
import com.google.common.truth.Truth.assertThat
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -248,6 +250,25 @@ class LruNormalizedCacheTest {
assertThat(record2).isNull()
}

@Test
fun test_getFromCacheIfPresent() {
val lruCache = createLruNormalizedCache()
val testRecord1 = createTestRecord("1")
val testRecordSet: MutableCollection<Record> = HashSet()
testRecordSet.add(testRecord1)
lruCache.merge(testRecordSet, CacheHeaders.NONE)
assertEquals(testRecord1, lruCache.getFromCacheIfPresent("key1"))

val testRecord2 = createTestRecord("2")
lruCache.merge(testRecord2, CacheHeaders.NONE)

assertEquals(testRecord2, lruCache.getFromCacheIfPresent("key2"))

val testRecord3 = createTestRecord("2")
lruCache.merge(testRecord3, builder().addHeader(ApolloCacheHeaders.DO_NOT_STORE, "true").build())
assertNull(lruCache.getFromCacheIfPresent("key3"))
}

private fun createLruNormalizedCache() =
LruNormalizedCacheFactory(EvictionPolicy.builder().maxSizeBytes(10 * 1024.toLong()).build()).create(basicFieldAdapter)

Expand Down