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

Call through to loadRecords from the MemoryCache to the SQL one #5848

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -39,19 +39,28 @@ class MemoryCache(
get() = lockRead { lruCache.weight() }

override fun loadRecord(key: String, cacheHeaders: CacheHeaders): Record? = lockRead {
val record = lruCache[key]?.also {
if (cacheHeaders.hasHeader(ApolloCacheHeaders.EVICT_AFTER_READ)) {
lruCache.remove(key)
}
}

val record = internalLoadRecord(key, cacheHeaders)
record ?: nextCache?.loadRecord(key, cacheHeaders)?.also { nextCachedRecord ->
lruCache[key] = nextCachedRecord
}
}

override fun loadRecords(keys: Collection<String>, cacheHeaders: CacheHeaders): Collection<Record> {
return keys.mapNotNull { key -> loadRecord(key, cacheHeaders) }
override fun loadRecords(keys: Collection<String>, cacheHeaders: CacheHeaders): Collection<Record> = lockRead {
val recordsByKey: Map<String, Record?> = keys.associateWith { key -> internalLoadRecord(key, cacheHeaders) }
val missingKeys = recordsByKey.filterValues { it == null }.keys
val nextCachedRecords = nextCache?.loadRecords(missingKeys, cacheHeaders).orEmpty()
for (record in nextCachedRecords) {
lruCache[record.key] = record
}
recordsByKey.values.filterNotNull() + nextCachedRecords
}

private fun internalLoadRecord(key: String, cacheHeaders: CacheHeaders): Record? {
return lruCache[key]?.also {
if (cacheHeaders.hasHeader(ApolloCacheHeaders.EVICT_AFTER_READ)) {
lruCache.remove(key)
}
}
}

override fun clearAll() {
Expand Down
Expand Up @@ -38,22 +38,34 @@ class MemoryCache(
get() = lruCache.size()

override fun loadRecord(key: String, cacheHeaders: CacheHeaders): Record? = lock.lock {
val cacheEntry = lruCache[key]?.also { cacheEntry ->
if (cacheEntry.isExpired || cacheHeaders.hasHeader(ApolloCacheHeaders.EVICT_AFTER_READ)) {
lruCache.remove(key)
}
}

cacheEntry?.takeUnless { it.isExpired }?.record ?: nextCache?.loadRecord(key, cacheHeaders)?.also { nextCachedRecord ->
val record = internalLoadRecord(key, cacheHeaders)
record ?: nextCache?.loadRecord(key, cacheHeaders)?.also { nextCachedRecord ->
lruCache[key] = CacheEntry(
record = nextCachedRecord,
expireAfterMillis = expireAfterMillis
)
}
}

override fun loadRecords(keys: Collection<String>, cacheHeaders: CacheHeaders): Collection<Record> {
return keys.mapNotNull { key -> loadRecord(key, cacheHeaders) }
override fun loadRecords(keys: Collection<String>, cacheHeaders: CacheHeaders): Collection<Record> = lock.lock {
val recordsByKey: Map<String, Record?> = keys.associateWith { key -> internalLoadRecord(key, cacheHeaders) }
val missingKeys = recordsByKey.filterValues { it == null }.keys
val nextCachedRecords = nextCache?.loadRecords(missingKeys, cacheHeaders).orEmpty()
for (record in nextCachedRecords) {
lruCache[record.key] = CacheEntry(
record = record,
expireAfterMillis = expireAfterMillis
)
}
recordsByKey.values.filterNotNull() + nextCachedRecords
}

private fun internalLoadRecord(key: String, cacheHeaders: CacheHeaders): Record? {
return lruCache[key]?.also { cacheEntry ->
if (cacheEntry.isExpired || cacheHeaders.hasHeader(ApolloCacheHeaders.EVICT_AFTER_READ)) {
lruCache.remove(key)
}
}?.takeUnless { it.isExpired }?.record
}

override fun clearAll() {
Expand All @@ -79,7 +91,7 @@ class MemoryCache(
var total = 0
val keys = HashSet(lruCache.keys()) // local copy to avoid concurrent modification
keys.forEach {
if (regex.matches(it)){
if (regex.matches(it)) {
lruCache.remove(it)
total++
}
Expand Down Expand Up @@ -137,7 +149,7 @@ class MemoryCache(

private class CacheEntry(
val record: Record,
val expireAfterMillis: Long
val expireAfterMillis: Long,
) {
val cachedAtMillis: Long = currentTimeMillis()

Expand Down