Skip to content

Commit 3ec908d

Browse files
committed
Added caching on failed (4xx) network requests
1 parent 0044d6b commit 3ec908d

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

app/src/main/java/com/nononsenseapps/feeder/FeederApplication.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import com.nononsenseapps.feeder.db.room.SyncRemoteDao
2929
import com.nononsenseapps.feeder.di.androidModule
3030
import com.nononsenseapps.feeder.di.archModelModule
3131
import com.nononsenseapps.feeder.di.networkModule
32+
import com.nononsenseapps.feeder.model.ForceCacheOnSomeFailuresInterceptor
3233
import com.nononsenseapps.feeder.model.TTSStateHolder
3334
import com.nononsenseapps.feeder.model.UserAgentInterceptor
3435
import com.nononsenseapps.feeder.notifications.NotificationsWorker
@@ -42,7 +43,6 @@ import com.nononsenseapps.jsonfeed.cachingHttpClient
4243
import kotlinx.coroutines.Dispatchers
4344
import kotlinx.coroutines.cancel
4445
import kotlinx.coroutines.withContext
45-
import okhttp3.Cache
4646
import okhttp3.CacheControl
4747
import okhttp3.OkHttpClient
4848
import org.conscrypt.Conscrypt
@@ -113,6 +113,7 @@ class FeederApplication : Application(), DIAware, ImageLoaderFactory {
113113
cacheDirectory = (filePathProvider.httpCacheDir),
114114
) {
115115
addNetworkInterceptor(UserAgentInterceptor)
116+
addNetworkInterceptor(ForceCacheOnSomeFailuresInterceptor)
116117
if (BuildConfig.DEBUG) {
117118
addInterceptor { chain ->
118119
val request = chain.request()
@@ -138,8 +139,6 @@ class FeederApplication : Application(), DIAware, ImageLoaderFactory {
138139
val okHttpClient =
139140
instance<OkHttpClient>()
140141
.newBuilder()
141-
// This is not used by Coil but no need to risk evicting the real cache
142-
.cache(Cache(filePathProvider.cacheDir.resolve("dummy_img"), 1024L))
143142
.addInterceptor { chain ->
144143
chain.proceed(
145144
when (!repository.loadImageOnlyOnWifi.value || currentlyUnmetered(this@FeederApplication)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.nononsenseapps.feeder.model
2+
3+
import com.nononsenseapps.feeder.util.logDebug
4+
import okhttp3.Interceptor
5+
import okhttp3.Response
6+
7+
object ForceCacheOnSomeFailuresInterceptor : Interceptor {
8+
override fun intercept(chain: Interceptor.Chain): Response {
9+
val response = chain.proceed(chain.request())
10+
// If server already set cache-control, don't override it
11+
if (response.headers("cache-control").isNotEmpty()) {
12+
return response
13+
}
14+
return when (response.code) {
15+
in 400..499 -> {
16+
logDebug("FEEDER", "cache-control forced for code ${response.code}")
17+
// Cache for 60 minutes
18+
// The intent is primarily to cache 404s for incorrect favicons
19+
// but all 4xx errors are probably wise not to hammer
20+
response
21+
.newBuilder()
22+
.header("cache-control", "max-age=3600")
23+
// Remove any headers that might conflict with caching
24+
.removeHeader("pragma")
25+
.removeHeader("expires")
26+
.removeHeader("x-cache")
27+
.build()
28+
}
29+
else -> response
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)