doc/api/classes/ActionController/Caching/Fragments.html

Summary

Maintainability
Test Coverage
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActionController::Caching::Fragments</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActionController::Caching::Fragments</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/actionpack/lib/action_controller/caching_rb.html">
                vendor/rails/actionpack/lib/action_controller/caching.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Fragment caching is used for caching various blocks within templates
without caching the entire action as a whole. This is useful when certain
elements of an action change frequently or depend on complicated state
while other parts rarely change or can be shared amongst multiple parties.
The caching is doing using the cache helper available in the Action View. A
template with caching might look something like:
</p>
<pre>
  &lt;b&gt;Hello &lt;%= @name %&gt;&lt;/b&gt;
  &lt;% cache do %&gt;
    All the topics in the system:
    &lt;%= render :partial =&gt; &quot;topic&quot;, :collection =&gt; Topic.find(:all) %&gt;
  &lt;% end %&gt;
</pre>
<p>
This cache will bind to the name of action that called it. So you would be
able to invalidate it using <tt><a
href="Fragments.html#M000121">expire_fragment</a>(:controller =&gt;
&quot;topics&quot;, :action =&gt; &quot;list&quot;)</tt> &#8212; if that
was the controller/action used. This is not too helpful if you need to
cache multiple fragments per action or if the action itself is cached using
<tt>caches_action</tt>. So instead we should qualify the name of the action
used with something like:
</p>
<pre>
  &lt;% cache(:action =&gt; &quot;list&quot;, :action_suffix =&gt; &quot;all_topics&quot;) do %&gt;
</pre>
<p>
That would result in a name such as &quot;/topics/list/all_topics&quot;,
which wouldn&#8216;t conflict with any action cache and neither with
another fragment using a different suffix. Note that the URL doesn&#8216;t
have to really exist or be callable. We&#8216;re just using the url_for
system to generate unique cache names that we can refer to later for
expirations. The expiration call for this example would be <tt><a
href="Fragments.html#M000121">expire_fragment</a>(:controller =&gt;
&quot;topics&quot;, :action =&gt; &quot;list&quot;, :action_suffix =&gt;
&quot;all_topics&quot;)</tt>.
</p>
<h2>Fragment stores</h2>
<p>
In order to use the fragment caching, you need to designate where the
caches should be stored. This is done by assigning a fragment store of
which there are four different kinds:
</p>
<ul>
<li>FileStore: Keeps the fragments on disk in the <tt>cache_path</tt>, which
works well for all types of environments and shares the fragments for all
the web server processes running off the same application directory.

</li>
<li>MemoryStore: Keeps the fragments in memory, which is fine for WEBrick and
for FCGI (if you don&#8216;t care that each FCGI process holds its own
fragment store). It&#8216;s not suitable for CGI as the process is thrown
away at the end of each request. It can potentially also take up a lot of
memory since each process keeps all the caches in memory.

</li>
<li>DRbStore: Keeps the fragments in the memory of a separate, shared DRb
process. This works for all environments and only keeps one cache around
for all processes, but requires that you run and manage a separate DRb
process.

</li>
<li>MemCacheStore: Works like DRbStore, but uses Danga&#8216;s MemCache
instead. Requires the ruby-memcache library: gem install ruby-memcache.

</li>
</ul>
<p>
Configuration examples (MemoryStore is the default):
</p>
<pre>
  ActionController::Base.fragment_cache_store = :memory_store
  ActionController::Base.fragment_cache_store = :file_store, &quot;/path/to/cache/directory&quot;
  ActionController::Base.fragment_cache_store = :drb_store, &quot;druby://localhost:9192&quot;
  ActionController::Base.fragment_cache_store = :mem_cache_store, &quot;localhost&quot;
  ActionController::Base.fragment_cache_store = MyOwnStore.new(&quot;parameter&quot;)
