Skip to content
Permalink
Browse files

Modernize the documentation for view caching somewhat

  • Loading branch information
dhh committed Aug 8, 2012
1 parent 5d15287 commit 4154bf012d2bec2aae79e4a49aa94a70d3e91d49
@@ -5,48 +5,18 @@ module Caching
# 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 done using
# the <tt>cache</tt> helper available in the Action View. A
# template with fragment caching might look like:
# the <tt>cache</tt> helper available in the Action View. See
# ActionView::Helpers::CacheHelper for more information.
#
# <b>Hello <%= @name %></b>
# While it's strongly recommended that you use key-based cache
# expiration (see links in CacheHelper for more information),
# it is also possible to manually expire caches. For example:
#
# <% cache do %>
# All the topics in the system:
# <%= render :partial => "topic", :collection => Topic.all %>
# <% end %>
#
# This cache will bind the name of the action that called it, so if
# this code was part of the view for the topics/list action, you
# would be able to invalidate it using:
#
# expire_fragment(:controller => "topics", :action => "list")
#
# This default behavior is limited if you need to cache multiple
# fragments per action or if the action itself is cached using
# <tt>caches_action</tt>. To remedy this, there is an option to
# qualify the name of the cached fragment by using the
# <tt>:action_suffix</tt> option:
#
# <% cache(:action => "list", :action_suffix => "all_topics") do %>
#
# That would result in a name such as
# <tt>/topics/list/all_topics</tt>, avoiding conflicts with the
# action cache and with any fragments that use a different suffix.
# Note that the URL doesn't have to really exist or be callable
# - the url_for system is just used to generate unique cache names
# that we can refer to when we need to expire the cache.
#
# The expiration call for this example is:
#
# expire_fragment(:controller => "topics",
# :action => "list",
# :action_suffix => "all_topics")
# expire_fragment("name_of_cache")
module Fragments
# Given a key (as described in <tt>expire_fragment</tt>), returns
# a key suitable for use in reading, writing, or expiring a
# cached fragment. If the key is a hash, the generated key is the
# return value of url_for on that hash (without the protocol).
# All keys are prefixed with <tt>views/</tt> and uses
# cached fragment. All keys are prefixed with <tt>views/</tt> and uses
# ActiveSupport::Cache.expand_cache_key for the expansion.
def fragment_cache_key(key)
ActiveSupport::Cache.expand_cache_key(key.is_a?(Hash) ? url_for(key).split("://").last : key, :views)
@@ -8,28 +8,28 @@ module CacheHelper
# fragments, and so on. This method takes a block that contains
# the content you wish to cache.
#
# See ActionController::Caching::Fragments for usage instructions.
# The best way to use this is by doing key-based cache expiration
# on top of a cache store like Memcached that'll automatically
# kick out old entries. For more on key-based expiration, see:
# http://37signals.com/svn/posts/3113-how-key-based-cache-expiration-works
#
# If you want to cache a navigation menu, you can do following:
# When using this method, you list the cache dependencies as part of
# the name of the cache, like so:
#
# <% cache do %>
# <%= render :partial => "menu" %>
# <% cache [ "v1", project ] do %>
# <b>All the topics on this project</b>
# <%= render project.topics %>
# <% end %>
#
# You can also cache static content:
# This approach will assume that when a new topic is added, you'll touch
# the project. The cache key generated from this call will be something like:
#
# <% cache do %>
# <p>Hello users! Welcome to our website!</p>
# <% end %>
#
# Static content with embedded ruby content can be cached as
# well:
# views/v1/projects/123-20120806214154
# ^class ^id ^updated_at
#
# <% cache do %>
# Topics:
# <%= render :partial => "topics", :collection => @topic_list %>
# <i>Topics listed alphabetically</i>
# <% end %>
# If you update the rendering of topics, you just bump the version to v2.
# Otherwise the cache is automatically bumped whenever the project updated_at
# is touched.
def cache(name = {}, options = nil, &block)
if controller.perform_caching
safe_concat(fragment_for(name, options, &block))

1 comment on commit 4154bf0

@atulveer

This comment has been minimized.

Copy link

@atulveer atulveer commented on 4154bf0 Aug 13, 2013

👍

Please sign in to comment.
You can’t perform that action at this time.