Skip Navigation

Presentation Slides (HTML)

Understanding and Using Page and Object Caching

What is caching?

"In computer engineering, a cache is a component that transparently stores data so that future requests for that data can be served faster."

http://en.wikipedia.org/wiki/Cache


Reason supports 2 types of caching

Page Caching

  • Caches HTML in the filesystem
  • Keyed by URL
  • Managed in Reason Admin
  • Specific to Reason CMS

Object Caching

  • Just for developers
  • Supports multiple data stores
  • Not really just for "objects"
  • Available in carl_util - not Reason CMS specific

Page Caching


Enabling Page Caching in Reason

  • Page caching is built into the Reason core.
  • It is enabled on a site by site basis in the Master Admin interface.

DEMO: Site entity editing interface for the "About Carleton" site:

Generating a Cached Page

  • generate_page.php checks if the URL requested is in the cache and delivers it.
  • Apache rewrite rule provides generate_page.php a site_id and page_id.
  • generate_page.php checks if a cache directory exists in the file system for the page.
  • generate_page.php checks if the specific URL is cached, and if so, delivers it.
  • if caching is on and the URL isn't cached, the html for the page is cached for future requests.

Page Caching before Reason 4.0 final

A database connection used to verify site and page id info before the caching layer was invoked.

In Reason 4 Beta 8 and below, page caching fails if the database goes down.

In the current release (4.0), cached pages are delivered even if the database is down (and they are delivered much faster).


When the page cache is NOT used

  • The page request includes $_POST data
  • The user is logged in
  • The user is a developer
  • The request is an module_api request

In Reason 4.0, a developer who is not logged in can test caching by passing test_cache with some value in the query string.


When to use page caching

  • Page caching makes the most sense for public facing, high traffic websites.
  • Page caching can be selectively enabled to handle expected or unexpected traffic surges to particular sites.

Carleton uses page caching on our admissions site, which has higher page generation times than most sites, and is mostly used by prospective students and parents who are not logged in.

DEMO: Lets see the impact of caching on the admissions site front page


How to clear a page cache

  • Page caching will cause updates made in the the Reason administrative interface to not appear for up to one hour.
  • However, it is possible to clear a page cache from the administrative interface "preview" of a page.

DEMO: Lets clear a cache


Simple change that I should make - or you could!

It would be useful for the minisite page content manager to zap the cache if the page content was changed. This would minimize the "why is my page not updating" calls from people with cached sites.


Object Caching


Object Caching - Storage options

The Object Caching system has 3 built-in storage options. After choosing a default, you typically don't have to concern yourself with how data is stored.

  • File system (default)
  • MySQL database
  • Memcached

Object Caching - Setup

Most cache configuration should be done in reason_package/settings/object_cache_settings.php.

Key options include:

  • default cache type (defaults to file)
  • default lifespan (defaults to 1 hour)

If you choose to use MySQL or Memcached, there are some additional options you can set.

DEMO: Look at config file.


Object Caching - Custom Cache Types

You can create custom cache types if you want to use a different storage option than what is provided.

A custom cache type must support these methods:

  • fetch - return the cached content or boolean FALSE
  • set - saves a cache
  • clear - deletes a cache
  • validate - checks basic setup for cache type

Super simple example

With no special configuration, the following code will fetch a cached copy of $obj from if it is no older than one hour. If it is not available or too old, it will populate $obj and then cache it.

$cache = new ObjectCache('cache_unique_id');
$obj =& $cache->fetch();
if (!$obj)
{
$obj = $this->get_stuff();
$cache->set($obj);
}

Note that we don't actually care about what the cache type is ... it just works.


More complex example

We setup and use a MySQL cache with a named database connection and table where cached items live forever:

$cache = new ObjectCache();
$cache->set_cache_type('db');
$cache->set_cache_params( array ( 'db_conn' => my_db_conn', 'db_table' => 'my_db_table'));
$cache->set_default_lifespan(-1); // last forever
$cache->init('cache_unique_id');
$obj =& $cache->fetch();
if (!$obj)
{
$obj = $this->get_stuff();
$cache->set($obj);
}

Next time we run this code $obj will be populated and cached.


Object Caching in Practice

Anytime a module does something that is computationally expensive (usually due to a query) and involves data that does not have to be absolutely up to date, object caching can yield performance enhancements.

  • 1-5 minute object caches can help you withstand traffic bursts.
  • Caches can be used for a "permanent" store, but be careful.

DEMO: Lets look at caching in the geocoder class.


Questions?