Skip to content

Commit

Permalink
Backport #5840 to the v3 branch (#5861)
Browse files Browse the repository at this point in the history
(cherry picked from commit 7079290)
  • Loading branch information
BoD authored Apr 29, 2024
1 parent d9f481b commit 25b78bc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import kotlin.reflect.KClass
* (there is no any sort of GC that runs in the background).
*/
class MemoryCache(
private val maxSizeBytes: Int = Int.MAX_VALUE,
private val expireAfterMillis: Long = -1,
private val maxSizeBytes: Int = Int.MAX_VALUE,
private val expireAfterMillis: Long = -1,
) : NormalizedCache() {
/**
* A lock that is only used during read accesses on the JVM because
Expand Down Expand Up @@ -47,8 +47,8 @@ class MemoryCache(

cacheEntry?.takeUnless { it.isExpired }?.record ?: nextCache?.loadRecord(key, cacheHeaders)?.also { nextCachedRecord ->
lruCache[key] = CacheEntry(
record = nextCachedRecord,
expireAfterMillis = expireAfterMillis
record = nextCachedRecord,
expireAfterMillis = expireAfterMillis
)
}
}
Expand Down Expand Up @@ -104,36 +104,41 @@ class MemoryCache(
return emptySet()
}

val changedKeys = internalMerge(record, cacheHeaders, recordMerger)
return changedKeys + nextCache?.merge(record, cacheHeaders, recordMerger).orEmpty()
}

@ApolloExperimental
override fun merge(records: Collection<Record>, cacheHeaders: CacheHeaders, recordMerger: RecordMerger): Set<String> {
if (cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE)) {
return emptySet()
}
val changedKeys = records.flatMap { record -> internalMerge(record, cacheHeaders, recordMerger) }.toSet()
return changedKeys + nextCache?.merge(records, cacheHeaders, recordMerger).orEmpty()
}

private fun internalMerge(record: Record, cacheHeaders: CacheHeaders, recordMerger: RecordMerger): Set<String> {
val oldRecord = loadRecord(record.key, cacheHeaders)
val changedKeys = if (oldRecord == null) {
lruCache[record.key] = CacheEntry(
record = record,
expireAfterMillis = expireAfterMillis
record = record,
expireAfterMillis = expireAfterMillis
)
record.fieldKeys()
} else {
val (mergedRecord, changedKeys) = recordMerger.merge(existing = oldRecord, incoming = record, newDate = null)
lruCache[record.key] = CacheEntry(
record = mergedRecord,
expireAfterMillis = expireAfterMillis
record = mergedRecord,
expireAfterMillis = expireAfterMillis
)
changedKeys
}

return changedKeys + nextCache?.merge(record, cacheHeaders, recordMerger).orEmpty()
}

@ApolloExperimental
override fun merge(records: Collection<Record>, cacheHeaders: CacheHeaders, recordMerger: RecordMerger): Set<String> {
if (cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE)) {
return emptySet()
}
return records.flatMap { record -> merge(record, cacheHeaders, recordMerger) }.toSet()
return changedKeys
}

override fun dump(): Map<KClass<*>, Map<String, Record>> {
return mapOf(
this::class to lruCache.dump().mapValues { (_, entry) -> entry.record }
this::class to lruCache.dump().mapValues { (_, entry) -> entry.record }
) + nextCache?.dump().orEmpty()
}

Expand All @@ -142,8 +147,8 @@ class MemoryCache(
}

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

Expand All @@ -161,14 +166,14 @@ class MemoryCache(
}

class MemoryCacheFactory @JvmOverloads constructor(
private val maxSizeBytes: Int = Int.MAX_VALUE,
private val expireAfterMillis: Long = -1,
private val maxSizeBytes: Int = Int.MAX_VALUE,
private val expireAfterMillis: Long = -1,
) : NormalizedCacheFactory() {

override fun create(): MemoryCache {
return MemoryCache(
maxSizeBytes = maxSizeBytes,
expireAfterMillis = expireAfterMillis,
maxSizeBytes = maxSizeBytes,
expireAfterMillis = expireAfterMillis,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ class MemoryCache(
return emptySet()
}

val changedKeys = internalMerge(record, cacheHeaders)
return changedKeys + nextCache?.merge(record, cacheHeaders).orEmpty()
}

override fun merge(records: Collection<Record>, cacheHeaders: CacheHeaders): Set<String> {
if (cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE)) {
return emptySet()
}
val changedKeys = records.flatMap { record -> internalMerge(record, cacheHeaders) }.toSet()
return changedKeys + nextCache?.merge(records, cacheHeaders).orEmpty()
}

private fun internalMerge(record: Record, cacheHeaders: CacheHeaders): Set<String> {
val oldRecord = loadRecord(record.key, cacheHeaders)
val changedKeys = if (oldRecord == null) {
lruCache[record.key] = CacheEntry(
Expand All @@ -109,15 +122,7 @@ class MemoryCache(
)
changedKeys
}

return changedKeys + nextCache?.merge(record, cacheHeaders).orEmpty()
}

override fun merge(records: Collection<Record>, cacheHeaders: CacheHeaders): Set<String> {
if (cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE)) {
return emptySet()
}
return records.flatMap { record -> merge(record, cacheHeaders) }.toSet()
return changedKeys
}

override fun dump(): Map<KClass<*>, Map<String, Record>> {
Expand Down

0 comments on commit 25b78bc

Please sign in to comment.