2015-06-04 79 views
0

我正在使用Parse和Picasso將圖像加載到ParseImageViews。有什麼我錯過緩存解析文件?我的listview似乎每次都從服務器獲取文件並使用Picasso附帶的磁盤緩存。如何使用磁盤緩存來緩存ParseFile

我沒有看到的Cache-Control:max-age的參數在解析文件下載的HTTP響應(從Amazon S3,其中解析並將它們存儲)

我有下面的代碼,

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

任何幫助,將不勝感激。謝謝。

+0

保存到stoarage? – Elltz

+0

@Elltz,你是什麼意思?對不起,我是新來Parse – user3030130

+0

我的意思是你有'parseFile'' f'因此將它保存到你的內部存儲器,當你需要它時你檢索它不需要再次去網絡 – Elltz

回答

0

使用OkHttp客戶端作爲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; 
    } 
    });