简体   繁体   English

清除缓存(Retrofit / okHttp)

[英]Clear Cache (Retrofit/okHttp)

I need a function to clear the complete Cache of my App. 我需要一个功能来清除我的应用程序的完整缓存。 I´m using Retrofit with okHttp for my defaut Requests and Picasso for Image Loading. 我正在使用带有okHttp的Retrofit来处理我的默认请求,并使用Picasso来加载图像。 Is there any possibility? 有没有可能?

I know that i can do CacheControl.FORCE_NETWORK for a specific Request, but i need to clear the whole Cache afterwards. 我知道我可以为特定请求执行CacheControl.FORCE_NETWORK ,但之后需要清除整个Cache。

Any Ideas? 有任何想法吗?

Here is a custom singleton which i made for picasso. 这是我为毕加索制作的自定义单身人士。 You can use the clear cache method to clear the picasso cache. 您可以使用清除缓存方法清除毕加索缓存。 I can't really help you with retrofit as i haven't used that... just use the values as you please inside this class... 我真的不能帮您进行翻新,因为我还没有用过...只要在类中随意使用值即可...

import android.content.Context;
import android.util.Log;

import com.jakewharton.picasso.OkHttp3Downloader;
import com.squareup.picasso.LruCache;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;

//Singleton Class for Picasso Downloading, Caching and Displaying Images Library
public class PicassoSingleton {

private static Picasso mInstance;
private static long mDiskCacheSize = 50*1024*1024; //Disk Cache 50mb
private static int mMemoryCacheSize = 50*1024*1024; //Memory Cache 50mb, not currently using this. Using default implementation
private static OkHttpClient mOkHttpClient; //OK Http Client for downloading
private static OkHttp3Downloader okHttp3Downloader;
private static Cache diskCache;
private static LruCache lruCache;//not using it currently


public static synchronized Picasso getSharedInstance(Context context)
{
    if(mInstance == null) {
        if (context != null) {
            //Create disk cache folder if does not exist
            File cache = new File(context.getApplicationContext().getCacheDir(), "picasso_cache");
            if (!cache.exists()) {
                cache.mkdirs();
            }

            diskCache = new Cache(cache, mDiskCacheSize);
            //lruCache = new LruCache(mMemoryCacheSize);//not going to be using it, using default memory cache currently
            lruCache = new LruCache(context); // This is the default lrucache for picasso-> calculates and sets memory cache by itself

            //Create OK Http Client with retry enabled, timeout and disk cache
            mOkHttpClient = new OkHttpClient.Builder().cache(diskCache).connectTimeout(6000, TimeUnit.SECONDS).build();  //100 min cache timeout



            //For better performence in Memory use set memoryCache(Cache.NONE) in this builder (If needed)
            mInstance = new Picasso.Builder(context).memoryCache(lruCache).downloader(new OkHttp3Downloader(mOkHttpClient)).indicatorsEnabled(true).build();

        }
    }
    return mInstance;
}

public static void deletePicassoInstance()
{
    mInstance = null;
}

public static void clearLRUCache()
{
    if(lruCache!=null) {
        lruCache.clear();
        Log.d("FragmentCreate","clearing LRU cache");
    }

    lruCache = null;

}

public static void clearDiskCache(){
    try {
        if(diskCache!=null) {
            diskCache.evictAll();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    diskCache = null;

}
}

You can use it as follows: 您可以按以下方式使用它:

 Picasso customPicasso= PicassoSingleton.getSharedInstance(youContext);
 Picasso.setSingletonInstance(customPicasso);

Then clear the cache as : 然后将缓存清除为:

 PicassoSingleton.clearLRUCache();

That means there is no need for the caching feature. 这意味着不需要缓存功能。 It allows you to pass a null into cache control. 它允许您将null传递给缓存控件。 It removes the cache. 它删除缓存。

private OkHttpClient createOkHttpClient() {
    return new OkHttpClient.Builder()
            .readTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60 / 2, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .cache(null)
            .build();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM