Gravatar caching plugin

Gravatar is slow.

You use Gravatar on your site.

Your site is slow.

We hate slow site.

Caching is fast.

Cache Gravatars on your site.

Your site is fast!

Get it!

script/plugin install http://code.macournoyer.com/svn/plugins/gravatar_cache

Or using piston:

piston import http://code.macournoyer.com/svn/plugins/gravatar_cache vendor/plugins/gravatar_cache

Place something like this in your Rakefile:

def gravatar_emails
  User.find(:all).collect(&:email) # Return the emails you wanna cache
end

Now put everything in place for showtime:

rake gravatar:setup

Cache it!

rake gravatar:cache

You might wanna run this in a cron job, here’s mine:

0 */3 * * * cd /var/www/refactormycode/current && /usr/local/bin/rake gravatar:cache RAILS_ENV=production

Caches it every 3 hours.

Now in your view (or helper):

image_tag Gravatar.new('an@email.com').url

Magic!

Page caching friendly

It should be optimized for really high traffic sites as there’s no hole in the caching process (there’s always a cached version and no chance that someone hits a semi-downloaded file). It is page caching friendly as the url to the image won’t change when it’s cached or not (always /images/gravatars/MD5_OR_YOUR_EMAIL.gif).

Warning

You might have guessed that using a different filename for each Gravatar won’t save you any bandwidth. Even if two images are the same, a different symlink will be created for each, causing a new request for each Gravatar (empty or not). I’d suggest not using this plugin if you’re concerned about bandwidth.

Hope this is helpful to anyone else then me. It was extracted from RefactorMyCode.com. Thanks to Daniel (code) and Matthew Hutchinson (code) for inspiration.

Digg it !

Update: I just found out that Gravatar was bought by Automattic, makers of Worpress. Here’s the news from their blog, TechCrunch, Mashable and Techmeme.

About these ads

3 Comments

Filed under rails, refactormycode

3 responses to “Gravatar caching plugin

  1. My Gravatar-Image would not display on refactormycode.com :( My Nickname is “Fu86″ and my gravatar-email is mail@aaron-muelelr.de.

  2. Hey Aaron,

    I see your face now: http://refactormycode.com/users/265

    As you can see by the code of the plugin ^^ it takes some time for the cronjob to run and cache the new gravatars (max 3 h)

    Glad you’re using the site!

  3. I’ve actually created a Gravatar class if you click the link in my name. Loosely coupled and works like a dream – it also has a cache with an expiration date for the avatar – to save on loading times. It can merely load the avatar in locally.

    Adam @ TalkPHP.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s