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

Backport #5848 to the v3 branch #5863

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.
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 @@ -39,22 +39,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)
}
val record = internalLoadRecord(key, cacheHeaders)
record ?: nextCache?.loadRecord(key, cacheHeaders)?.also { nextCachedRecord ->
lruCache[key] = CacheEntry(
record = nextCachedRecord,
expireAfterMillis = expireAfterMillis
)
}
}

cacheEntry?.takeUnless { it.isExpired }?.record ?: nextCache?.loadRecord(key, cacheHeaders)?.also { nextCachedRecord ->
lruCache[key] = CacheEntry(
record = nextCachedRecord,
expireAfterMillis = expireAfterMillis
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
}

override fun loadRecords(keys: Collection<String>, cacheHeaders: CacheHeaders): Collection<Record> {
return keys.mapNotNull { key -> loadRecord(key, cacheHeaders) }
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 Down
Original file line number Diff line number Diff line change
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 @@ -132,7 +144,7 @@ class MemoryCache(

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

Expand Down
Loading