Skip to content
davydotcom edited this page Nov 13, 2012 · 3 revisions

Spud CMS was heavily optimized for caching from the outset. As a result of this caching architecture it may be best to explain from a (top-down) point of view how this caching is structured.

CMS supports 2 types of caching. They are "Full Page Caching" and "Action Caching" respectively. There also exists a database level "Liquid" cache used to store the pre-generated result of processing the liquid template directives.

Liquid Caching

This is the highest caching layer and is always enabled. Therefore it is best to describe what this is first.

When a page is created/saved, its content_blocks/partials are saved. Since these partials support use of liquid tags, we have to run these content blocks through a Liquid parser. In order to prevent overhead on the visitor side from using this template engine, we preprocess the liquid directives and store them in the database in a field called processed_content. This is important to know, for the event in which a custom liquid tag is added to the liquid processor. For example, spud_inquiries adds the {% inquiry Form Name %} tag. This works well, but what if we go and edit the form after we have saved a page? Well, since the page was preprocessed, nothing changes unless we reprocess this content. To do that we use sweepers on both the cms side and the inquiries side. For more details on how to accomplish this for your own Liquid tags, a future article will be created.

Full Page Caching

Full page caching is a method that allows rails to save rendered pages to the rails cache directory (defaults to the public folder). When configuring your web server, it can check for these files before asking rails for them. This allows the ruby processes to be skipped altogether, and files can be server statically. Spud automatically handles the removal of these files in the event the content of a page has changed. To enable full page caching you must add a configuration to spud_cms:

Spud::Cms.configure do|config|
  config.cache_mode = :full_page
end

This configuration can be added to the application.rb file. It is also important to note, that you will have to enable caching for rails in general, using the config.action_controller.perform_caching = true option, which can be found in production.rb

Action Caching

Action caching is similar to full page caching, except, instead of storing the rendered page to a file, we store it in memory. Rails has many action caching options, including the use of memcache or redis. To enable this cache mode, simply add this config to your application.rb:

Spud::Cms.configure do|config|
  config.cache_mode = :action
end

It is also necessary to turn on rails caching for the production environment.

For more information about rails and caching, check out the Rails guides. Rails Guides: Caching