Skip to content

Commit

Permalink
Call through to loadRecords from the MemoryCache to the SQL one
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD committed Apr 24, 2024
1 parent 52bea72 commit 0e38695
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
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
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 @@ -137,7 +149,7 @@ class MemoryCache(

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

Expand Down

0 comments on commit 0e38695

Please sign in to comment.