<?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>rel=me &#187; cache</title>
	<atom:link href="http://rel.me/t/cache/feed/" rel="self" type="application/rss+xml" />
	<link>http://rel.me</link>
	<description>programming, objective-c, cocoa, iphone, c</description>
	<lastBuildDate>Wed, 01 Feb 2012 07:26:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>nginx conf with alternate cache dir</title>
		<link>http://rel.me/2008/01/07/nginx-conf-with-alternate-cache-dir/</link>
		<comments>http://rel.me/2008/01/07/nginx-conf-with-alternate-cache-dir/#comments</comments>
		<pubDate>Mon, 07 Jan 2008 07:25:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">/2008/01/07/nginx-conf-with-alternate-cache-dir</guid>
		<description><![CDATA[Its a good idea to use an alternate cache directory in rails (in your environment.rb): config.action_controller.page_cache_directory = RAILS_ROOT + &#34;/public/cache/&#34; This makes it easier to sweep. To setup nginx to pick up from a rails alternate cache directory like public/cache, I used: if ($http_user_agent ~* &#34;(iPhone&#124;iPod)&#34;) { rewrite ^/$ /iphone break; proxy_pass http://mongrel-pt; break; } [...]]]></description>
			<content:encoded><![CDATA[<p>Its a good idea to use an alternate cache directory in rails (in your environment.rb):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">config.<span class="me1">action_controller</span>.<span class="me1">page_cache_directory</span> = RAILS_ROOT <span class="sy0">+</span> <span class="st0">&quot;/public/cache/&quot;</span></pre></div></div>

<p>This makes it easier to sweep. To setup nginx to pick up from a rails alternate cache directory like public/cache, I used:</p>

<div class="wp_syntax"><div class="code"><pre class="text">if ($http_user_agent ~* &quot;(iPhone|iPod)&quot;) {
  rewrite ^/$ /iphone break;
  proxy_pass http://mongrel-pt;
  break;
}
&nbsp;
if (-f $request_filename) {
  break;
}
&nbsp;
if (-f $document_root/cache/$uri/index.html) {
  rewrite (.*) /cache/$1/index.html break;
}
&nbsp;
if (-f $document_root/cache/$uri.html) {
  rewrite (.*) /cache/$1.html break;
}
&nbsp;
if (-f $document_root/cache/$uri) {
  rewrite (.*) /cache/$1 break;
}
&nbsp;
if (!-f $request_filename) {
  proxy_pass http://mongrel-pt;
  break;
}</pre></div></div>

<p>It was a little tricky to figure out to use the $uri variable. The first rewrite is used for iphone requests so it doesn&#8217;t get the non-iphone cached page.</p>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2008/01/07/nginx-conf-with-alternate-cache-dir/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Fragment Cache Store with TTL</title>
		<link>http://rel.me/2007/02/21/fragment-cache-store-with-ttl/</link>
		<comments>http://rel.me/2007/02/21/fragment-cache-store-with-ttl/#comments</comments>
		<pubDate>Wed, 21 Feb 2007 05:26:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[railsbench]]></category>

		<guid isPermaLink="false">/2007/09/03/fragment-cache-store-with-ttl</guid>
		<description><![CDATA[I wanted to use a fragment cache, but I didn&#8217;t want to deal with expiring it. So I just made a self-expiring cache based on a TTL. The fragment cache store just wants 4 methods: read, write, delete, delete_matched 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to use a fragment cache, but I didn&#8217;t want to deal with expiring it. So I just made a self-expiring cache based on a TTL.<br/><br />
The fragment cache store just wants 4 methods: <code>read, write, delete, delete_matched</code></p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt>15<tt>
</tt>16<tt>
</tt>17<tt>
</tt>18<tt>
</tt>19<tt>
</tt><strong>20</strong><tt>
</tt>21<tt>
</tt>22<tt>
</tt>23<tt>
</tt>24<tt>
</tt>25<tt>
</tt>26<tt>
</tt>27<tt>
</tt>28<tt>
</tt>29<tt>
</tt><strong>30</strong><tt>
</tt>31<tt>
</tt>32<tt>
</tt>33<tt>
</tt>34<tt>
</tt>35<tt>
</tt>36<tt>
</tt>37<tt>
</tt>38<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="r">class</span> <span class="cl">Cache::MemStore</span><tt>
</tt><tt>
</tt>  attr_reader <span class="sy">:ttl</span>, <span class="sy">:auto_expire</span><tt>
</tt>    <tt>
</tt>  <span class="r">def</span> <span class="fu">initialize</span>(options = {})<tt>
</tt>    <span class="iv">@ttl</span> = options[<span class="sy">:ttl</span>] || <span class="i">10</span>.minutes<tt>
</tt>    <span class="iv">@cache</span> = {}<tt>
</tt>  <span class="r">end</span><tt>
</tt>    <tt>
</tt>  <span class="r">def</span> <span class="fu">expire</span>(pattern)<tt>
</tt>    <span class="iv">@cache</span>.each { |key, val| delete key <span class="r">if</span> key =~ pattern }<tt>
</tt>  <span class="r">end</span><tt>
</tt>  <tt>
</tt>  <span class="r">def</span> <span class="fu">read</span>(key, options)<tt>
</tt>    delete(key, options) <span class="r">and</span> <span class="r">return</span> <span class="r">if</span> is_expired?(key)<tt>
</tt>    <span class="r">return</span> <span class="iv">@cache</span>[key][<span class="sy">:content</span>] <span class="r">if</span> <span class="iv">@cache</span>.has_key? key<tt>
</tt>  <span class="r">end</span><tt>
</tt>  <tt>
</tt>  <span class="r">def</span> <span class="fu">write</span>(key, content, options)<tt>
</tt>    <span class="iv">@cache</span>[key] = { <span class="sy">:content</span> =&gt; content, <span class="sy">:created_on</span> =&gt; <span class="co">Time</span>.now }<tt>
</tt>  <span class="r">end</span><tt>
</tt>  <tt>
</tt>  <span class="r">def</span> <span class="fu">delete_matched</span>(pattern, options)<tt>
</tt>    expire pattern<tt>
</tt>  <span class="r">end</span><tt>
</tt>  <tt>
</tt>  <span class="r">def</span> <span class="fu">delete</span>(key, options)<tt>
</tt>    <span class="iv">@cache</span>.delete key<tt>
</tt>  <span class="r">end</span><tt>
</tt>  <tt>
</tt>  <span class="r">def</span> <span class="fu">is_expired?</span>(key)<tt>
</tt>    <span class="r">if</span> <span class="iv">@cache</span>.has_key? key<tt>
</tt>      created_on = <span class="iv">@cache</span>[key][<span class="sy">:created_on</span>]<tt>
</tt>      <span class="r">return</span> <span class="co">Time</span>.now &gt; (created_on + <span class="iv">@ttl</span>)<tt>
</tt>    <span class="r">end</span><tt>
</tt>  <span class="r">end</span><tt>
</tt>  <tt>
</tt><span class="r">end</span></pre>
</td>
</tr>
</table>
<p>Then in your <code>environment.rb</code> put: </p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="co">ActionController</span>::<span class="co">Base</span>.fragment_cache_store = <span class="co">Cache</span>::<span class="co">MemStore</span>.new(<span class="sy">:ttl</span> =&gt; <span class="i">10</span>.minutes)</pre>
</td>
</tr>
</table>
<p>railsbench gave me the following results:</p>
<pre>
page request                                      total  stddev%     r/s    ms/r
with cache: /                                   7.23471   2.8012   13.82   72.35
nocache:    /                                  25.81057   0.6966    3.87  258.11
</pre>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2007/02/21/fragment-cache-store-with-ttl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

