2016-09-26 86 views
-1

我需要一個函數來清除我的應用程序的完整緩存。我正在使用Retrofit和okHttp爲我的defaut Requests和Picasso進行圖片加載。有沒有可能?清除緩存(Retrofit/okHttp)

我知道我可以爲CacheControl.FORCE_NETWORK做一個特定的請求,但我需要清除整個Cache之後。

任何想法?

+0

對不起,會幫你改裝,如果能:)退房雖然清除緩存畢加索..也安排有讓你可以設置自定義光盤和LRU高速緩存大小 – Kushan

回答

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();