1

我们正在开发一个 chrome 扩展,其中我们有 FB 朋友导入功能。现在,每当加载扩展程序时,它都会显示所有 FB 好友图像,因此从 FB 加载这些图像需要时间。我们希望最小化图像加载时间。有什么办法可以做到这一点?我们可以将图像缓存到 chrome 缓存并在第二次加载时从缓存中读取它们吗?

我也不认为这是相同的解决方案。但想检查有没有办法实现这一目标?

谢谢。

4

1 回答 1

2

I guess you’re loading the images by pointing to http(s)://graph.facebook.com/userid/picture?

Well, Facebook delivers these URLs with caching headers as follows,

Cache-Control: private, no-cache, no-store, must-revalidate 
Expires:       Sat, 01 Jan 2000 00:00:00 GMT    
Pragma:        no-cache

These are for the Graph API redirect URL – Facebook is forbidding caching here, so that the browser has to look up these resources every time again. The final image URL on their CDN, to which these URLs redirect with a Location header – those have the following headers regarding caching,

Last-Modified: Fri, 01 Jan 2010 00:00:00 GMT    
Cache-Control: max-age=1209600  
Expires:       Thu, 13 Sep 2012 08:07:52 GMT

So you could try and require the CDN URLs in your app upfront, and then use these as image URLs in your app’s HTML pages – then the browser won’t have to make a request every time to see if the cached version of the image URL is still valid, but can serve it from it’s cache straight away.

CDN URLs should not be used over longer time spans – they can change; but for a shorter amount of time it’s probably safe to use them, because they will not change willy-nilly without a good reason. F.e., the user changing their profile pic might be such a reason – but caching them for up to an hour or so could be a compromise that makes the user experience while using your app better.

But the drawback of doing so is, that your app will have to make an API request for each friend’s image initially, to get the CDN profile pic URL – and that’s time-consuming as well, if you do it for large number of friends. (Although batch requests or an FQL multi-query might be able to shorten that to one HTTP request instead of multiple ones.)

于 2012-08-30T08:15:43.480 回答