Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use a shared cache in okhttp

Tags:

java

okhttp

Is it possible to use the okhttp cache in a shared manner, ie: having multiple clients accessing the same cache directory in order to avoid having to each keep their own.

In the repices of the okhttp wiki we find:

It is an error to have multiple caches accessing the same cache directory simultaneously

I am not looking for different caches to use the same folder, but rather a single shared cache that can be updated by more than one client.

A simple example would be (in Java) running:

    Cache cache = new Cache(cacheDirectory, cacheSize);

    client  = new OkHttpClient.Builder().cache(cache).build();
    client2 = new OkHttpClient.Builder().cache(cache).build();

Would this be unsafe to do? If so, is there an alternative to achieve this or is it not possible at all?

like image 479
Kostas Avatar asked Dec 08 '25 20:12

Kostas


1 Answers

Yep, that's completely safe. It would be unsafe to have multiple instances of Cache that share a directory, whether in one process or multiple.

like image 66
Jesse Wilson Avatar answered Dec 10 '25 09:12

Jesse Wilson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!