Cache donation campaign responses

This commit is contained in:
Grishka 2024-04-18 02:55:07 +03:00
parent b9956950b6
commit b5aa1a4598
3 changed files with 16 additions and 0 deletions

View File

@ -12,10 +12,12 @@ import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import org.joinmastodon.android.BuildConfig;
import org.joinmastodon.android.MastodonApp;
import org.joinmastodon.android.api.gson.IsoInstantTypeAdapter;
import org.joinmastodon.android.api.gson.IsoLocalDateTypeAdapter;
import org.joinmastodon.android.api.session.AccountSession;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.time.Instant;
@ -29,6 +31,8 @@ import java.util.concurrent.TimeUnit;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import me.grishka.appkit.utils.WorkerThread;
import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
@ -49,8 +53,11 @@ public class MastodonAPIController{
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.cache(new Cache(new File(MastodonApp.context.getCacheDir(), "http"), 10*1024*1024))
.build();
private static final CacheControl NO_CACHE_WHATSOEVER=new CacheControl.Builder().noCache().noStore().build();
private AccountSession session;
static{
@ -80,6 +87,9 @@ public class MastodonAPIController{
if(token!=null)
builder.header("Authorization", "Bearer "+token);
if(!req.cacheable)
builder.cacheControl(NO_CACHE_WHATSOEVER);
if(req.headers!=null){
for(Map.Entry<String, String> header:req.headers.entrySet()){
builder.header(header.getKey(), header.getValue());

View File

@ -46,6 +46,7 @@ public abstract class MastodonAPIRequest<T> extends APIRequest<T>{
boolean canceled;
Map<String, String> headers;
long timeout;
boolean cacheable;
private ProgressDialog progressDialog;
protected boolean removeUnsupportedItems;
@ -132,6 +133,10 @@ public abstract class MastodonAPIRequest<T> extends APIRequest<T>{
this.timeout=timeout;
}
protected void setCacheable(){
cacheable=true;
}
protected String getPathPrefix(){
return "/api/v1";
}

View File

@ -12,6 +12,7 @@ public class GetDonationCampaigns extends MastodonAPIRequest<DonationCampaign>{
super(HttpMethod.GET, null, DonationCampaign.class);
this.locale=locale;
this.seed=seed;
setCacheable();
}
@Override