<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Robert Dorrian &#187; programming</title>
	<atom:link href="http://blog.ten-24.co.uk/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ten-24.co.uk</link>
	<description>Computers, Coffee and Pedantry</description>
	<lastBuildDate>Sun, 07 Aug 2011 13:41:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Gravatar Cache</title>
		<link>http://blog.ten-24.co.uk/2009/05/10/gravatar-cache/</link>
		<comments>http://blog.ten-24.co.uk/2009/05/10/gravatar-cache/#comments</comments>
		<pubDate>Sun, 10 May 2009 21:51:17 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[gravatar]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.ten-24.co.uk/?p=39</guid>
		<description><![CDATA[Implementing Gravatar powered avatars is as simple as calculating a MD5 hash of the e-mail and loading the remote image at http://gravatar.com/avatar/[hash]. I needed a bespoke caching gravatar system, and decided to follow this simple approach: Store all avatars as [hash].jpg If [hash].jpg exists, and was created within a defined freshness period &#8211; use the [...]]]></description>
			<content:encoded><![CDATA[<p>Implementing <a href="http://gravatar.com">Gravatar</a> powered avatars is as simple as calculating a MD5 hash of the e-mail and loading the remote image at http://gravatar.com/avatar/<em>[hash]</em>.</p>
<p>I needed a bespoke caching gravatar system, and decided to follow this simple approach:</p>
<ul>
<li>Store all avatars as <em>[hash]</em>.jpg</li>
<li>If <em>[hash]</em>.jpg exists, and was created within a defined freshness period &#8211; use the stored avatar</li>
<li>Otherwise, load a new avatar from gravatar.</li>
</ul>
<p>This method allows batch updating of avatars, which is useful for background batch jobs.
</p>
<p>Desktop apps can use this as a local cache of images; while web apps can avoid extra external traffic.</p>
<p>My first attempt in ruby:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'open-uri'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'md5'</span>
&nbsp;
<span style="color:#ff6633; font-weight:bold;">$size</span>=<span style="color:#996600;">&quot;200&quot;</span>
<span style="color:#ff6633; font-weight:bold;">$default</span>=<span style="color:#996600;">&quot;identicon&quot;</span>
<span style="color:#ff6633; font-weight:bold;">$days</span>=<span style="color:#006666;">7</span>
<span style="color:#ff6633; font-weight:bold;">$dir</span>=<span style="color:#996600;">&quot;avatar&quot;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> fill_gravatar_cache<span style="color:#006600; font-weight:bold;">&#40;</span>email = null<span style="color:#006600; font-weight:bold;">&#41;</span>
  cacheTime = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">60</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#006666;">60</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#006666;">24</span><span style="color:#006600; font-weight:bold;">*</span>$days<span style="color:#006600; font-weight:bold;">&#41;</span>
  email.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>email<span style="color:#006600; font-weight:bold;">|</span>
    hash = MD5.<span style="color:#9900CC;">md5</span><span style="color:#006600; font-weight:bold;">&#40;</span>email<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_s</span>
    fileName = <span style="color:#ff6633; font-weight:bold;">$dir</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">&quot;/&quot;</span><span style="color:#006600; font-weight:bold;">+</span>hash<span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">&quot;.jpg&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">not</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>fileName<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">and</span>
             <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">ctime</span><span style="color:#006600; font-weight:bold;">&#40;</span>fileName<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&gt;</span> cacheTime<span style="color:#006600; font-weight:bold;">&#41;</span>
      url = <span style="color:#996600;">&quot;http://gravatar.com/avatar/&quot;</span> <span style="color:#006600; font-weight:bold;">+</span>
             hash <span style="color:#006600; font-weight:bold;">+</span>
             <span style="color:#996600;">&quot;?s=&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#ff6633; font-weight:bold;">$size</span> <span style="color:#006600; font-weight:bold;">+</span>
             <span style="color:#996600;">&quot;&amp;d=&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#ff6633; font-weight:bold;">$default</span>
      <span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>$dir<span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">&quot;/&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> hash <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;.jpg&quot;</span>,<span style="color:#996600;">&quot;wb&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.
        <span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>url<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>
Thanks to auto un-boxing we can use a string or an array of strings. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">email = <span style="color:#996600;">&quot;an-email@address&quot;</span>
email = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;more&quot;</span>,<span style="color:#996600;">&quot;than&quot;</span>,<span style="color:#996600;">&quot;one&quot;</span>,<span style="color:#996600;">&quot;email&quot;</span>,<span style="color:#996600;">&quot;address&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

</p>
<p>Planned extensions:</p>
<ul>
<li>Update all avatars in the cache</li>
<li>Delete avatars that haven&#8217;t been used in a while</li>
<li>Store more than one size</li>
<li>Java and Flex implementations</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.ten-24.co.uk/2009/05/10/gravatar-cache/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

