glide icon indicating copy to clipboard operation
glide copied to clipboard

OkHttp + Glide cache

Open yoobi opened this issue 3 years ago • 1 comments
trafficstars

Glide Version:

    kapt 'com.github.bumptech.glide:compiler:4.12.0'
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    implementation 'com.github.bumptech.glide:annotations:4.12.0'
    implementation('com.github.bumptech.glide:okhttp3-integration:4.12.0')

Integration libraries:

// OkHttp
    implementation(platform("com.squareup.okhttp3:okhttp-bom:4.9.0"))
    implementation("com.squareup.okhttp3:okhttp")
    implementation("com.squareup.okhttp3:logging-interceptor")

//Retrofit
    def retrofitVersion = "2.9.0"
    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
    implementation "com.squareup.retrofit2:converter-moshi:$retrofitVersion"
    implementation "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:$retrofitAdapterVersion"

Device/Android Version: Galaxy S7 Edge

Issue details / Repro steps / Use case background: I'm having a weird issue between my OkHttp cache and Glide. I'm displaying images in a Recyclerview. Those image changes in a daily basis. The issue is that it seems Glide is keeping the old image in cache and does not refresh it with the new ones. I've quickly created a button with onClickListener GlideApp.get(it.context).clearDiskCache() and when I go back to the page after clicking it I do get the new images.

My question is should I use diskCacheStrategy(DiskCacheStrategy.NONE) and skipMemoryCache(true) on my images if OkHttp is already caching the urls ? What's your recommandation on this use case ?

Retrofit Client

    private const val HEADER_CACHE_CONTROL = "Cache-Control"
    private const val HEADER_PRAGMA = "Pragma"

    private val retrofit = Retrofit.Builder()
            .addConverterFactory(MoshiConverterFactory.create(moshi))
            .addCallAdapterFactory(CoroutineCallAdapterFactory())
            .client(OkHttpClient.Builder()
                .readTimeout(15, TimeUnit.SECONDS)
                .connectTimeout(15, TimeUnit.SECONDS)
                .followRedirects(true)
                .cache(Cache(applicationContext.cacheDir, CACHE_SIZE))
                .addNetworkInterceptor(networkInterceptor())
                .addInterceptor(offlineInterceptor())
           ).baseUrl(BASE_URL)
            .build()

    private fun offlineInterceptor() = Interceptor { chain ->
        var request: Request = chain.request()
        if(!PreferencesHelper.readHasInternet()) {
            val cacheControl = CacheControl.Builder().maxStale(7, TimeUnit.DAYS).build()
            request = request.newBuilder()
                .removeHeader(HEADER_PRAGMA)
                .removeHeader(HEADER_CACHE_CONTROL)
                .cacheControl(cacheControl)
                .build()
        }
        chain.proceed(request)
    }

    private fun networkInterceptor() = Interceptor { chain ->
        val response: Response = chain.proceed(chain.request())
        val cacheControl = CacheControl.Builder()
            .maxAge(60, TimeUnit.MINUTES)
            .build()

        response.newBuilder()
            .removeHeader(HEADER_PRAGMA)
            .removeHeader(HEADER_CACHE_CONTROL)
            .header(HEADER_CACHE_CONTROL, cacheControl.toString())
            .build()
    }

My adapter is calling this snippet to show images in the layout below

    val imgUri = imgUrl.toUri().buildUpon().scheme("https").build()
    GlideApp.with(context).setDefaultRequestOptions(RequestOptions().error(R.drawable.image_load))
        .load(imgUri)
        .into(this)

Layout XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <com.google.android.material.card.MaterialCardView
        android:id="@+id/category_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardElevation="0dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/primaryColor">

            <ImageView
                android:id="@+id/category_image"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:scaleType="fitXY"
                app:layout_constraintDimensionRatio="H,16:9"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toTopOf="@id/category_title"
                android:contentDescription="@string/description_image_category" />

            <TextView
                android:id="@+id/category_title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/category_image"
                app:layout_constraintBottom_toBottomOf="parent"
                tool:text="Categorie name"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.card.MaterialCardView>
</FrameLayout>

yoobi avatar Jan 13 '22 11:01 yoobi

Since you are using GlideApp, you can share the cache for both OkHttp and Glide, see this example: https://github.com/jaredsburrows/android-gif-example/blob/master/app/src/main/java/com/burrowsapps/example/gif/di/AppGlideModule.kt#L56.

jaredsburrows avatar May 27 '22 23:05 jaredsburrows

Hello, sorry for the (more than) late answer, I've tried to follow your link but it seems it doesn't exists anymore

EDIT: Looks like I found it https://github.com/jaredsburrows/android-gif-search/blob/5c00c1a2e3299ed70164c344cf775216487c64c5/app/src/main/java/com/burrowsapps/example/gif/di/AppModule.kt

yoobi avatar Apr 25 '23 16:04 yoobi