【问题标题】:Clear Cache (Retrofit/okHttp)清除缓存(改造/okHttp)
【发布时间】:2017-02-03 16:18:58
【问题描述】:

我需要一个函数来清除我的应用程序的完整缓存。我将 Retrofit 与 okHttp 用于我的默认请求和 Picasso 用于图像加载。有没有可能?

我知道我可以为特定的请求执行CacheControl.FORCE_NETWORK,但之后我需要清除整个缓存。

有什么想法吗?

【问题讨论】:

  • 对不起,如果可以的话,会帮助你进行改造:) 检查清除毕加索缓存.. 还在那里进行了安排,以便您可以设置自定义磁盘和 lru 缓存大小

标签: android retrofit picasso okhttp


【解决方案1】:

这是我为毕加索制作的自定义单例。您可以使用清除缓存方法来清除毕加索缓存。我无法真正帮助您进行改造,因为我没有使用过...只需在此类中使用您喜欢的值...

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;

}
}

你可以按如下方式使用它:

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

然后清除缓存为:

 PicassoSingleton.clearLRUCache();

【讨论】:

    【解决方案2】:

    这意味着不需要缓存功能。它允许您将 null 传递给缓存控件。它会删除缓存。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-25
      • 2015-06-01
      • 2023-03-27
      • 2016-01-29
      • 2015-05-21
      • 2016-03-20
      • 2017-12-29
      • 2018-05-15
      相关资源
      最近更新 更多