简体   繁体   English

如何在Rails 3.2中缓存gravatars?

[英]How do I cache gravatars in Rails 3.2?

I wrote a gravatar helper to use in my learning app. 我写了一个Gravatar助手来在我的学习应用程序中使用。 The Users#index action lists all users with their gravatars, which can take a while even with pagination. “ Users#index”操作列出了所有带有重力的用户,即使分页也会花费一些时间。 Is there a way to cache the gravatars in Rails 3.2 with the following in mind: 考虑到以下几点,有没有一种方法可以在Rails 3.2中缓存凹印:

  1. Without using a Gravatar plugin that has a cache feature. 不使用具有缓存功能的Gravatar插件。
  2. Without caching the users page itself; 无需缓存用户页面本身; really, I'm only looking to cache the images themselves without having to grab them from gravatar.com again. 确实,我只是希望缓存图像本身,而不必再次从gravatar.com上获取它们。

     def gravatar_for(user, options = { size: 50 }) gravatar_id = Digest::MD5::hexdigest(user.email.downcase) size = options[:size] gravatar_url = "http://gravatar.com/avatar/#{gravatar_id}.png?s=#{size}" image_tag(gravatar_url, alt: user.name) end 

If you view the source, you'll see SO doesn't do their own gravatar caching. 如果您查看源代码,您将看到SO不会自己进行图像缓存。 Refresh the page a few times and you might see the gravatars are still the last elements to load. 刷新页面几次,您可能会看到凹版仍然是最后要加载的元素。

Some things you can do to improve performance are: 您可以采取一些措施来提高性能:

  • When run in production mode with cache control set up correctly, the browser will do some image caching for you (maybe watch the railscasts on that, episode 321 I think) 在生产模式下运行并正确设置了缓存控制后,浏览器将为您执行一些图像缓存(我想也许可以看一下第321集的相关视频)
  • Include the image dimensions in your img tag, the browser will reserve the right amount of screen space even before the image is available, it does fewer redraws and feels livelier 在img标签中包含图像尺寸,浏览器将在图像可用之前保留适当的屏幕空间,重绘次数更少,感觉更生动
  • Serve up your own placeholder instead of more expensive identicons etc. 服务自己的占位符,而不要使用更昂贵的identicons等。

My question is why you need this? 我的问题是为什么你需要这个? You are putting image from external server and user browser and gravatar.com must cache it. 您正在从外部服务器和用户浏览器放置图像,并且gravatar.com必须对其进行缓存。 It's reason why gravatar exists, for manage avatars without any work from developer. 这是存在gravatar的原因,无需开发人员的任何工作即可管理化身。 If you need to cache gravatars then why you really need it? 如果您需要缓存凹印机,那么为什么您真正需要它呢?

Caching the entire users page or fragment caching the image urls wouldn't make a difference anyway because the images will still be downloaded from Gravatar. 缓存整个用户页面或缓存图像URL片段都没有任何区别,因为仍然可以从Gravatar下载图像。 Gravatar sets their Cache-Control header to have a max age of 5 minutes and there's not much else you can do about it. Gravatar将其Cache-Control标头设置为最长5分钟,并且您无能为力。

One possibility is to save the images to another location where you have control over the response headers. 一种可能是将图像保存到您可以控制响应头的另一个位置。

One minor thing you could do during development though is to not use a hard refresh on your pages. 但是,在开发过程中您可以做的一件事是不对页面进行硬刷新。 In some browsers CMD/CTRL + R or pressing the refresh button sends request headers with Cache-Control:max-age=0 which causes you to re-download all the images. 在某些浏览器中, CMD/CTRL + R或按刷新按钮将发送带有Cache-Control:max-age=0请求标头,从而使您重新下载所有图像。 You can do a soft refresh by simply visiting the page again (for example: pressing enter on the url in the addressbar) and it won't send that header. 您只需再次访问该页面即可进行软刷新(例如:在地址栏中的url上按Enter键),它将不会发送该标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM