railties 3.1.3 → 3.1.4.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. data/CHANGELOG.md +17 -2
  2. data/guides/source/3_1_release_notes.textile +107 -0
  3. data/guides/source/active_record_querying.textile +1 -1
  4. data/guides/source/asset_pipeline.textile +3 -11
  5. data/guides/source/configuring.textile +3 -1
  6. data/guides/source/i18n.textile +1 -1
  7. data/guides/source/performance_testing.textile +1 -1
  8. data/guides/source/routing.textile +1 -1
  9. data/lib/rails/application.rb +21 -1
  10. data/lib/rails/application/configuration.rb +3 -1
  11. data/lib/rails/generators/app_base.rb +19 -5
  12. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +2 -0
  13. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +4 -0
  14. data/lib/rails/tasks/documentation.rake +1 -1
  15. data/lib/rails/version.rb +2 -2
  16. metadata +335 -491
  17. data/guides/output/2_2_release_notes.html +0 -565
  18. data/guides/output/2_3_release_notes.html +0 -713
  19. data/guides/output/3_0_release_notes.html +0 -652
  20. data/guides/output/3_1_release_notes.html +0 -670
  21. data/guides/output/action_controller_overview.html +0 -925
  22. data/guides/output/action_mailer_basics.html +0 -658
  23. data/guides/output/action_view_overview.html +0 -1471
  24. data/guides/output/active_model_basics.html +0 -349
  25. data/guides/output/active_record_basics.html +0 -364
  26. data/guides/output/active_record_querying.html +0 -1272
  27. data/guides/output/active_record_validations_callbacks.html +0 -1292
  28. data/guides/output/active_resource_basics.html +0 -252
  29. data/guides/output/active_support_core_extensions.html +0 -3374
  30. data/guides/output/ajax_on_rails.html +0 -412
  31. data/guides/output/api_documentation_guidelines.html +0 -317
  32. data/guides/output/asset_pipeline.html +0 -691
  33. data/guides/output/association_basics.html +0 -1742
  34. data/guides/output/caching_with_rails.html +0 -533
  35. data/guides/output/command_line.html +0 -662
  36. data/guides/output/configuring.html +0 -811
  37. data/guides/output/contribute.html +0 -216
  38. data/guides/output/contributing_to_ruby_on_rails.html +0 -465
  39. data/guides/output/credits.html +0 -210
  40. data/guides/output/debugging_rails_applications.html +0 -791
  41. data/guides/output/engines.html +0 -673
  42. data/guides/output/form_helpers.html +0 -850
  43. data/guides/output/generators.html +0 -725
  44. data/guides/output/getting_started.html +0 -1980
  45. data/guides/output/i18n.html +0 -1054
  46. data/guides/output/images/belongs_to.png +0 -0
  47. data/guides/output/images/book_icon.gif +0 -0
  48. data/guides/output/images/bullet.gif +0 -0
  49. data/guides/output/images/challenge.png +0 -0
  50. data/guides/output/images/chapters_icon.gif +0 -0
  51. data/guides/output/images/check_bullet.gif +0 -0
  52. data/guides/output/images/credits_pic_blank.gif +0 -0
  53. data/guides/output/images/csrf.png +0 -0
  54. data/guides/output/images/customized_error_messages.png +0 -0
  55. data/guides/output/images/edge_badge.png +0 -0
  56. data/guides/output/images/error_messages.png +0 -0
  57. data/guides/output/images/feature_tile.gif +0 -0
  58. data/guides/output/images/footer_tile.gif +0 -0
  59. data/guides/output/images/fxn.png +0 -0
  60. data/guides/output/images/grey_bullet.gif +0 -0
  61. data/guides/output/images/habtm.png +0 -0
  62. data/guides/output/images/has_many.png +0 -0
  63. data/guides/output/images/has_many_through.png +0 -0
  64. data/guides/output/images/has_one.png +0 -0
  65. data/guides/output/images/has_one_through.png +0 -0
  66. data/guides/output/images/header_backdrop.png +0 -0
  67. data/guides/output/images/header_tile.gif +0 -0
  68. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  69. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  70. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  71. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  72. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  73. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  74. data/guides/output/images/icons/README +0 -5
  75. data/guides/output/images/icons/callouts/1.png +0 -0
  76. data/guides/output/images/icons/callouts/10.png +0 -0
  77. data/guides/output/images/icons/callouts/11.png +0 -0
  78. data/guides/output/images/icons/callouts/12.png +0 -0
  79. data/guides/output/images/icons/callouts/13.png +0 -0
  80. data/guides/output/images/icons/callouts/14.png +0 -0
  81. data/guides/output/images/icons/callouts/15.png +0 -0
  82. data/guides/output/images/icons/callouts/2.png +0 -0
  83. data/guides/output/images/icons/callouts/3.png +0 -0
  84. data/guides/output/images/icons/callouts/4.png +0 -0
  85. data/guides/output/images/icons/callouts/5.png +0 -0
  86. data/guides/output/images/icons/callouts/6.png +0 -0
  87. data/guides/output/images/icons/callouts/7.png +0 -0
  88. data/guides/output/images/icons/callouts/8.png +0 -0
  89. data/guides/output/images/icons/callouts/9.png +0 -0
  90. data/guides/output/images/icons/caution.png +0 -0
  91. data/guides/output/images/icons/example.png +0 -0
  92. data/guides/output/images/icons/home.png +0 -0
  93. data/guides/output/images/icons/important.png +0 -0
  94. data/guides/output/images/icons/next.png +0 -0
  95. data/guides/output/images/icons/note.png +0 -0
  96. data/guides/output/images/icons/prev.png +0 -0
  97. data/guides/output/images/icons/tip.png +0 -0
  98. data/guides/output/images/icons/up.png +0 -0
  99. data/guides/output/images/icons/warning.png +0 -0
  100. data/guides/output/images/jaimeiniesta.jpg +0 -0
  101. data/guides/output/images/nav_arrow.gif +0 -0
  102. data/guides/output/images/polymorphic.png +0 -0
  103. data/guides/output/images/posts_index.png +0 -0
  104. data/guides/output/images/radar.png +0 -0
  105. data/guides/output/images/rails_guides_logo.gif +0 -0
  106. data/guides/output/images/rails_logo_remix.gif +0 -0
  107. data/guides/output/images/rails_welcome.png +0 -0
  108. data/guides/output/images/session_fixation.png +0 -0
  109. data/guides/output/images/tab_grey.gif +0 -0
  110. data/guides/output/images/tab_info.gif +0 -0
  111. data/guides/output/images/tab_note.gif +0 -0
  112. data/guides/output/images/tab_red.gif +0 -0
  113. data/guides/output/images/tab_yellow.gif +0 -0
  114. data/guides/output/images/tab_yellow.png +0 -0
  115. data/guides/output/images/validation_error_messages.png +0 -0
  116. data/guides/output/images/vijaydev.jpg +0 -0
  117. data/guides/output/index.html +0 -300
  118. data/guides/output/initialization.html +0 -1087
  119. data/guides/output/javascripts/guides.js +0 -7
  120. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  121. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  122. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  123. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  124. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  125. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  126. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  127. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  128. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  129. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  130. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  131. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  132. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  133. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  134. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  135. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  136. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  137. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  138. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  139. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  140. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  141. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  142. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  143. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  144. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  145. data/guides/output/javascripts/syntaxhighlighter/shCore.js +0 -17
  146. data/guides/output/layout.html +0 -312
  147. data/guides/output/layouts_and_rendering.html +0 -1257
  148. data/guides/output/migrations.html +0 -751
  149. data/guides/output/nested_model_forms.html +0 -350
  150. data/guides/output/performance_testing.html +0 -858
  151. data/guides/output/plugins.html +0 -590
  152. data/guides/output/rails_application_templates.html +0 -368
  153. data/guides/output/rails_on_rack.html +0 -408
  154. data/guides/output/routing.html +0 -1246
  155. data/guides/output/ruby_on_rails_guides_guidelines.html +0 -218
  156. data/guides/output/security.html +0 -968
  157. data/guides/output/stylesheets/fixes.css +0 -16
  158. data/guides/output/stylesheets/main.css +0 -445
  159. data/guides/output/stylesheets/print.css +0 -52
  160. data/guides/output/stylesheets/reset.css +0 -43
  161. data/guides/output/stylesheets/style.css +0 -13
  162. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +0 -226
  163. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  164. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  165. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  166. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  167. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  168. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  169. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  170. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  171. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  172. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  173. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  174. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  175. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  176. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  177. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  178. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  179. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  180. data/guides/output/testing.html +0 -1182
@@ -1,533 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
-
4
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
- <head>
6
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
-
8
- <title>Ruby on Rails Guides: Caching with Rails: An overview</title>
9
-
10
- <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
11
- <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
12
-
13
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
14
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
15
-
16
- <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
17
- </head>
18
- <body class="guide">
19
- <div id="topNav">
20
- <div class="wrapper">
21
- <strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
22
- <a href="http://rubyonrails.org/">Overview</a> |
23
- <a href="http://rubyonrails.org/download">Download</a> |
24
- <a href="http://rubyonrails.org/deploy">Deploy</a> |
25
- <a href="https://github.com/rails/rails">Code</a> |
26
- <a href="http://rubyonrails.org/screencasts">Screencasts</a> |
27
- <a href="http://rubyonrails.org/documentation">Documentation</a> |
28
- <a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
29
- <a href="http://rubyonrails.org/community">Community</a> |
30
- <a href="http://weblog.rubyonrails.org/">Blog</a>
31
- </div>
32
- </div>
33
- <div id="header">
34
- <div class="wrapper clearfix">
35
- <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
36
- <p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
37
- <ul class="nav">
38
- <li><a href="index.html">Home</a></li>
39
- <li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
40
- <div id="guides" class="clearfix" style="display: none;">
41
- <hr />
42
- <dl class="L">
43
- <dt>Start Here</dt>
44
- <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
45
- <dt>Models</dt>
46
- <dd><a href="migrations.html">Rails Database Migrations</a></dd>
47
- <dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
48
- <dd><a href="association_basics.html">Active Record Associations</a></dd>
49
- <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
50
- <dt>Views</dt>
51
- <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
52
- <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
53
- <dt>Controllers</dt>
54
- <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
55
- <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
56
- </dl>
57
- <dl class="R">
58
- <dt>Digging Deeper</dt>
59
- <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
60
- <dd><a href="i18n.html">Rails Internationalization API</a></dd>
61
- <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
62
- <dd><a href="testing.html">Testing Rails Applications</a></dd>
63
- <dd><a href="security.html">Securing Rails Applications</a></dd>
64
- <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
65
- <dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
66
- <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
67
- <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
68
- <dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
69
- <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
70
-
71
- <dt>Extending Rails</dt>
72
- <dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
73
- <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
74
- <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
75
-
76
- <dt>Contributing to Ruby on Rails</dt>
77
- <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
78
- <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
79
- <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
80
-
81
- <dt>Release Notes</dt>
82
- <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
83
- <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
84
- <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
85
- <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
86
- </dl>
87
- </div>
88
- </li>
89
- <li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
90
- <li><a href="credits.html">Credits</a></li>
91
- </ul>
92
- </div>
93
- </div>
94
- <hr class="hide" />
95
-
96
- <div id="feature">
97
- <div class="wrapper">
98
- <h2>Caching with Rails: An overview</h2>
99
- <p>This guide will teach you what you need to know about avoiding that expensive round-trip to your database and returning what you need to return to the web clients in the shortest time possible.</p>
100
- <p>After reading this guide, you should be able to use and configure:</p>
101
- <ul>
102
- <li>Page, action, and fragment caching</li>
103
- <li>Sweepers</li>
104
- <li>Alternative cache stores</li>
105
- <li>Conditional <span class="caps">GET</span> support</li>
106
- </ul>
107
-
108
- <div id="subCol">
109
- <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
110
- <ol class="chapters">
111
- <li><a href="#basic-caching">Basic Caching</a><ul><li><a href="#page-caching">Page Caching</a></li> <li><a href="#action-caching">Action Caching</a></li> <li><a href="#fragment-caching">Fragment Caching</a></li> <li><a href="#sweepers">Sweepers</a></li> <li><a href="#sql-caching"><span class="caps">SQL</span> Caching</a></li></ul></li><li><a href="#cache-stores">Cache Stores</a><ul><li><a href="#configuration">Configuration</a></li> <li><a href="#activesupport-cache-store">ActiveSupport::Cache::Store</a></li> <li><a href="#activesupport-cache-memorystore">ActiveSupport::Cache::MemoryStore</a></li> <li><a href="#activesupport-cache-filestore">ActiveSupport::Cache::FileStore</a></li> <li><a href="#activesupport-cache-memcachestore">ActiveSupport::Cache::MemCacheStore</a></li> <li><a href="#activesupport-cache-ehcachestore">ActiveSupport::Cache::EhcacheStore</a></li> <li><a href="#custom-cache-stores">Custom Cache Stores</a></li> <li><a href="#cache-keys">Cache Keys</a></li></ul></li><li><a href="#conditional-get-support">Conditional <span class="caps">GET</span> support</a></li><li><a href="#further-reading">Further reading</a></li></ol></div>
112
- </div>
113
- </div>
114
-
115
- <div id="container">
116
- <div class="wrapper">
117
- <div id="mainCol">
118
- <h3 id="basic-caching">1 Basic Caching</h3>
119
- <p>This is an introduction to the three types of caching techniques that Rails provides by default without the use of any third party plugins.</p>
120
- <p>To start playing with caching you&#8217;ll want to ensure that <tt>config.action_controller.perform_caching</tt> is set to <tt>true</tt>, if you&#8217;re running in development mode. This flag is normally set in the corresponding <tt>config/environments/*.rb</tt> and caching is disabled by default for development and test, and enabled for production.</p>
121
- <div class="code_container">
122
- <pre class="brush: ruby; gutter: false; toolbar: false">
123
- config.action_controller.perform_caching = true
124
- </pre>
125
- </div>
126
- <h4 id="page-caching">1.1 Page Caching</h4>
127
- <p>Page caching is a Rails mechanism which allows the request for a generated page to be fulfilled by the webserver (i.e. Apache or nginx), without ever having to go through the Rails stack at all. Obviously, this is super-fast. Unfortunately, it can&#8217;t be applied to every situation (such as pages that need authentication) and since the webserver is literally just serving a file from the filesystem, cache expiration is an issue that needs to be dealt with.</p>
128
- <p>To enable page caching, you need to use the <tt>caches_page</tt> method.</p>
129
- <div class="code_container">
130
- <pre class="brush: ruby; gutter: false; toolbar: false">
131
- class ProductsController &lt; ActionController
132
-
133
- caches_page :index
134
-
135
- def index
136
- @products = Products.all
137
- end
138
- end
139
- </pre>
140
- </div>
141
- <p>Let&#8217;s say you have a controller called <tt>ProductsController</tt> and an <tt>index</tt> action that lists all the products. The first time anyone requests <tt>/products</tt>, Rails will generate a file called <tt>products.html</tt> and the webserver will then look for that file before it passes the next request for <tt>/products</tt> to your Rails application.</p>
142
- <p>By default, the page cache directory is set to <tt>Rails.public_path</tt> (which is usually set to the <tt>public</tt> folder) and this can be configured by changing the configuration setting <tt>config.action_controller.page_cache_directory</tt>. Changing the default from <tt>public</tt> helps avoid naming conflicts, since you may want to put other static html in <tt>public</tt>, but changing this will require web server reconfiguration to let the web server know where to serve the cached files from.</p>
143
- <p>The Page Caching mechanism will automatically add a <tt>.html</tt> extension to requests for pages that do not have an extension to make it easy for the webserver to find those pages and this can be configured by changing the configuration setting <tt>config.action_controller.page_cache_extension</tt>.</p>
144
- <p>In order to expire this page when a new product is added we could extend our example controller like this:</p>
145
- <div class="code_container">
146
- <pre class="brush: ruby; gutter: false; toolbar: false">
147
- class ProductsController &lt; ActionController
148
-
149
- caches_page :index
150
-
151
- def index
152
- @products = Products.all
153
- end
154
-
155
- def create
156
- expire_page :action =&gt; :index
157
- end
158
-
159
- end
160
- </pre>
161
- </div>
162
- <p>If you want a more complicated expiration scheme, you can use cache sweepers to expire cached objects when things change. This is covered in the section on Sweepers.</p>
163
- <div class="note"><p>Page caching ignores all parameters. For example <tt>/products?page=1</tt> will be written out to the filesystem as <tt>products.html</tt> with no reference to the <tt>page</tt> parameter. Thus, if someone requests <tt>/products?page=2</tt> later, they will get the cached first page. Be careful when page caching <span class="caps">GET</span> parameters in the <span class="caps">URL</span>!</p></div>
164
- <div class="info"><p>Page caching runs in an after filter. Thus, invalid requests won&#8217;t generate spurious cache entries as long as you halt them. Typically, a redirection in some before filter that checks request preconditions does the job.</p></div>
165
- <h4 id="action-caching">1.2 Action Caching</h4>
166
- <p>One of the issues with Page Caching is that you cannot use it for pages that require to restrict access somehow. This is where Action Caching comes in. Action Caching works like Page Caching except for the fact that the incoming web request does go from the webserver to the Rails stack and Action Pack so that before filters can be run on it before the cache is served. This allows authentication and other restriction to be run while still serving the result of the output from a cached copy.</p>
167
- <p>Clearing the cache works in the exact same way as with Page Caching.</p>
168
- <p>Let&#8217;s say you only wanted authenticated users to call actions on <tt>ProductsController</tt>.</p>
169
- <div class="code_container">
170
- <pre class="brush: ruby; gutter: false; toolbar: false">
171
- class ProductsController &lt; ActionController
172
-
173
- before_filter :authenticate
174
- caches_action :index
175
-
176
- def index
177
- @products = Product.all
178
- end
179
-
180
- def create
181
- expire_action :action =&gt; :index
182
- end
183
-
184
- end
185
- </pre>
186
- </div>
187
- <p>You can also use <tt>:if</tt> (or <tt>:unless</tt>) to pass a Proc that specifies when the action should be cached. Also, you can use <tt>:layout => false</tt> to cache without layout so that dynamic information in the layout such as logged in user info or the number of items in the cart can be left uncached. This feature is available as of Rails 2.2.</p>
188
- <p>You can modify the default action cache path by passing a <tt>:cache_path</tt> option. This will be passed directly to <tt>ActionCachePath.path_for</tt>. This is handy for actions with multiple possible routes that should be cached differently. If a block is given, it is called with the current controller instance.</p>
189
- <p>Finally, if you are using memcached or Ehcache, you can also pass <tt>:expires_in</tt>. In fact, all parameters not used by <tt>caches_action</tt> are sent to the underlying cache store.</p>
190
- <div class="info"><p>Action caching runs in an after filter. Thus, invalid requests won&#8217;t generate spurious cache entries as long as you halt them. Typically, a redirection in some before filter that checks request preconditions does the job.</p></div>
191
- <h4 id="fragment-caching">1.3 Fragment Caching</h4>
192
- <p>Life would be perfect if we could get away with caching the entire contents of a page or action and serving it out to the world. Unfortunately, dynamic web applications usually build pages with a variety of components not all of which have the same caching characteristics. In order to address such a dynamically created page where different parts of the page need to be cached and expired differently, Rails provides a mechanism called Fragment Caching.</p>
193
- <p>Fragment Caching allows a fragment of view logic to be wrapped in a cache block and served out of the cache store when the next request comes in.</p>
194
- <p>As an example, if you wanted to show all the orders placed on your website in real time and didn&#8217;t want to cache that part of the page, but did want to cache the part of the page which lists all products available, you could use this piece of code:</p>
195
- <div class="code_container">
196
- <pre class="brush: ruby; gutter: false; toolbar: false">
197
- &lt;% Order.find_recent.each do |o| %&gt;
198
- &lt;%= o.buyer.name %&gt; bought &lt;%= o.product.name %&gt;
199
- &lt;% end %&gt;
200
-
201
- &lt;% cache do %&gt;
202
- All available products:
203
- &lt;% Product.all.each do |p| %&gt;
204
- &lt;%= link_to p.name, product_url(p) %&gt;
205
- &lt;% end %&gt;
206
- &lt;% end %&gt;
207
- </pre>
208
- </div>
209
- <p>The cache block in our example will bind to the action that called it and is written out to the same place as the Action Cache, which means that if you want to cache multiple fragments per action, you should provide an <tt>action_suffix</tt> to the cache call:</p>
210
- <div class="code_container">
211
- <pre class="brush: ruby; gutter: false; toolbar: false">
212
- &lt;% cache(:action =&gt; 'recent', :action_suffix =&gt; 'all_products') do %&gt;
213
- All available products:
214
- </pre>
215
- </div>
216
- <p>and you can expire it using the <tt>expire_fragment</tt> method, like so:</p>
217
- <div class="code_container">
218
- <pre class="brush: ruby; gutter: false; toolbar: false">
219
- expire_fragment(:controller =&gt; 'products', :action =&gt; 'recent', :action_suffix =&gt; 'all_products')
220
- </pre>
221
- </div>
222
- <p>If you don&#8217;t want the cache block to bind to the action that called it, You can also use globally keyed fragments by calling the <tt>cache</tt> method with a key, like so:</p>
223
- <div class="code_container">
224
- <pre class="brush: ruby; gutter: false; toolbar: false">
225
- &lt;% cache('all_available_products') do %&gt;
226
- All available products:
227
- &lt;% end %&gt;
228
- </pre>
229
- </div>
230
- <p>This fragment is then available to all actions in the <tt>ProductsController</tt> using the key and can be expired the same way:</p>
231
- <div class="code_container">
232
- <pre class="brush: ruby; gutter: false; toolbar: false">
233
- expire_fragment('all_available_products')
234
- </pre>
235
- </div>
236
- <h4 id="sweepers">1.4 Sweepers</h4>
237
- <p>Cache sweeping is a mechanism which allows you to get around having a ton of <tt>expire_{page,action,fragment}</tt> calls in your code. It does this by moving all the work required to expire cached content into an <tt>ActionController::Caching::Sweeper</tt> subclass. This class is an observer and looks for changes to an object via callbacks, and when a change occurs it expires the caches associated with that object in an around or after filter.</p>
238
- <p>Continuing with our Product controller example, we could rewrite it with a sweeper like this:</p>
239
- <div class="code_container">
240
- <pre class="brush: ruby; gutter: false; toolbar: false">
241
- class ProductSweeper &lt; ActionController::Caching::Sweeper
242
- observe Product # This sweeper is going to keep an eye on the Product model
243
-
244
- # If our sweeper detects that a Product was created call this
245
- def after_create(product)
246
- expire_cache_for(product)
247
- end
248
-
249
- # If our sweeper detects that a Product was updated call this
250
- def after_update(product)
251
- expire_cache_for(product)
252
- end
253
-
254
- # If our sweeper detects that a Product was deleted call this
255
- def after_destroy(product)
256
- expire_cache_for(product)
257
- end
258
-
259
- private
260
- def expire_cache_for(product)
261
- # Expire the index page now that we added a new product
262
- expire_page(:controller =&gt; 'products', :action =&gt; 'index')
263
-
264
- # Expire a fragment
265
- expire_fragment('all_available_products')
266
- end
267
- end
268
- </pre>
269
- </div>
270
- <p>You may notice that the actual product gets passed to the sweeper, so if we were caching the edit action for each product, we could add an expire method which specifies the page we want to expire:</p>
271
- <div class="code_container">
272
- <pre class="brush: ruby; gutter: false; toolbar: false">
273
- expire_action(:controller =&gt; 'products', :action =&gt; 'edit', :id =&gt; product.id)
274
- </pre>
275
- </div>
276
- <p>Then we add it to our controller to tell it to call the sweeper when certain actions are called. So, if we wanted to expire the cached content for the list and edit actions when the create action was called, we could do the following:</p>
277
- <div class="code_container">
278
- <pre class="brush: ruby; gutter: false; toolbar: false">
279
- class ProductsController &lt; ActionController
280
-
281
- before_filter :authenticate
282
- caches_action :index
283
- cache_sweeper :product_sweeper
284
-
285
- def index
286
- @products = Product.all
287
- end
288
-
289
- end
290
- </pre>
291
- </div>
292
- <h4 id="sql-caching">1.5 <span class="caps">SQL</span> Caching</h4>
293
- <p>Query caching is a Rails feature that caches the result set returned by each query so that if Rails encounters the same query again for that request, it will use the cached result set as opposed to running the query against the database again.</p>
294
- <p>For example:</p>
295
- <div class="code_container">
296
- <pre class="brush: ruby; gutter: false; toolbar: false">
297
- class ProductsController &lt; ActionController
298
-
299
- def index
300
- # Run a find query
301
- @products = Product.all
302
-
303
- ...
304
-
305
- # Run the same query again
306
- @products = Product.all
307
- end
308
-
309
- end
310
- </pre>
311
- </div>
312
- <p>The second time the same query is run against the database, it&#8217;s not actually going to hit the database. The first time the result is returned from the query it is stored in the query cache (in memory) and the second time it&#8217;s pulled from memory.</p>
313
- <p>However, it&#8217;s important to note that query caches are created at the start of an action and destroyed at the end of that action and thus persist only for the duration of the action. If you&#8217;d like to store query results in a more persistent fashion, you can in Rails by using low level caching.</p>
314
- <h3 id="cache-stores">2 Cache Stores</h3>
315
- <p>Rails provides different stores for the cached data created by action and fragment caches. Page caches are always stored on disk.</p>
316
- <h4 id="configuration">2.1 Configuration</h4>
317
- <p>You can set up your application&#8217;s default cache store by calling <tt>config.cache_store=</tt> in the Application definition inside your <tt>config/application.rb</tt> file or in an Application.configure block in an environment specific configuration file (i.e. <tt>config/environments/*.rb</tt>). The first argument will be the cache store to use and the rest of the argument will be passed as arguments to the cache store constructor.</p>
318
- <div class="code_container">
319
- <pre class="brush: ruby; gutter: false; toolbar: false">
320
- config.cache_store = :memory_store
321
- </pre>
322
- </div>
323
- <p>Alternatively, you can call <tt>ActionController::Base.cache_store</tt> outside of a configuration block.</p>
324
- <p>You can access the cache by calling <tt>Rails.cache</tt>.</p>
325
- <h4 id="activesupport-cache-store">2.2 ActiveSupport::Cache::Store</h4>
326
- <p>This class provides the foundation for interacting with the cache in Rails. This is an abstract class and you cannot use it on its own. Rather you must use a concrete implementation of the class tied to a storage engine. Rails ships with several implementations documented below.</p>
327
- <p>The main methods to call are <tt>read</tt>, <tt>write</tt>, <tt>delete</tt>, <tt>exist?</tt>, and <tt>fetch</tt>. The fetch method takes a block and will either return an existing value from the cache, or evaluate the block and write the result to the cache if no value exists.</p>
328
- <p>There are some common options used by all cache implementations. These can be passed to the constructor or the various methods to interact with entries.</p>
329
- <ul>
330
- <li><tt>:namespace</tt> &#8211; This option can be used to create a namespace within the cache store. It is especially useful if your application shares a cache with other applications. The default value will include the application name and Rails environment.</li>
331
- </ul>
332
- <ul>
333
- <li><tt>:compress</tt> &#8211; This option can be used to indicate that compression should be used in the cache. This can be useful for transferring large cache entries over a slow network.</li>
334
- </ul>
335
- <ul>
336
- <li><tt>:compress_threshold</tt> &#8211; This options is used in conjunction with the <tt>:compress</tt> option to indicate a threshold under which cache entries should not be compressed. This defaults to 16 kilobytes.</li>
337
- </ul>
338
- <ul>
339
- <li><tt>:expires_in</tt> &#8211; This option sets an expiration time in seconds for the cache entry when it will be automatically removed from the cache.</li>
340
- </ul>
341
- <ul>
342
- <li><tt>:race_condition_ttl</tt> &#8211; This option is used in conjunction with the <tt>:expires_in</tt> option. It will prevent race conditions when cache entries expire by preventing multiple processes from simultaneously regenerating the same entry (also known as the dog pile effect). This option sets the number of seconds that an expired entry can be reused while a new value is being regenerated. It&#8217;s a good practice to set this value if you use the <tt>:expires_in</tt> option.</li>
343
- </ul>
344
- <h4 id="activesupport-cache-memorystore">2.3 ActiveSupport::Cache::MemoryStore</h4>
345
- <p>This cache store keeps entries in memory in the same Ruby process. The cache store has a bounded size specified by the <tt>:size</tt> options to the initializer (default is 32Mb). When the cache exceeds the allotted size, a cleanup will occur and the least recently used entries will be removed.</p>
346
- <div class="code_container">
347
- <pre class="brush: ruby; gutter: false; toolbar: false">
348
- ActionController::Base.cache_store = :memory_store, :size =&gt; 64.megabytes
349
- </pre>
350
- </div>
351
- <p>If you&#8217;re running multiple Ruby on Rails server processes (which is the case if you&#8217;re using mongrel_cluster or Phusion Passenger), then your Rails server process instances won&#8217;t be able to share cache data with each other. This cache store is not appropriate for large application deployments, but can work well for small, low traffic sites with only a couple of server processes or for development and test environments.</p>
352
- <p>This is the default cache store implementation.</p>
353
- <h4 id="activesupport-cache-filestore">2.4 ActiveSupport::Cache::FileStore</h4>
354
- <p>This cache store uses the file system to store entries. The path to the directory where the store files will be stored must be specified when initializing the cache.</p>
355
- <div class="code_container">
356
- <pre class="brush: ruby; gutter: false; toolbar: false">
357
- ActionController::Base.cache_store = :file_store, &quot;/path/to/cache/directory&quot;
358
- </pre>
359
- </div>
360
- <p>With this cache store, multiple server processes on the same host can share a cache. Servers processes running on different hosts could share a cache by using a shared file system, but that set up would not be ideal and is not recommended. The cache store is appropriate for low to medium traffic sites that are served off one or two hosts.</p>
361
- <p>Note that the cache will grow until the disk is full unless you periodically clear out old entries.</p>
362
- <h4 id="activesupport-cache-memcachestore">2.5 ActiveSupport::Cache::MemCacheStore</h4>
363
- <p>This cache store uses Danga&#8217;s <tt>memcached</tt> server to provide a centralized cache for your application. Rails uses the bundled <tt>memcached-client</tt> gem by default. This is currently the most popular cache store for production websites. It can be used to provide a single, shared cache cluster with very a high performance and redundancy.</p>
364
- <p>When initializing the cache, you need to specify the addresses for all memcached servers in your cluster. If none is specified, it will assume memcached is running on the local host on the default port, but this is not an ideal set up for larger sites.</p>
365
- <p>The <tt>write</tt> and <tt>fetch</tt> methods on this cache accept two additional options that take advantage of features specific to memcached. You can specify <tt>:raw</tt> to send a value directly to the server with no serialization. The value must be a string or number. You can use memcached direct operation like <tt>increment</tt> and <tt>decrement</tt> only on raw values. You can also specify <tt>:unless_exist</tt> if you don&#8217;t want memcached to overwrite an existing entry.</p>
366
- <div class="code_container">
367
- <pre class="brush: ruby; gutter: false; toolbar: false">
368
- ActionController::Base.cache_store = :mem_cache_store, &quot;cache-1.example.com&quot;, &quot;cache-2.example.com&quot;
369
- </pre>
370
- </div>
371
- <h4 id="activesupport-cache-ehcachestore">2.6 ActiveSupport::Cache::EhcacheStore</h4>
372
- <p>If you are using JRuby you can use Terracotta&#8217;s Ehcache as the cache store for your application. Ehcache is an open source Java cache that also offers an enterprise version with increased scalability, management, and commercial support. You must first install the jruby-ehcache-rails3 gem (version 1.1.0 or later) to use this cache store.</p>
373
- <div class="code_container">
374
- <pre class="brush: ruby; gutter: false; toolbar: false">
375
- ActionController::Base.cache_store = :ehcache_store
376
- </pre>
377
- </div>
378
- <p>When initializing the cache, you may use the <tt>:ehcache_config</tt> option to specify the Ehcache config file to use (where the default is &#8220;ehcache.xml&#8221; in your Rails config directory), and the :cache_name option to provide a custom name for your cache (the default is rails_cache).</p>
379
- <p>In addition to the standard <tt>:expires_in</tt> option, the <tt>write</tt> method on this cache can also accept the additional <tt>:unless_exist</tt> option, which will cause the cache store to use Ehcache&#8217;s <tt>putIfAbsent</tt> method instead of <tt>put</tt>, and therefore will not overwrite an existing entry. Additionally, the <tt>write</tt> method supports all of the properties exposed by the <a href="http://ehcache.org/apidocs/net/sf/ehcache/Element.html">Ehcache Element class</a> , including:</p>
380
- <table>
381
- <tr>
382
- <th>Property </th>
383
- <th>Argument Type </th>
384
- <th>Description </th>
385
- </tr>
386
- <tr>
387
- <td> elementEvictionData </td>
388
- <td> ElementEvictionData </td>
389
- <td> Sets this element&#8217;s eviction data instance. </td>
390
- </tr>
391
- <tr>
392
- <td> eternal </td>
393
- <td> boolean </td>
394
- <td> Sets whether the element is eternal. </td>
395
- </tr>
396
- <tr>
397
- <td> timeToIdle, tti </td>
398
- <td> int </td>
399
- <td> Sets time to idle </td>
400
- </tr>
401
- <tr>
402
- <td> timeToLive, ttl, expires_in </td>
403
- <td> int </td>
404
- <td> Sets time to Live </td>
405
- </tr>
406
- <tr>
407
- <td> version </td>
408
- <td> long </td>
409
- <td> Sets the version attribute of the ElementAttributes object. </td>
410
- </tr>
411
- </table>
412
- <p>These options are passed to the <tt>write</tt> method as Hash options using either camelCase or underscore notation, as in the following examples:</p>
413
- <div class="code_container">
414
- <pre class="brush: ruby; gutter: false; toolbar: false">
415
- Rails.cache.write('key', 'value', :time_to_idle =&gt; 60.seconds, :timeToLive =&gt; 600.seconds)
416
- caches_action :index, :expires_in =&gt; 60.seconds, :unless_exist =&gt; true
417
- </pre>
418
- </div>
419
- <p>For more information about Ehcache, see <a href="http://ehcache.org/">http://ehcache.org/</a> .
420
- For more information about Ehcache for JRuby and Rails, see <a href="http://ehcache.org/documentation/jruby.html">http://ehcache.org/documentation/jruby.html</a></p>
421
- <h4 id="custom-cache-stores">2.7 Custom Cache Stores</h4>
422
- <p>You can create your own custom cache store by simply extending <tt>ActiveSupport::Cache::Store</tt> and implementing the appropriate methods. In this way, you can swap in any number of caching technologies into your Rails application.</p>
423
- <p>To use a custom cache store, simple set the cache store to a new instance of the class.</p>
424
- <div class="code_container">
425
- <pre class="brush: ruby; gutter: false; toolbar: false">
426
- ActionController::Base.cache_store = MyCacheStore.new
427
- </pre>
428
- </div>
429
- <h4 id="cache-keys">2.8 Cache Keys</h4>
430
- <p>The keys used in a cache can be any object that responds to either <tt>:cache_key</tt> or to <tt>:to_param</tt>. You can implement the <tt>:cache_key</tt> method on your classes if you need to generate custom keys. Active Record will generate keys based on the class name and record id.</p>
431
- <p>You can use Hashes and Arrays of values as cache keys.</p>
432
- <div class="code_container">
433
- <pre class="brush: ruby; gutter: false; toolbar: false">
434
- # This is a legal cache key
435
- Rails.cache.read(:site =&gt; &quot;mysite&quot;, :owners =&gt; [owner_1, owner_2])
436
- </pre>
437
- </div>
438
- <p>The keys you use on <tt>Rails.cache</tt> will not be the same as those actually used with the storage engine. They may be modified with a namespace or altered to fit technology backend constraints. This means, for instance, that you can&#8217;t save values with <tt>Rails.cache</tt> and then try to pull them out with the <tt>memcache-client</tt> gem. However, you also don&#8217;t need to worry about exceeding the memcached size limit or violating syntax rules.</p>
439
- <h3 id="conditional-get-support">3 Conditional <span class="caps">GET</span> support</h3>
440
- <p>Conditional GETs are a feature of the <span class="caps">HTTP</span> specification that provide a way for web servers to tell browsers that the response to a <span class="caps">GET</span> request hasn&#8217;t changed since the last request and can be safely pulled from the browser cache.</p>
441
- <p>They work by using the <tt>HTTP_IF_NONE_MATCH</tt> and <tt>HTTP_IF_MODIFIED_SINCE</tt> headers to pass back and forth both a unique content identifier and the timestamp of when the content was last changed. If the browser makes a request where the content identifier (etag) or last modified since timestamp matches the server’s version then the server only needs to send back an empty response with a not modified status.</p>
442
- <p>It is the server&#8217;s (i.e. our) responsibility to look for a last modified timestamp and the if-none-match header and determine whether or not to send back the full response. With conditional-get support in Rails this is a pretty easy task:</p>
443
- <div class="code_container">
444
- <pre class="brush: ruby; gutter: false; toolbar: false">
445
- class ProductsController &lt; ApplicationController
446
-
447
- def show
448
- @product = Product.find(params[:id])
449
-
450
- # If the request is stale according to the given timestamp and etag value
451
- # (i.e. it needs to be processed again) then execute this block
452
- if stale?(:last_modified =&gt; @product.updated_at.utc, :etag =&gt; @product)
453
- respond_to do |wants|
454
- # ... normal response processing
455
- end
456
- end
457
-
458
- # If the request is fresh (i.e. it's not modified) then you don't need to do
459
- # anything. The default render checks for this using the parameters
460
- # used in the previous call to stale? and will automatically send a
461
- # :not_modified. So that's it, you're done.
462
- end
463
- end
464
- </pre>
465
- </div>
466
- <p>If you don&#8217;t have any special response processing and are using the default rendering mechanism (i.e. you&#8217;re not using respond_to or calling render yourself) then you’ve got an easy helper in fresh_when:</p>
467
- <div class="code_container">
468
- <pre class="brush: ruby; gutter: false; toolbar: false">
469
- class ProductsController &lt; ApplicationController
470
-
471
- # This will automatically send back a :not_modified if the request is fresh,
472
- # and will render the default template (product.*) if it's stale.
473
-
474
- def show
475
- @product = Product.find(params[:id])
476
- fresh_when :last_modified =&gt; @product.published_at.utc, :etag =&gt; @product
477
- end
478
- end
479
- </pre>
480
- </div>
481
- <h3 id="further-reading">4 Further reading</h3>
482
- <ul>
483
- <li><a href="http://railslab.newrelic.com/scaling-rails">Scaling Rails Screencasts</a></li>
484
- </ul>
485
-
486
- <h3>Feedback</h3>
487
- <p>
488
- You're encouraged to help improve the quality of this guide.
489
- </p>
490
- <p>
491
- If you see any typos or factual errors you are confident to
492
- patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
493
- and push the change yourself. That branch of Rails has public write access.
494
- Commits are still reviewed, but that happens after you've submitted your
495
- contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
496
- cross-merged with master periodically.
497
- </p>
498
- <p>
499
- You may also find incomplete content, or stuff that is not up to date.
500
- Please do add any missing documentation for master. Check the
501
- <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
502
- for style and conventions.
503
- </p>
504
- <p>
505
- If for whatever reason you spot something to fix but cannot patch it yourself, please
506
- <a href="https://github.com/rails/rails/issues">open an issue</a>.
507
- </p>
508
- <p>And last but not least, any kind of discussion regarding Ruby on Rails
509
- documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
510
- </p>
511
- </div>
512
- </div>
513
- </div>
514
-
515
- <hr class="hide" />
516
- <div id="footer">
517
- <div class="wrapper">
518
- <p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
519
- <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
520
- </div>
521
- </div>
522
-
523
- <script type="text/javascript" src="javascripts/guides.js"></script>
524
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
525
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
526
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
527
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
528
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
529
- <script type="text/javascript">
530
- SyntaxHighlighter.all()
531
- </script>
532
- </body>
533
- </html>