2005-01-08 18:32:11 -05:00
|
|
|
module ActionView
|
2010-06-16 14:17:49 -04:00
|
|
|
# = Action View Cache Helper
|
2005-01-08 18:32:11 -05:00
|
|
|
module Helpers
|
|
|
|
module CacheHelper
|
2012-10-06 23:14:35 -04:00
|
|
|
# This helper exposes a method for caching fragments of a view
|
2011-05-10 18:36:00 -04:00
|
|
|
# rather than an entire action or page. This technique is useful
|
2013-09-02 01:11:55 -04:00
|
|
|
# caching pieces like menus, lists of new topics, static HTML
|
2010-12-23 07:43:36 -05:00
|
|
|
# fragments, and so on. This method takes a block that contains
|
2012-10-06 23:14:35 -04:00
|
|
|
# the content you wish to cache.
|
2010-06-16 14:17:49 -04:00
|
|
|
#
|
2012-08-08 12:25:13 -04:00
|
|
|
# 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:
|
2014-04-12 03:16:04 -04:00
|
|
|
# http://signalvnoise.com/posts/3113-how-key-based-cache-expiration-works
|
2007-06-23 13:49:18 -04:00
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# When using this method, you list the cache dependency as the name of the cache, like so:
|
2007-06-23 13:49:18 -04:00
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# <% cache project do %>
|
2012-08-08 12:25:13 -04:00
|
|
|
# <b>All the topics on this project</b>
|
|
|
|
# <%= render project.topics %>
|
2007-06-23 13:49:18 -04:00
|
|
|
# <% end %>
|
|
|
|
#
|
2012-08-08 12:25:13 -04:00
|
|
|
# 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:
|
2007-06-23 13:49:18 -04:00
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# views/projects/123-20120806214154/7a1156131a6928cb0026877f8b749ac9
|
|
|
|
# ^class ^id ^updated_at ^template tree digest
|
2007-06-23 13:49:18 -04:00
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# The cache is thus automatically bumped whenever the project updated_at is touched.
|
|
|
|
#
|
|
|
|
# If your template cache depends on multiple sources (try to avoid this to keep things simple),
|
|
|
|
# you can name all these dependencies as part of an array:
|
|
|
|
#
|
|
|
|
# <% cache [ project, current_user ] do %>
|
|
|
|
# <b>All the topics on this project</b>
|
|
|
|
# <%= render project.topics %>
|
|
|
|
# <% end %>
|
|
|
|
#
|
|
|
|
# This will include both records as part of the cache key and updating either of them will
|
|
|
|
# expire the cache.
|
|
|
|
#
|
2015-06-09 05:48:53 -04:00
|
|
|
# ==== \Template digest
|
2012-08-29 15:23:15 -04:00
|
|
|
#
|
|
|
|
# The template digest that's added to the cache key is computed by taking an md5 of the
|
|
|
|
# contents of the entire template file. This ensures that your caches will automatically
|
|
|
|
# expire when you change the template file.
|
|
|
|
#
|
|
|
|
# Note that the md5 is taken of the entire template file, not just what's within the
|
|
|
|
# cache do/end call. So it's possible that changing something outside of that call will
|
|
|
|
# still expire the cache.
|
|
|
|
#
|
|
|
|
# Additionally, the digestor will automatically look through your template file for
|
|
|
|
# explicit and implicit dependencies, and include those as part of the digest.
|
|
|
|
#
|
2012-11-25 23:10:44 -05:00
|
|
|
# The digestor can be bypassed by passing skip_digest: true as an option to the cache call:
|
|
|
|
#
|
|
|
|
# <% cache project, skip_digest: true do %>
|
|
|
|
# <b>All the topics on this project</b>
|
|
|
|
# <%= render project.topics %>
|
|
|
|
# <% end %>
|
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# ==== Implicit dependencies
|
|
|
|
#
|
|
|
|
# Most template dependencies can be derived from calls to render in the template itself.
|
|
|
|
# Here are some examples of render calls that Cache Digests knows how to decode:
|
2012-10-06 23:14:35 -04:00
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# render partial: "comments/comment", collection: commentable.comments
|
|
|
|
# render "comments/comments"
|
|
|
|
# render 'comments/comments'
|
|
|
|
# render('comments/comments')
|
2012-10-06 23:14:35 -04:00
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# render "header" => render("comments/header")
|
2012-10-06 23:14:35 -04:00
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# render(@topic) => render("topics/topic")
|
|
|
|
# render(topics) => render("topics/topic")
|
|
|
|
# render(message.topics) => render("topics/topic")
|
2012-10-06 23:14:35 -04:00
|
|
|
#
|
2015-06-09 05:48:53 -04:00
|
|
|
# It's not possible to derive all render calls like that, though.
|
2015-05-08 13:15:36 -04:00
|
|
|
# Here are a few examples of things that can't be derived:
|
2012-10-06 23:14:35 -04:00
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# render group_of_attachments
|
|
|
|
# render @project.documents.where(published: true).order('created_at')
|
2012-10-06 23:14:35 -04:00
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# You will have to rewrite those to the explicit form:
|
2012-10-06 23:14:35 -04:00
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# render partial: 'attachments/attachment', collection: group_of_attachments
|
|
|
|
# render partial: 'documents/document', collection: @project.documents.where(published: true).order('created_at')
|
|
|
|
#
|
|
|
|
# === Explicit dependencies
|
2012-10-06 23:14:35 -04:00
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# Some times you'll have template dependencies that can't be derived at all. This is typically
|
|
|
|
# the case when you have template rendering that happens in helpers. Here's an example:
|
2012-10-06 23:14:35 -04:00
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# <%= render_sortable_todolists @project.todolists %>
|
2012-10-06 23:14:35 -04:00
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# You'll need to use a special comment format to call those out:
|
2012-10-06 23:14:35 -04:00
|
|
|
#
|
2012-08-29 15:23:15 -04:00
|
|
|
# <%# Template Dependency: todolists/todolist %>
|
|
|
|
# <%= render_sortable_todolists @project.todolists %>
|
2012-10-06 23:14:35 -04:00
|
|
|
#
|
2015-06-09 05:48:53 -04:00
|
|
|
# The pattern used to match these is <tt>/# Template Dependency: (\S+)/</tt>,
|
2015-05-08 13:15:36 -04:00
|
|
|
# so it's important that you type it out just so.
|
2012-08-29 15:23:15 -04:00
|
|
|
# You can only declare one template dependency per line.
|
|
|
|
#
|
|
|
|
# === External dependencies
|
|
|
|
#
|
2015-06-09 05:48:53 -04:00
|
|
|
# If you use a helper method, for example, inside a cached block and
|
|
|
|
# you then update that helper, you'll have to bump the cache as well.
|
2015-05-08 13:15:36 -04:00
|
|
|
# It doesn't really matter how you do it, but the md5 of the template file
|
2012-08-29 15:23:15 -04:00
|
|
|
# must change. One recommendation is to simply be explicit in a comment, like:
|
|
|
|
#
|
|
|
|
# <%# Helper Dependency Updated: May 6, 2012 at 6pm %>
|
|
|
|
# <%= some_helper_method(person) %>
|
|
|
|
#
|
2015-05-08 13:15:36 -04:00
|
|
|
# Now all you have to do is change that timestamp when the helper method changes.
|
2015-02-15 16:39:04 -05:00
|
|
|
#
|
|
|
|
# === Automatic Collection Caching
|
|
|
|
#
|
|
|
|
# When rendering collections such as:
|
|
|
|
#
|
|
|
|
# <%= render @notifications %>
|
|
|
|
# <%= render partial: 'notifications/notification', collection: @notifications %>
|
|
|
|
#
|
2015-05-08 13:15:36 -04:00
|
|
|
# If the notifications/_notification partial starts with a cache call as:
|
2015-02-15 16:39:04 -05:00
|
|
|
#
|
|
|
|
# <% cache notification do %>
|
|
|
|
# <%= notification.name %>
|
|
|
|
# <% end %>
|
|
|
|
#
|
|
|
|
# The collection can then automatically use any cached renders for that
|
|
|
|
# template by reading them at once instead of one by one.
|
|
|
|
#
|
2015-06-09 05:48:53 -04:00
|
|
|
# See ActionView::Template::Handlers::ERB.resource_cache_call_pattern for
|
|
|
|
# more information on what cache calls make a template eligible for this
|
2015-05-08 13:15:36 -04:00
|
|
|
# collection caching.
|
2015-02-15 16:39:04 -05:00
|
|
|
#
|
|
|
|
# The automatic cache multi read can be turned off like so:
|
|
|
|
#
|
|
|
|
# <%= render @notifications, cache: false %>
|
2008-01-03 16:05:12 -05:00
|
|
|
def cache(name = {}, options = nil, &block)
|
2015-04-28 11:46:17 -04:00
|
|
|
if controller.respond_to?(:perform_caching) && controller.perform_caching
|
2012-11-25 23:10:44 -05:00
|
|
|
safe_concat(fragment_for(cache_fragment_name(name, options), options, &block))
|
2010-06-07 20:54:53 -04:00
|
|
|
else
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
2010-03-16 14:43:04 -04:00
|
|
|
nil
|
2005-01-08 18:32:11 -05:00
|
|
|
end
|
2010-03-18 18:52:43 -04:00
|
|
|
|
2012-12-14 13:17:23 -05:00
|
|
|
# Cache fragments of a view if +condition+ is true
|
2012-12-12 12:26:38 -05:00
|
|
|
#
|
2015-02-20 12:04:35 -05:00
|
|
|
# <% cache_if admin?, project do %>
|
2012-12-12 12:26:38 -05:00
|
|
|
# <b>All the topics on this project</b>
|
|
|
|
# <%= render project.topics %>
|
|
|
|
# <% end %>
|
|
|
|
def cache_if(condition, name = {}, options = nil, &block)
|
|
|
|
if condition
|
|
|
|
cache(name, options, &block)
|
|
|
|
else
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2012-12-14 13:17:23 -05:00
|
|
|
# Cache fragments of a view unless +condition+ is true
|
|
|
|
#
|
2015-02-20 12:04:35 -05:00
|
|
|
# <% cache_unless admin?, project do %>
|
2012-12-14 13:17:23 -05:00
|
|
|
# <b>All the topics on this project</b>
|
|
|
|
# <%= render project.topics %>
|
|
|
|
# <% end %>
|
2012-12-12 12:26:38 -05:00
|
|
|
def cache_unless(condition, name = {}, options = nil, &block)
|
|
|
|
cache_if !condition, name, options, &block
|
|
|
|
end
|
|
|
|
|
2012-11-25 23:10:44 -05:00
|
|
|
# This helper returns the name of a cache key for a given fragment cache
|
|
|
|
# call. By supplying skip_digest: true to cache, the digestion of cache
|
|
|
|
# fragments can be manually bypassed. This is useful when cache fragments
|
|
|
|
# cannot be manually expired unless you know the exact key which is the
|
|
|
|
# case when using memcached.
|
|
|
|
def cache_fragment_name(name = {}, options = nil)
|
2012-11-27 10:27:41 -05:00
|
|
|
skip_digest = options && options[:skip_digest]
|
2012-11-25 23:10:44 -05:00
|
|
|
|
|
|
|
if skip_digest
|
|
|
|
name
|
|
|
|
else
|
|
|
|
fragment_name_with_digest(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-15 14:34:18 -05:00
|
|
|
# Given a key (as described in ActionController::Caching::Fragments.expire_fragment),
|
|
|
|
# returns a key suitable for use in reading, writing, or expiring a
|
|
|
|
# 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)
|
|
|
|
end
|
|
|
|
|
2012-11-27 10:27:41 -05:00
|
|
|
private
|
2012-11-29 10:16:18 -05:00
|
|
|
|
2012-10-06 23:14:35 -04:00
|
|
|
def fragment_name_with_digest(name) #:nodoc:
|
2012-09-27 11:41:51 -04:00
|
|
|
if @virtual_path
|
2014-03-21 11:29:00 -04:00
|
|
|
names = Array(name.is_a?(Hash) ? controller.url_for(name).split("://").last : name)
|
|
|
|
digest = Digestor.digest name: @virtual_path, finder: lookup_context, dependencies: view_cache_dependencies
|
2014-03-08 17:09:54 -05:00
|
|
|
|
2014-03-21 11:29:00 -04:00
|
|
|
[ *names, digest ]
|
2012-09-27 11:41:51 -04:00
|
|
|
else
|
|
|
|
name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-18 18:52:43 -04:00
|
|
|
# TODO: Create an object that has caching read/write on it
|
|
|
|
def fragment_for(name = {}, options = nil, &block) #:nodoc:
|
2013-06-25 19:51:02 -04:00
|
|
|
read_fragment_for(name, options) || write_fragment_for(name, options, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_fragment_for(name, options) #:nodoc:
|
|
|
|
controller.read_fragment(name, options)
|
2013-06-01 16:45:43 -04:00
|
|
|
end
|
|
|
|
|
2013-06-25 19:51:02 -04:00
|
|
|
def write_fragment_for(name, options) #:nodoc:
|
2013-06-01 16:45:43 -04:00
|
|
|
# VIEW TODO: Make #capture usable outside of ERB
|
|
|
|
# This dance is needed because Builder can't use capture
|
|
|
|
pos = output_buffer.length
|
|
|
|
yield
|
|
|
|
output_safe = output_buffer.html_safe?
|
|
|
|
fragment = output_buffer.slice!(pos..-1)
|
|
|
|
if output_safe
|
|
|
|
self.output_buffer = output_buffer.class.new(output_buffer)
|
2010-03-18 18:52:43 -04:00
|
|
|
end
|
2013-06-01 16:45:43 -04:00
|
|
|
controller.write_fragment(name, fragment, options)
|
2010-03-18 18:52:43 -04:00
|
|
|
end
|
2005-01-08 18:32:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|