PageRenderTime 28ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/ability-list-inc/abilitylist
HTML | 361 lines | 223 code | 135 blank | 3 comment | 0 complexity | 18074a307818cf8cbbcd31504ba97160 MD5 | raw file
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5. <head>
  6. <title>ActionController::Caching::Actions</title>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8. <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
  9. <link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
  10. <link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
  11. <script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
  12. <script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
  13. <script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
  14. <script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>
  15. </head>
  16. <body>
  17. <div class="banner">
  18. <span>Ruby on Rails 4.2.0</span><br />
  19. <h1>
  20. <span class="type">Module</span>
  21. ActionController::Caching::Actions
  22. </h1>
  23. <ul class="files">
  24. <li><a href="../../../files/__/__/_rvm/gems/ruby-2_2_0/bundler/gems/actionpack-action_caching-30ae10cba0f0/lib/action_controller/caching/actions_rb.html">/Users/az/.rvm/gems/ruby-2.2.0/bundler/gems/actionpack-action_caching-30ae10cba0f0/lib/action_controller/caching/actions.rb</a></li>
  25. </ul>
  26. </div>
  27. <div id="bodyContent">
  28. <div id="content">
  29. <div class="description">
  30. <p>Action caching is similar to page caching by the fact that the entire
  31. output of the response is cached, but unlike page caching, every request
  32. still goes through Action Pack. The key benefit of this is that filters run
  33. before the cache is served, which allows for authentication and other
  34. restrictions on whether someone is allowed to execute such action.</p>
  35. <pre><code>class ListsController &lt; ApplicationController
  36. before_filter :authenticate, except: :public
  37. caches_page :public
  38. caches_action :index, :show
  39. end
  40. </code></pre>
  41. <p>In this example, the <code>public</code> action doesn&#39;t require
  42. authentication so it&#39;s possible to use the faster page caching. On the
  43. other hand <code>index</code> and <code>show</code> require authentication.
  44. They can still be cached, but we need action caching for them.</p>
  45. <p>Action caching uses fragment caching internally and an around filter to do
  46. the job. The fragment cache is named according to the host and path of the
  47. request. A page that is accessed at
  48. <code>http://david.example.com/lists/show/1</code> will result in a
  49. fragment named <code>david.example.com/lists/show/1</code>. This allows the
  50. cacher to differentiate between <code>david.example.com/lists/</code> and
  51. <code>jamis.example.com/lists/</code> which is a helpful way of assisting
  52. the subdomain-as-account-key pattern.</p>
  53. <p>Different representations of the same resource, e.g.
  54. <code>http://david.example.com/lists</code> and
  55. <code>http://david.example.com/lists.xml</code> are treated like separate
  56. requests and so are cached separately. Keep in mind when expiring an action
  57. cache that <code>action: &#39;lists&#39;</code> is not the same as
  58. <code>action: &#39;lists&#39;, format: :xml</code>.</p>
  59. <p>You can modify the default action cache path by passing a
  60. <code>:cache_path</code> option. This will be passed directly to
  61. <code>ActionCachePath.new</code>. This is handy for actions with multiple
  62. possible routes that should be cached differently. If a block is given, it
  63. is called with the current controller instance. If an object that responds
  64. to <code>call</code> is given, it&#39;ll be called with the current
  65. controller instance.</p>
  66. <p>And you can also use <code>:if</code> (or <code>:unless</code>) to pass a
  67. proc that specifies when the action should be cached.</p>
  68. <p>As of Rails 3.0, you can also pass <code>:expires_in</code> with a time
  69. interval (in seconds) to schedule expiration of the cached item.</p>
  70. <p>The following example depicts some of the points made above:</p>
  71. <pre><code>class CachePathCreator
  72. def initialize(name)
  73. @name = name
  74. end
  75. def call(controller)
  76. &quot;cache-path-#{@name}&quot;
  77. end
  78. end
  79. class ListsController &lt; ApplicationController
  80. before_filter :authenticate, except: :public
  81. caches_page :public
  82. caches_action :index, if: Proc.new do
  83. !request.format.json? # cache if is not a JSON request
  84. end
  85. caches_action :show, cache_path: { project: 1 },
  86. expires_in: 1.hour
  87. caches_action :feed, cache_path: Proc.new do
  88. if params[:user_id]
  89. user_list_url(params[:user_id, params[:id])
  90. else
  91. list_url(params[:id])
  92. end
  93. end
  94. caches_action :posts, cache_path: CachePathCreator.new(&#39;posts&#39;)
  95. end</code></pre>
  96. <p>If you pass <code>layout: false</code>, it will only cache your action
  97. content. That&#39;s useful when your layout has dynamic information.</p>
  98. <p>Warning: If the format of the request is determined by the Accept HTTP
  99. header the Content-Type of the cached response could be wrong because no
  100. information about the MIME type is stored in the cache key. So, if you
  101. first ask for MIME type M in the Accept header, a cache entry is created,
  102. and then perform a second request to the same resource asking for a
  103. different MIME type, you&#39;d get the content cached for M.</p>
  104. <p>The <code>:format</code> parameter is taken into account though. The safest
  105. way to cache by MIME type is to pass the format in the route.</p>
  106. </div>
  107. <!-- Namespace -->
  108. <div class="sectiontitle">Namespace</div>
  109. <ul>
  110. <li>
  111. <span class="type">MODULE</span>
  112. <a href="Actions/ClassMethods.html">ActionController::Caching::Actions::ClassMethods</a>
  113. </li>
  114. <li>
  115. <span class="type">CLASS</span>
  116. <a href="Actions/ActionCachePath.html">ActionController::Caching::Actions::ActionCachePath</a>
  117. </li>
  118. </ul>
  119. <!-- Method ref -->
  120. <div class="sectiontitle">Methods</div>
  121. <dl class="methods">
  122. <dt>#</dt>
  123. <dd>
  124. <ul>
  125. <li>
  126. <a href="#method-i-_save_fragment">_save_fragment</a>
  127. </li>
  128. </ul>
  129. </dd>
  130. <dt>C</dt>
  131. <dd>
  132. <ul>
  133. <li>
  134. <a href="#method-i-caching_allowed-3F">caching_allowed?</a>
  135. </li>
  136. </ul>
  137. </dd>
  138. <dt>E</dt>
  139. <dd>
  140. <ul>
  141. <li>
  142. <a href="#method-i-expire_action">expire_action</a>
  143. </li>
  144. </ul>
  145. </dd>
  146. </dl>
  147. <!-- Methods -->
  148. <div class="sectiontitle">Instance Public methods</div>
  149. <div class="method">
  150. <div class="title method-title" id="method-i-_save_fragment">
  151. <b>_save_fragment</b>(name, options)
  152. <a href="../../../classes/ActionController/Caching/Actions.html#method-i-_save_fragment" name="method-i-_save_fragment" class="permalink">Link</a>
  153. </div>
  154. <div class="description">
  155. </div>
  156. <div class="sourcecode">
  157. <p class="source-link">
  158. Source:
  159. <a href="javascript:toggleSource('method-i-_save_fragment_source')" id="l_method-i-_save_fragment_source">show</a>
  160. </p>
  161. <div id="method-i-_save_fragment_source" class="dyn-source">
  162. <pre><span class="ruby-comment"># File ../../.rvm/gems/ruby-2.2.0/bundler/gems/actionpack-action_caching-30ae10cba0f0/lib/action_controller/caching/actions.rb, line 120</span>
  163. <span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">_save_fragment</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span>)
  164. <span class="ruby-identifier">content</span> = <span class="ruby-string">&#39;&#39;</span>
  165. <span class="ruby-identifier">response_body</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">parts</span><span class="ruby-operator">|</span>
  166. <span class="ruby-identifier">content</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">parts</span>
  167. <span class="ruby-keyword">end</span>
  168. <span class="ruby-keyword">if</span> <span class="ruby-identifier">caching_allowed?</span>
  169. <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>)
  170. <span class="ruby-keyword">else</span>
  171. <span class="ruby-identifier">content</span>
  172. <span class="ruby-keyword">end</span>
  173. <span class="ruby-keyword">end</span></pre>
  174. </div>
  175. </div>
  176. </div>
  177. <div class="method">
  178. <div class="title method-title" id="method-i-caching_allowed-3F">
  179. <b>caching_allowed?</b>()
  180. <a href="../../../classes/ActionController/Caching/Actions.html#method-i-caching_allowed-3F" name="method-i-caching_allowed-3F" class="permalink">Link</a>
  181. </div>
  182. <div class="description">
  183. </div>
  184. <div class="sourcecode">
  185. <p class="source-link">
  186. Source:
  187. <a href="javascript:toggleSource('method-i-caching_allowed-3F_source')" id="l_method-i-caching_allowed-3F_source">show</a>
  188. </p>
  189. <div id="method-i-caching_allowed-3F_source" class="dyn-source">
  190. <pre><span class="ruby-comment"># File ../../.rvm/gems/ruby-2.2.0/bundler/gems/actionpack-action_caching-30ae10cba0f0/lib/action_controller/caching/actions.rb, line 133</span>
  191. <span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">caching_allowed?</span>
  192. (<span class="ruby-identifier">request</span>.<span class="ruby-identifier">get?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">request</span>.<span class="ruby-identifier">head?</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">response</span>.<span class="ruby-identifier">status</span> <span class="ruby-operator">==</span> <span class="ruby-number">200</span>
  193. <span class="ruby-keyword">end</span></pre>
  194. </div>
  195. </div>
  196. </div>
  197. <div class="sectiontitle">Instance Protected methods</div>
  198. <div class="method">
  199. <div class="title method-title" id="method-i-expire_action">
  200. <b>expire_action</b>(options = {})
  201. <a href="../../../classes/ActionController/Caching/Actions.html#method-i-expire_action" name="method-i-expire_action" class="permalink">Link</a>
  202. </div>
  203. <div class="description">
  204. </div>
  205. <div class="sourcecode">
  206. <p class="source-link">
  207. Source:
  208. <a href="javascript:toggleSource('method-i-expire_action_source')" id="l_method-i-expire_action_source">show</a>
  209. </p>
  210. <div id="method-i-expire_action_source" class="dyn-source">
  211. <pre><span class="ruby-comment"># File ../../.rvm/gems/ruby-2.2.0/bundler/gems/actionpack-action_caching-30ae10cba0f0/lib/action_controller/caching/actions.rb, line 138</span>
  212. <span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">expire_action</span>(<span class="ruby-identifier">options</span> = {})
  213. <span class="ruby-keyword">return</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">cache_configured?</span>
  214. <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:action</span>].<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Array</span>)
  215. <span class="ruby-identifier">options</span>[<span class="ruby-value">:action</span>].<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">action</span><span class="ruby-operator">|</span> <span class="ruby-identifier">expire_action</span>(<span class="ruby-identifier">options</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">action</span><span class="ruby-operator">:</span> <span class="ruby-identifier">action</span>)) }
  216. <span class="ruby-keyword">else</span>
  217. <span class="ruby-identifier">expire_fragment</span>(<span class="ruby-constant">ActionCachePath</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword">self</span>, <span class="ruby-identifier">options</span>, <span class="ruby-keyword">false</span>).<span class="ruby-identifier">path</span>)
  218. <span class="ruby-keyword">end</span>
  219. <span class="ruby-keyword">end</span></pre>
  220. </div>
  221. </div>
  222. </div>
  223. </div>
  224. </div>
  225. </body>
  226. </html>