BenchmarkHelper, CacheHelper, and CaptureHelper
All three of these classes contain methods that wrap around content in
your templates. BenchmarkHelper is a
class you’ll mostly want to use during development, when it may help you
isolate code that’s taking the view a long time to run. CacheHelper and CaptureHelper are both
for advanced development. While CacheHelper allows you to specify fragments of your
views that will be stored for future reuse, and applied when the same
call comes through, CaptureHelper
lets you manually grab content that needs to be used again in the same
view, probably to share content from the template with the
layout:
benchmarkThe
benchmarkmethod takes an optional message argument and an optional logging level argument (:debug,:info,:warn, or:error). It records how long the wrapped code takes to run. It requires a block argument, so it usually looks something like:<% benchmark "It took this long:" do %> <%= my_long_method %> <% end % >
The message and the length of time it takes to run will end up in the logs.
cacheThe
cachemethod lets you flag fragments of your view to be kept for caching. Likebenchmark,cachewraps around the view code it’s meant to work on with a block argument:<% cache do %> <%= my_repetitive_method_that_should_be_cached %> <% end % >
You should only cache information that doesn’t change very often, but many HTML components are pretty stable.
captureThe
capturemethod wraps around view code and stores its output to a variable. You can ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access