  
<div id="snippet_302" class="snippet">
  <h2><a href="/snippets/302-How-to-setup-and-use-Rack-Cache-with-Rails-2-3" title="How to setup and use Rack::Cache with Rails 2.3 - Ruby - rack, rack::cache, cache, rails, 2.3">How to setup and use Rack::Cache with Rails 2.3</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/124-Ruby">
      Ruby</a> posted about 1 year ago by christian
          
  </div>

  <div class="body">
    <p>First install rack-cache:</p>
<p><pre class="active4d"><span class="line-numbers">   1 </span> sudo gem install rack<span class="Operator">-</span>cache
</pre></p>
<p>Install Rails version 2.3 or greater:</p>
<p><pre class="active4d"><span class="line-numbers">   1 </span> sudo gem install rails
</pre></p>
<p>Or, freeze your project to Rails edge:</p>
<p><pre class="active4d"><span class="line-numbers">   1 </span> cd project
<span class="line-numbers">   2 </span> rake rails<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>freeze</span><span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>edge</span>
</pre></p>
<p>Add this to config/environment.rb:<br />
<pre class="active4d"><span class="line-numbers">   1 </span> config.<span class="FunctionName">middleware</span>.<span class="FunctionName">use</span> <span class="LibraryClassType">Rack</span>::<span class="FunctionName">Cache</span>,
<span class="line-numbers">   2 </span>   <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>verbose</span> =&gt; <span class="BuiltInConstant">true</span>,
<span class="line-numbers">   3 </span>   <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>metastore</span>   =&gt; <span class="String"><span class="String">'</span>file:/var/cache/rack/meta<span class="String">'</span></span>,
<span class="line-numbers">   4 </span>   <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>entitystore</span> =&gt; <span class="String"><span class="String">'</span>file:/var/cache/rack/body<span class="String">'</span></span>
</pre></p>
<p>Note the name is middleware, not middlewares which is used in all examples I found online, even the Rails blog.</p>
<p>Check the Rack configuration:<br />
<pre class="active4d"><span class="line-numbers">   1 </span> rake middleware
</pre></p>
<p>You should see something like this:<br />
<pre class="active4d"><span class="line-numbers">   1 </span> use <span class="LibraryClassType">Rack</span>::<span class="FunctionName">Lock</span>
<span class="line-numbers">   2 </span> use <span class="LibraryClassType">ActionController</span>::<span class="FunctionName">Failsafe</span>
<span class="line-numbers">   3 </span> use <span class="LibraryClassType">ActionController</span>::<span class="FunctionName">Reloader</span>
<span class="line-numbers">   4 </span> use <span class="LibraryClassType">ActionController</span>::<span class="FunctionName">Session</span>::<span class="FunctionName">CookieStore</span>, <span class="LineComment"><span class="LineComment">#</span>&lt;Proc:0x00002b45ab39e3a8@(eval):8&gt;</span>
<span class="line-numbers">   5 </span> use <span class="LibraryClassType">ActionController</span>::<span class="FunctionName">RewindableInput</span>
<span class="line-numbers">   6 </span> use <span class="LibraryClassType">ActionController</span>::<span class="FunctionName">ParamsParser</span>
<span class="line-numbers">   7 </span> use <span class="LibraryClassType">Rack</span>::<span class="FunctionName">MethodOverride</span>
<span class="line-numbers">   8 </span> use <span class="LibraryClassType">Rack</span>::<span class="FunctionName">Head</span>
<span class="line-numbers">   9 </span> use <span class="LibraryClassType">Rack</span>::<span class="FunctionName">Cache</span>, {<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>metastore</span>=&gt;<span class="String"><span class="String">&quot;</span>file:/var/cache/rack/meta<span class="String">&quot;</span></span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>entitystore</span>=&gt;<span class="String"><span class="String">&quot;</span>file:/var/cache/rack/body<span class="String">&quot;</span></span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>verbose</span>=&gt;<span class="BuiltInConstant">true</span>}
<span class="line-numbers">  10 </span> use <span class="LibraryClassType">ActiveRecord</span>::<span class="FunctionName">QueryCache</span>
<span class="line-numbers">  11 </span> run <span class="LibraryClassType">ActionController</span>::<span class="FunctionName">Dispatcher</span>.<span class="FunctionName">new</span>
</pre></p>
<p>Tell Rack to cache data by putting this in your controller:</p>
<p><pre class="active4d"><span class="line-numbers">   1 </span> expires_in <span class="Number">5</span>.<span class="FunctionName">minutes</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>public</span> =&gt; <span class="BuiltInConstant">true</span>
</pre></p>
<p>Note that you should avoid caching private data when the user is signed in. In this case you should set the Cache-Control header to private or completely avoid using expires_in:</p>
<p><pre class="active4d"><span class="line-numbers">   1 </span> expires_in <span class="Number">5</span>.<span class="FunctionName">minutes</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>public</span> =&gt; <span class="BuiltInConstant">true</span> <span class="Keyword">if</span> <span class="Operator">!</span>signed_in?
</pre></p>
<p>Rails sets cache-control to private by default, Rack needs public content.</p>
<p>With verbose set to true, you&#8217;ll see this in the thin logs:<br />
<pre class="active4d"><span class="line-numbers">   1 </span> [cache] trace: cache miss
<span class="line-numbers">   2 </span> [cache] trace: fetching response from backend
<span class="line-numbers">   3 </span> [cache] trace: store backend response <span class="Keyword">in</span> cache (ttl: 300s)
<span class="line-numbers">   4 </span> [cache] trace: storing response <span class="Keyword">in</span> cache
<span class="line-numbers">   5 </span> [cache] trace: delivering response ...
<span class="line-numbers">   6 </span> [cache] trace: cache hit (ttl: 276s)
<span class="line-numbers">   7 </span> [cache] trace: delivering response ...
</pre></p>
<p>Note that ActionController::AbstractRequest has been renamed to ActionController::Request in Rails 2.3.0, so some plugins might throw this error in your face:<br />
<pre class="active4d"><span class="line-numbers">   1 </span> load_missing_constant<span class="String"><span class="String">'</span>: uninitialized constant ActionController::AbstractRequest</span>
</pre></p>
<h2>Problems</h2>
<p>If you get one of these errors you need to change the way you require the rack-cache gem:<br />
<pre class="active4d"><span class="line-numbers">   1 </span> uninitialized constant <span class="LibraryClassType">Rack</span>::<span class="FunctionName">Cache</span>
<span class="line-numbers">   2 </span> 
<span class="line-numbers">   3 </span> uninitialized constant <span class="LibraryClassType">Rails</span>::<span class="FunctionName">Rack</span>::<span class="FunctionName">Cache</span>
</pre></p>
I&#8217;ve found that this doesn&#8217;t work:
<p><pre class="active4d"><span class="line-numbers">   1 </span> config.<span class="FunctionName">gem</span> <span class="String"><span class="String">&quot;</span>rack/cache<span class="String">&quot;</span></span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>lib</span> =&gt; <span class="String"><span class="String">'</span>rack/cache<span class="String">'</span></span>
</pre></p>
<p>Instead, I&#8217;m requiring rack-cache by adding this to environment.rb:</p>
<p><pre class="active4d"><span class="line-numbers">   1 </span> <span class="Keyword">require</span> <span class="String"><span class="String">'</span>rack-cache<span class="String">'</span></span>
<span class="line-numbers">   2 </span> 
<span class="line-numbers">   3 </span> 
<span class="line-numbers">   4 </span> <span class="LibraryClassType">Rails</span>::<span class="FunctionName">Initializer</span>.<span class="FunctionName">run</span> <span class="Keyword">do </span>|<span class="Variable">config</span>|
</pre></p>
<h2>References</h2>
<p><a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1"><span class="caps">HTTP</span> <span class="caps">RFC</span> &#8211; Cache-Control</a><br />
<a href="http://tomayko.com/src/rack-cache/api/classes/Rack/Cache/Options.html">Rack::Cache options documentation</a><br />
<a href="http://api.rubyonrails.org/classes/ActionController/Base.html#M000666">Rails expires_in documentation</a></p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/639-rack">rack</a>, <a href="/tags/663-rack-cache">rack::cache</a>, <a href="/tags/323-cache">cache</a>, <a href="/tags/9-rails">rails</a>, <a href="/tags/731-2-3">2.3</a>
    
    
  </div>
