本文介绍了将Square Picasso的缓存大小限制为最大60MB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Picasso在多个回收站视图中的应用程序中下载和缓存图像.到目前为止,毕加索已使用了大约49MB的缓存大小,我担心随着越来越多的图像发挥作用,这个数字会变得更高.

I am currently using Picasso to download and cache images in my app inside multiple recycler views. So far Picasso has used around 49MB cache size and i am worried that as more images come into play, this will become much higher.

我正在使用默认的Picasso.with(context)对象.请回答以下问题:

I am using the default Picasso.with(context) object. Please answer the following:

1)有没有一种方法可以限制毕加索的缓存大小.不能将MemoryPolicy和NetworkPolicy设置为NO_CACHE.我需要缓存,但要达到一定级别(最大60MB)

1) Is there a way to restrict the Size of Picasso's cache. MemoryPolicy and NetworkPolicy set to NO_CACHE isn't an option. I need caching but upto a certain level (60MB max)

2)毕加索中是否有一种方法可以像Glide DiskCacheStrategy.RESULT那样存储调整大小/裁剪后的图像

2) Is there a way in picasso to store Resized/cropped images like in Glide DiskCacheStrategy.RESULT

3)如果该选项是使用OKHTTP,请引导我学习一个很好的教程,以使用它来限制Picasso的缓存大小. (毕加索2.5.2)

3) If the option is to use OKHTTP, please guide me to a good tutorial for using it to limit Picasso's cache size. (Picasso 2.5.2)

4)由于我使用的是毕加索的Gradle依赖项,因此如何添加一个清晰的Cache函数,如下所示:

4) Since i am using a Gradle dependency of Picasso, how can i add a clear Cache function as shown here:

清除毕加索的高速缓存

推荐答案

请尝试使用此方法,它确实对我有用:

Please try this one, it does seem to work great for me:

我将其用作Singleton.只需将DISK/CACHE大小参数放在60处即可.

I use it as a Singleton.Just put 60 where DISK/CACHE size parameters are.

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

    private static Picasso mInstance;
    private static long mDiskCacheSize = CommonConsts.DISK_CACHE_SIZE * 1024 * 1024; //Disk Cache
    private static int mMemoryCacheSize = CommonConsts.MEMORY_CACHE_SIZE * 1024 * 1024; //Memory Cache
    private static OkHttpClient mOkHttpClient; //OK Http Client for downloading
    private static Cache diskCache;
    private static LruCache lruCache;


    public static Picasso getSharedInstance(Context context) {
        if (mInstance == null && 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);
            //Create OK Http Client with retry enabled, timeout and disk cache
            mOkHttpClient = new OkHttpClient();
            mOkHttpClient.setConnectTimeout(CommonConsts.SECONDS_TO_OK_HTTP_TIME_OUT, TimeUnit.SECONDS);
            mOkHttpClient.setRetryOnConnectionFailure(true);
            mOkHttpClient.setCache(diskCache);

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

        }
    }
        return mInstance;
}

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

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

        lruCache = null;
        diskCache = null;
    }
}

这篇关于将Square Picasso的缓存大小限制为最大60MB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 12:52