Clear Cache (Retrofit/okHttp)
我需要一个功能来清除我的应用程序的完整缓存。 我正在使用带有okHttp的Deffit进行默认请求,并使用Picasso进行图像加载。 有没有可能?
我知道我可以为特定请求执行
有任何想法吗?
这是我为毕加索制作的自定义单身人士。 您可以使用清除缓存方法清除毕加索缓存。 我真的不能帮您进行改造,因为我还没有用过……只要在类中随意使用值即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | 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; } } |
您可以按以下方式使用它:
1 2 | Picasso customPicasso= PicassoSingleton.getSharedInstance(youContext); Picasso.setSingletonInstance(customPicasso); |
然后将缓存清除为:
1 | PicassoSingleton.clearLRUCache(); |
这意味着不需要缓存功能。 它允许您将null传递给缓存控件。 它删除缓存。
1 2 3 4 5 6 7 8 | private OkHttpClient createOkHttpClient() { return new OkHttpClient.Builder() .readTimeout(60, TimeUnit.SECONDS) .connectTimeout(60 / 2, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .cache(null) .build(); } |