【问题标题】:How to use disk cache to cache ParseFile如何使用磁盘缓存来缓存 ParseFile
【发布时间】:2015-08-18 10:32:48
【问题描述】:

我正在使用 Parse 和 Picasso 将图像加载到 ParseImageViews。缓存解析文件时我缺少什么吗?我的列表视图似乎每次都从服务器获取文件并使用毕加索附带的磁盘缓存。

我没有在解析文件下载的 http 响应中看到 cache-control: max-age 参数(来自 amazon s3,其中 parse 存储它们)

我有以下代码,

    final ParseImageView pic = viewHolder.img;
    pic.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    ParseFile f = parseObject.getParseFile("image");
    Picasso.with(mContext).load(f.getUrl()).into(pic);

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 保存到存储区?
  • @Elltz,你这是什么意思?抱歉,我是 Parse 新手
  • 我的意思是你有 parseFile f 所以把它保存到你的内部存储中,当你需要它时你可以检索它,而无需再次访问网络

标签: android parse-platform picasso


【解决方案1】:

使用 OkHttp 客户端作为 Picasso 的 http 传输并指定磁盘和内存缓存大小:

OkHttpClient okHttp = new OkHttpClient();

Cache cache = new Cache(ctx.getCacheDir(), cacheSize);

okHttp.setCache(cache);

// Use OkHttp as downloader
Downloader downloader = new OkHttpDownloader(okHttp);

mPicasso = new Picasso.Builder(getApplicationContext())
  .downloader(downloader)).memoryCache(new LruCache(size)).build();

为 OkHttp 客户端设置请求拦截器(示例):

// Add Cache-Control to origin response (force cache)
 client.networkInterceptors().add(new Interceptor() {

    private com.squareup.okhttp.Request request;
    private Response response;
    private String requestUrl;

    @Override
    public Response intercept(Chain c) throws IOException {
        request = c.request();

        response = c.proceed(request);

        if (!request.cacheControl().noStore()
               && !response.cacheControl().noStore()) {

        requestUrl = request.urlString();

        // Do not cache keys or playlists

           response = response
            .newBuilder()
            .header("Cache-Control","public, max-age=42000").build();

        }

         return response;
     }
   });

【讨论】:

    猜你喜欢
    • 2016-06-12
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 2014-07-21
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多