<?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; railsbench</title>
	<atom:link href="http://rel.me/t/railsbench/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>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>

