Defining a Cache

For large XenForo installations, it can be advantageous to define a cache in order to save on processing time and database queries.

XenForo has the ability to store and retrieve various data from a cache, which can be configured in your library/config.php file.

Example Cache Types

The XenForo cache is an implementation of the Zend_Cache_Core front-end, and an extension of Zend_Cache_Backend for the back-end.

Cache Front-End

Configuring the front-end is easy, and involves simply adding the following lines to your library/config.php file:

$config['cache']['enabled'] = true;
$config['cache']['frontend'] = 'Core';
$config['cache']['frontendOptions']['cache_id_prefix'] = 'xf_';

You may alter the 'cache_id_prefix' values to suit your own needs. Advanced users may change other Zend_Cache_Core front-end options to fit their needs.

If you wish to read and write XenForo sessions to the cache, add the following line to your library/config.php file:

$config['cache']['cacheSessions'] = true;

Note that your cache must have enough space to hold the sessions, or users may not be able to login properly. We do not recommend writing sessions to the cache if you are using APC as your cache back-end.

The following are a few examples of cache back-ends that are usable by XenForo:

File Back-End

The simplest kind of cache back-end is a simple file store, which stores cache data as files in the temporary directory of your server.

Configuration is equally simple:

$config['cache']['backend'] = 'File';

Full list of file cache options.

APC Back-End

If you have APC (Alternative PHP Cache) installed on your server, you may use it to store cache data.

Note that some versions of APC may be susceptible to cache-slam issues, so if you do enable APC caching of data (in addition to the PHP opcode cache that it maintains automatically without requiring any configuration), you should monitor your server carefully for a time to ensure that it is running smoothly.

$config['cache']['backend'] = 'Apc';

Full list of APC cache options.

Memcached Back-End

Memcached is an advanced storage mechanism for shared data, and can be used as a back-end for XenForo caching.

It requires slightly more configuration than some simpler back-ends, for example:

$config['cache']['backend'] = 'Memcached';
$config['cache']['backendOptions'] = array(
	'compression' => false,
	'servers' => array(
		array(
			// your memcached server IP /address
			'host' => 'localhost',
			
			// memcached port
			'port' => 11211,
		)
	)
);

Full list of memcached cache options.