Skip to content

Commit

Permalink
Ignore IOException when calling ApolloHttpCache.remove (#5730)
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD committed Mar 14, 2024
1 parent f9e439f commit 17dc887
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import okio.Buffer
import okio.ByteString.Companion.toByteString
import okio.FileSystem
import java.io.File
import java.io.IOException
import java.time.Instant
import java.time.format.DateTimeParseException

Expand Down Expand Up @@ -144,7 +145,10 @@ class CachingHttpInterceptor internal constructor(

val expireAfterRead = request.headers.valueOf(CACHE_EXPIRE_AFTER_READ_HEADER)?.lowercase() == "true"
if (expireAfterRead) {
lruHttpCache.remove(cacheKey)
try {
lruHttpCache.remove(cacheKey)
} catch (_: IOException) {
}
}

val timeoutMillis = request.headers.valueOf(CACHE_EXPIRE_TIMEOUT_HEADER)?.toLongOrNull() ?: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import okio.FileSystem
import java.io.File
import java.io.IOException

enum class HttpFetchPolicy {
/**
Expand Down Expand Up @@ -129,13 +130,19 @@ fun ApolloClient.Builder.httpCache(
catch { throwable ->
// Revert caching of responses with errors
val cacheKey = synchronized(apolloRequestToCacheKey) { apolloRequestToCacheKey[request.requestUuid.toString()] }
cacheKey?.let { cachingHttpInterceptor.cache.remove(it) }
try {
cacheKey?.let { cachingHttpInterceptor.cache.remove(it) }
} catch (_: IOException) {
}
throw throwable
}.onEach { response ->
// Revert caching of responses with errors
val cacheKey = synchronized(apolloRequestToCacheKey) { apolloRequestToCacheKey[request.requestUuid.toString()] }
if (response.hasErrors()) {
cacheKey?.let { cachingHttpInterceptor.cache.remove(it) }
try {
cacheKey?.let { cachingHttpInterceptor.cache.remove(it) }
} catch (_: IOException) {
}
}
}.onCompletion {
synchronized(apolloRequestToCacheKey) { apolloRequestToCacheKey.remove(request.requestUuid.toString()) }
Expand Down

0 comments on commit 17dc887

Please sign in to comment.