Robby on Rails: Setting Akamai Edge-Control headers with Ruby on Railsthoughts.sort_by{|t| t[:topic]}.collect tag:www.robbyonrails.com,2005:TypoTypo2012-06-19T00:56:31-04:00Robby Russellurn:uuid:029e2f64-44bd-4b0b-89be-c669171f18072012-06-19T00:54:00-04:002012-06-19T00:56:31-04:00Setting Akamai Edge-Control headers with Ruby on Rails<p>Just a short and sweet little tip.</p>
<p>Several months ago we moved one of our clients over to Akamai’s Content Delivery Network (CDN). Ww were previously using a combination of Amazon S3 and CloudFront with <em>some</em> benefits, but we were finding several key areas of the world were not s covered by Amazon (yet) for asset delivery. Along with that, we really wanted to take advantage of the <span class="caps">CDN</span> for more of our <span class="caps">HTML</span> content with a lot of complex rules that related to geo-targeting and regionalization of content.</p>
<p>I’ll try to cover those topics in another post, but wanted to share a few tidbits of code that we are using to manage Akamai’s Edge-control caches from within our Rails application.</p>
<p>With Akamai, we’re able to tell their Edge servers whether it should hold on to the response so it can try to avoid an extra request to the origin (aka our Rails application). From Rails, we just added a few helper methods to our controllers so that we can litter our application with various expiration times.</p>
<div class="typocode"><pre><code class="typocode_ruby "> <span class="comment"># Sets the headers for Akamai</span>
<span class="comment"># acceptable formats include:</span>
<span class="comment"># 1m, 10m, 90m, 2h, 5d</span>
<span class="keyword">def </span><span class="method">set_cache_control_for</span><span class="punct">(</span><span class="ident">maxage</span><span class="punct">="</span><span class="string">20m</span><span class="punct">")</span>
<span class="ident">headers</span><span class="punct">['</span><span class="string">Edge-control</span><span class="punct">']</span> <span class="punct">=</span> <span class="punct">"</span><span class="string">!no-store, max-age=<span class="expr">#{maxage}</span></span><span class="punct">"</span>
<span class="keyword">end</span></code></pre></div>
<p>This allows us to do things like:</p>
<div class="typocode"><pre><code class="typocode_ruby "> <span class="keyword">class </span><span class="class">ProductsController</span> <span class="punct"><</span> <span class="constant">ApplicationController</span>
<span class="keyword">def </span><span class="method">show</span>
<span class="ident">set_cache_control_for</span><span class="punct">('</span><span class="string">4h</span><span class="punct">')</span>
<span class="attribute">@product</span> <span class="punct">=</span> <span class="constant">Product</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="ident">params</span><span class="punct">[</span><span class="symbol">:id</span><span class="punct">])</span>
<span class="keyword">end</span>
<span class="keyword">end</span></code></pre></div>
<p>Then when Akamai gets a request for <code>http://domain.com/products/20-foo-bar</code>, it’ll try to keep a cached copy around for four hours before it hits our server again.</p><p>Just a short and sweet little tip.</p>
<p>Several months ago we moved one of our clients over to Akamai’s Content Delivery Network (CDN). Ww were previously using a combination of Amazon S3 and CloudFront with <em>some</em> benefits, but we were finding several key areas of the world were not s covered by Amazon (yet) for asset delivery. Along with that, we really wanted to take advantage of the <span class="caps">CDN</span> for more of our <span class="caps">HTML</span> content with a lot of complex rules that related to geo-targeting and regionalization of content.</p>
<p>I’ll try to cover those topics in another post, but wanted to share a few tidbits of code that we are using to manage Akamai’s Edge-control caches from within our Rails application.</p>
<p>With Akamai, we’re able to tell their Edge servers whether it should hold on to the response so it can try to avoid an extra request to the origin (aka our Rails application). From Rails, we just added a few helper methods to our controllers so that we can litter our application with various expiration times.</p>
<div class="typocode"><pre><code class="typocode_ruby "> <span class="comment"># Sets the headers for Akamai</span>
<span class="comment"># acceptable formats include:</span>
<span class="comment"># 1m, 10m, 90m, 2h, 5d</span>
<span class="keyword">def </span><span class="method">set_cache_control_for</span><span class="punct">(</span><span class="ident">maxage</span><span class="punct">="</span><span class="string">20m</span><span class="punct">")</span>
<span class="ident">headers</span><span class="punct">['</span><span class="string">Edge-control</span><span class="punct">']</span> <span class="punct">=</span> <span class="punct">"</span><span class="string">!no-store, max-age=<span class="expr">#{maxage}</span></span><span class="punct">"</span>
<span class="keyword">end</span></code></pre></div>
<p>This allows us to do things like:</p>
<div class="typocode"><pre><code class="typocode_ruby "> <span class="keyword">class </span><span class="class">ProductsController</span> <span class="punct"><</span> <span class="constant">ApplicationController</span>
<span class="keyword">def </span><span class="method">show</span>
<span class="ident">set_cache_control_for</span><span class="punct">('</span><span class="string">4h</span><span class="punct">')</span>
<span class="attribute">@product</span> <span class="punct">=</span> <span class="constant">Product</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="ident">params</span><span class="punct">[</span><span class="symbol">:id</span><span class="punct">])</span>
<span class="keyword">end</span>
<span class="keyword">end</span></code></pre></div>
<p>Then when Akamai gets a request for <code>http://domain.com/products/20-foo-bar</code>, it’ll try to keep a cached copy around for four hours before it hits our server again.</p>