Skip to content

Commit 446b760

Browse files
committed
Call through to loadRecords from the MemoryCache to the SQL one (apollographql#5848)
1 parent 300ca4e commit 446b760

File tree

2 files changed

+40
-19
lines changed
  • libraries
    • apollo-normalized-cache-api-incubating/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/api
    • apollo-normalized-cache-api/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/api

2 files changed

+40
-19
lines changed

libraries/apollo-normalized-cache-api-incubating/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/api/MemoryCache.kt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,28 @@ class MemoryCache(
3939
get() = lockRead { lruCache.weight() }
4040

4141
override fun loadRecord(key: String, cacheHeaders: CacheHeaders): Record? = lockRead {
42-
val record = lruCache[key]?.also {
43-
if (cacheHeaders.hasHeader(ApolloCacheHeaders.EVICT_AFTER_READ)) {
44-
lruCache.remove(key)
45-
}
46-
}
47-
42+
val record = internalLoadRecord(key, cacheHeaders)
4843
record ?: nextCache?.loadRecord(key, cacheHeaders)?.also { nextCachedRecord ->
4944
lruCache[key] = nextCachedRecord
5045
}
5146
}
5247

53-
override fun loadRecords(keys: Collection<String>, cacheHeaders: CacheHeaders): Collection<Record> {
54-
return keys.mapNotNull { key -> loadRecord(key, cacheHeaders) }
48+
override fun loadRecords(keys: Collection<String>, cacheHeaders: CacheHeaders): Collection<Record> = lockRead {
49+
val recordsByKey: Map<String, Record?> = keys.associateWith { key -> internalLoadRecord(key, cacheHeaders) }
50+
val missingKeys = recordsByKey.filterValues { it == null }.keys
51+
val nextCachedRecords = nextCache?.loadRecords(missingKeys, cacheHeaders).orEmpty()
52+
for (record in nextCachedRecords) {
53+
lruCache[record.key] = record
54+
}
55+
recordsByKey.values.filterNotNull() + nextCachedRecords
56+
}
57+
58+
private fun internalLoadRecord(key: String, cacheHeaders: CacheHeaders): Record? {
59+
return lruCache[key]?.also {
60+
if (cacheHeaders.hasHeader(ApolloCacheHeaders.EVICT_AFTER_READ)) {
61+
lruCache.remove(key)
62+
}
63+
}
5564
}
5665

5766
override fun clearAll() {

libraries/apollo-normalized-cache-api/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/api/MemoryCache.kt

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,34 @@ class MemoryCache(
3838
get() = lruCache.size()
3939

4040
override fun loadRecord(key: String, cacheHeaders: CacheHeaders): Record? = lock.lock {
41-
val cacheEntry = lruCache[key]?.also { cacheEntry ->
42-
if (cacheEntry.isExpired || cacheHeaders.hasHeader(ApolloCacheHeaders.EVICT_AFTER_READ)) {
43-
lruCache.remove(key)
44-
}
45-
}
46-
47-
cacheEntry?.takeUnless { it.isExpired }?.record ?: nextCache?.loadRecord(key, cacheHeaders)?.also { nextCachedRecord ->
41+
val record = internalLoadRecord(key, cacheHeaders)
42+
record ?: nextCache?.loadRecord(key, cacheHeaders)?.also { nextCachedRecord ->
4843
lruCache[key] = CacheEntry(
4944
record = nextCachedRecord,
5045
expireAfterMillis = expireAfterMillis
5146
)
5247
}
5348
}
5449

55-
override fun loadRecords(keys: Collection<String>, cacheHeaders: CacheHeaders): Collection<Record> {
56-
return keys.mapNotNull { key -> loadRecord(key, cacheHeaders) }
50+
override fun loadRecords(keys: Collection<String>, cacheHeaders: CacheHeaders): Collection<Record> = lock.lock {
51+
val recordsByKey: Map<String, Record?> = keys.associateWith { key -> internalLoadRecord(key, cacheHeaders) }
52+
val missingKeys = recordsByKey.filterValues { it == null }.keys
53+
val nextCachedRecords = nextCache?.loadRecords(missingKeys, cacheHeaders).orEmpty()
54+
for (record in nextCachedRecords) {
55+
lruCache[record.key] = CacheEntry(
56+
record = record,
57+
expireAfterMillis = expireAfterMillis
58+
)
59+
}
60+
recordsByKey.values.filterNotNull() + nextCachedRecords
61+
}
62+
63+
private fun internalLoadRecord(key: String, cacheHeaders: CacheHeaders): Record? {
64+
return lruCache[key]?.also { cacheEntry ->
65+
if (cacheEntry.isExpired || cacheHeaders.hasHeader(ApolloCacheHeaders.EVICT_AFTER_READ)) {
66+
lruCache.remove(key)
67+
}
68+
}?.takeUnless { it.isExpired }?.record
5769
}
5870

5971
override fun clearAll() {
@@ -79,7 +91,7 @@ class MemoryCache(
7991
var total = 0
8092
val keys = HashSet(lruCache.keys()) // local copy to avoid concurrent modification
8193
keys.forEach {
82-
if (regex.matches(it)){
94+
if (regex.matches(it)) {
8395
lruCache.remove(it)
8496
total++
8597
}
@@ -137,7 +149,7 @@ class MemoryCache(
137149

138150
private class CacheEntry(
139151
val record: Record,
140-
val expireAfterMillis: Long
152+
val expireAfterMillis: Long,
141153
) {
142154
val cachedAtMillis: Long = currentTimeMillis()
143155

0 commit comments

Comments
 (0)