</div>



  
<div id="snippet_126" class="snippet">
  <h2><a href="/snippets/126-How-to-cache-PHP-objects-to-disk-with-CakePHP" title="How to cache PHP objects to disk with CakePHP - PHP - tag, cloud, cache, ttl">How to cache PHP objects to disk with CakePHP</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/102-PHP">
      PHP</a> posted over 2 years ago by christian
          
  </div>

  <div class="body">
    <p>CakePHP has a view cache (similar to Rails) that can be used to cache objects.  The following snippet shows a CakePHP action that uses the serialize and unserialize functions to cache a tag cloud&#8212;an array containing tags in this case&#8212;to disk, and then read it back.</p>


	<p>Note that we assign a  TTL  of 1 hours to the tag cloud, so if it&#8217;s more than one hour old it will be refreshed from the database.</p>


	<p><pre class="active4d"><span class="line-numbers">   1 </span> function index()
<span class="line-numbers">   2 </span> 	{
<span class="line-numbers">   3 </span> 		$maximum = 100;
<span class="line-numbers">   4 </span> 		$cache_key = &quot;tag_cloud_$maximum&quot;;
<span class="line-numbers">   5 </span> 		$tag_cloud = cache($cache_key, null, '+1 hours');
<span class="line-numbers">   6 </span> 		
<span class="line-numbers">   7 </span> 		if(empty($tag_cloud))
<span class="line-numbers">   8 </span> 		{
<span class="line-numbers">   9 </span> 			$tag_cloud = Tag::generate_cloud($maximum);
<span class="line-numbers">  10 </span> 			cache($cache_key, serialize($tag_cloud));
<span class="line-numbers">  11 </span> 		}
<span class="line-numbers">  12 </span> 		else
<span class="line-numbers">  13 </span> 		{
<span class="line-numbers">  14 </span> 			$tag_cloud = unserialize($tag_cloud);
<span class="line-numbers">  15 </span> 		}
<span class="line-numbers">  16 </span> 		
<span class="line-numbers">  17 </span> 		return $tag_cloud;
<span class="line-numbers">  18 </span> 	}
</pre></p>


	<p>The cache function is defined in $APP_ROOT/cake/basics.php, which is where you should look if you want to know more about how the caching works&#8230;</p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/321-tag">tag</a>, <a href="/tags/322-cloud">cloud</a>, <a href="/tags/323-cache">cache</a>, <a href="/tags/324-ttl">ttl</a>
    
    
  </div>
</div>