</pre>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000118">cache_erb_fragment</a>&nbsp;&nbsp;
      <a href="#M000121">expire_fragment</a>&nbsp;&nbsp;
      <a href="#M000117">fragment_cache_key</a>&nbsp;&nbsp;
      <a href="#M000116">fragment_cache_store=</a>&nbsp;&nbsp;
      <a href="#M000120">read_fragment</a>&nbsp;&nbsp;
      <a href="#M000119">write_fragment</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M000116" class="method-detail">
        <a name="M000116"></a>

        <div class="method-heading">
          <a href="#M000116" class="method-signature">
          <span class="method-name">fragment_cache_store=</span><span class="method-args">(store_option)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000116-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000116-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/caching.rb, line 313</span>
313:           <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">fragment_cache_store=</span>(<span class="ruby-identifier">store_option</span>)
314:             <span class="ruby-identifier">store</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">parameters</span> = <span class="ruby-operator">*</span>([ <span class="ruby-identifier">store_option</span> ].<span class="ruby-identifier">flatten</span>)
315:             <span class="ruby-ivar">@@fragment_cache_store</span> = <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">store</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>)
316:               <span class="ruby-identifier">store_class_name</span> = (<span class="ruby-identifier">store</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">:drb_store</span> <span class="ruby-operator">?</span> <span class="ruby-value str">&quot;DRbStore&quot;</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">store</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">camelize</span>)
317:               <span class="ruby-identifier">store_class</span> = <span class="ruby-constant">ActionController</span><span class="ruby-operator">::</span><span class="ruby-constant">Caching</span><span class="ruby-operator">::</span><span class="ruby-constant">Fragments</span>.<span class="ruby-identifier">const_get</span>(<span class="ruby-identifier">store_class_name</span>)
318:               <span class="ruby-identifier">store_class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">parameters</span>)
319:             <span class="ruby-keyword kw">else</span>
320:               <span class="ruby-identifier">store</span>
321:             <span class="ruby-keyword kw">end</span>
322:           <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000118" class="method-detail">
        <a name="M000118"></a>

        <div class="method-heading">
          <a href="#M000118" class="method-signature">
          <span class="method-name">cache_erb_fragment</span><span class="method-args">(block, name = {}, options = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Called by CacheHelper#cache
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000118-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000118-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/caching.rb, line 331</span>
331:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">cache_erb_fragment</span>(<span class="ruby-identifier">block</span>, <span class="ruby-identifier">name</span> = {}, <span class="ruby-identifier">options</span> = <span class="ruby-keyword kw">nil</span>)
332:         <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">perform_caching</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>; <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">end</span>
333: 
334:         <span class="ruby-identifier">buffer</span> = <span class="ruby-identifier">eval</span>(<span class="ruby-value str">&quot;_erbout&quot;</span>, <span class="ruby-identifier">block</span>.<span class="ruby-identifier">binding</span>)
335: 
336:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">cache</span> = <span class="ruby-identifier">read_fragment</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span>)
337:           <span class="ruby-identifier">buffer</span>.<span class="ruby-identifier">concat</span>(<span class="ruby-identifier">cache</span>)
338:         <span class="ruby-keyword kw">else</span>
339:           <span class="ruby-identifier">pos</span> = <span class="ruby-identifier">buffer</span>.<span class="ruby-identifier">length</span>
340:           <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>
341:           <span class="ruby-identifier">write_fragment</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">buffer</span>[<span class="ruby-identifier">pos</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>], <span class="ruby-identifier">options</span>)
342:         <span class="ruby-keyword kw">end</span>
343:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000121" class="method-detail">
        <a name="M000121"></a>

        <div class="method-heading">
          <a href="#M000121" class="method-signature">
          <span class="method-name">expire_fragment</span><span class="method-args">(name, options = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Name can take one of three forms:
</p>
<ul>
<li>String: This would normally take the form of a path like
&quot;pages/45/notes&quot;

</li>
<li>Hash: Is treated as an implicit call to url_for, like { :controller =&gt;
&quot;pages&quot;, :action =&gt; &quot;notes&quot;, :id =&gt; 45 }

</li>
<li>Regexp: Will destroy all the matched fragments, example:

<pre>
  %r{pages/\d*/notes}
</pre>
<p>
Ensure you do not specify start and finish in the regex (^$) because the
actual filename matched looks like ./cache/filename/path.cache Regexp
expiration is not supported on caches which can&#8216;t iterate over all
keys, such as memcached.
</p>
</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000121-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000121-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/caching.rb, line 373</span>
373:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">expire_fragment</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span> = <span class="ruby-keyword kw">nil</span>)
374:         <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">perform_caching</span>
375: 
376:         <span class="ruby-identifier">key</span> = <span class="ruby-identifier">fragment_cache_key</span>(<span class="ruby-identifier">name</span>)
377: 
378:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">key</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Regexp</span>)
379:           <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">benchmark</span> <span class="ruby-node">&quot;Expired fragments matching: #{key.source}&quot;</span> <span class="ruby-keyword kw">do</span>
380:             <span class="ruby-identifier">fragment_cache_store</span>.<span class="ruby-identifier">delete_matched</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">options</span>)
381:           <span class="ruby-keyword kw">end</span>
382:         <span class="ruby-keyword kw">else</span>
383:           <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">benchmark</span> <span class="ruby-node">&quot;Expired fragment: #{key}&quot;</span> <span class="ruby-keyword kw">do</span>
384:             <span class="ruby-identifier">fragment_cache_store</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">options</span>)
385:           <span class="ruby-keyword kw">end</span>
386:         <span class="ruby-keyword kw">end</span>
387:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000117" class="method-detail">
        <a name="M000117"></a>

        <div class="method-heading">
          <a href="#M000117" class="method-signature">
          <span class="method-name">fragment_cache_key</span><span class="method-args">(name)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000117-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000117-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/caching.rb, line 326</span>
326:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">fragment_cache_key</span>(<span class="ruby-identifier">name</span>)
327:         <span class="ruby-identifier">name</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">url_for</span>(<span class="ruby-identifier">name</span>).<span class="ruby-identifier">split</span>(<span class="ruby-value str">&quot;://&quot;</span>).<span class="ruby-identifier">last</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">name</span>
328:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000120" class="method-detail">
        <a name="M000120"></a>

        <div class="method-heading">
          <a href="#M000120" class="method-signature">
          <span class="method-name">read_fragment</span><span class="method-args">(name, options = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000120-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000120-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/caching.rb, line 355</span>
355:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">read_fragment</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span> = <span class="ruby-keyword kw">nil</span>)
356:         <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">perform_caching</span>
357: 
358:         <span class="ruby-identifier">key</span> = <span class="ruby-identifier">fragment_cache_key</span>(<span class="ruby-identifier">name</span>)
359:         <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">benchmark</span> <span class="ruby-node">&quot;Fragment read: #{key}&quot;</span> <span class="ruby-keyword kw">do</span>
360:           <span class="ruby-identifier">fragment_cache_store</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">options</span>)
361:         <span class="ruby-keyword kw">end</span>
362:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000119" class="method-detail">
        <a name="M000119"></a>

        <div class="method-heading">
          <a href="#M000119" class="method-signature">
          <span class="method-name">write_fragment</span><span class="method-args">(name, content, options = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000119-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000119-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/caching.rb, line 345</span>
345:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">write_fragment</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">content</span>, <span class="ruby-identifier">options</span> = <span class="ruby-keyword kw">nil</span>)
346:         <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">perform_caching</span>
347: 
348:         <span class="ruby-identifier">key</span> = <span class="ruby-identifier">fragment_cache_key</span>(<span class="ruby-identifier">name</span>)
349:         <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">benchmark</span> <span class="ruby-node">&quot;Cached fragment: #{key}&quot;</span> <span class="ruby-keyword kw">do</span>
350:           <span class="ruby-identifier">fragment_cache_store</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">content</span>, <span class="ruby-identifier">options</span>)
351:         <span class="ruby-keyword kw">end</span>
352:         <span class="ruby-identifier">content</span>
353:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>