Kotlin OKHTTP, handle cache when header has change

Viewed 15

I'am using okhttp for my api call, it working very good and i would like to optimise my code a little bit so i wanted to add some cache control on my OkHttpClient like so :

val cacheSize = 10 * 1024 * 1024 // 10MB

        client = OkHttpClient.Builder()
                .addInterceptor(logging)
                .readTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .cache(Cache(context.cacheDir, cacheSize.toLong()))
                .build()

First of all i'am not really sure how it's work does it check if some cache is available and it doesn't call my api ?

Anyways my problem is that i have a call : "https://myapi.com/store", that give me a list of object, but i have the same call with a header : title = "yourtitle" that give me a list of object that only contain the title i was looking for.

Here is my problem i do my first call and it give me list A of all object, but when i do the second call with my header, instead of giving me list B of the object i want, i still get the list A that is saved in cache.

How can i handle cache so when my header change, the cache is ignored ? Also i have a client of all my all so i can't ignore cache only of this client.

0 Answers
Related