好的,我在 Picasso 内部做了很多挖掘工作,OKHTTP 的内部正在努力找出缓存是如何发生的,策略是什么等等。
对于尝试使用最新的 picasso 2.5+ 和 Okhttp 3+ 的人,接受的答案将不起作用! (我不检查最新的不好:()
1) getSharedInstance 不是线程安全的,使其同步。
2) 如果您不每次都进行此调用,请拨打Picasso.setSingletonInstance(thecustompicassocreatedbygetsharedinstance)
附注在 try 块内执行此操作,以避免在静态单例未销毁的破坏后非常快速地重新打开活动时出现非法状态异常。还要确保在任何 Picasso.with(context) 调用之前调用此方法
3) 查看代码,我建议人们不要干预 LruCache,除非绝对确定,否则很容易导致浪费未使用的 RAM 或设置低->内存不足异常。
4) 如果你甚至不做任何这些都很好。默认情况下,毕加索会尝试从其内置的 okhttpdownloader 中创建磁盘缓存。但这可能会也可能不会根据您使用的毕加索版本而起作用。如果它不起作用,它会使用默认的 java URL 下载器,该下载器也会自己进行一些缓存。
5) 我看到做这一切的唯一主要原因是获得清除缓存功能。众所周知,毕加索不会轻易做到这一点,因为它在包装内受到保护。而且像我这样的大多数普通人都使用 gradle 来包含使我们无法访问缓存清除权限的包。
这是代码以及我想要的所有选项。这将使用 jakewharton 的 Picasso 2.5.2、Okhttp 3.4.0 和 OkHttp3Downloader。
package com.example.project.recommendedapp;
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 limit 50mb
//private static int mMemoryCacheSize = 50*1024*1024; //Memory Cache 50mb, not currently using this. Using default implementation
private static OkHttpClient mOkHttp3Client; //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
mOkHttp3Client = 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(mOkHttp3Client)).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;
}
}