This is the documentation for concrete5 version 5.6 and earlier. View Current Documentation

concrete5 uses its own cache class, which wraps the Zend_Cache library. This library is automatically loaded early in the dispatcher process, and is available throughout all aspects of concrete5. The cache library is suitable for storing PHP objects or simple data types like strings, numbers, etc...

Methods

Cache::set($type, $id, $obj, $expire = false)

Additional Arguments

  • $type - The type of object caching. This is a category. For example, all pages in concrete5 are cached with the type = "page".
  • $id - The id of the object being cached. The type and ID are combined when retrieving items from the cache.
  • $obj - The actual data to be cached. If the data is an object type, it will be serialized prior to storage.
  • $expire - Number of seconds after which a new object of this type will be requested. If this is set to false, the item will never expire and will have to be cleared manually.

Example

Cache an array of a large record set;

	 $db = Loader::db(); 	 $records = $db->GetAll('select * from LargeTable'); 	 Cache::set('largeRecordsList', false, $records);

Cache::get($type, $id, $mustBeNewerThan = false, $forceGet = false)

Retrieves data from the cache. Looks for the item matching $type and $id.

Additional Arguments

  • $mustBeNewerThan - if specified, the timestamp of the relevant cached data is checked. If the cached data is not newer than this data, the cache will not be hit.
  • $forceGet - The cache will be checked even if the cache has been disabled. Usage of this is not recommended.
$records = Cache::get('largeRecordsList', false); 

Cache::delete($type, $id)

Removes an item from the cache based on its $type and $id.

Cache::delete('largeRecordsList', false);

Cache::flush();

Removes all items from the cache.

Additional Example

The following example shows a way to use both $type and $id when caching an item.

Say we want to retrieve information about our two cat objects.

	$cat1 = PetCat::getByName('whiskers'); 	$cat2 = PetCat::getByName('mittens');

Within the PetCat class, we define getByName as follows:

	public function getByName($catName) { 		$r = Cache::get('PetCat', $catName); 		if ($r instanceof PetCat) { 			return $r; 		} 		 		// retrieve information about the pet cat into the $petCat object 		// .. 		// .. 		// end retrieve 		 		// set the data into the cache. 		Cache::set('PetCat', $catName, $petCat); 		return $petCat; 	} 	

Adapters

By default, concrete5 uses the Zend Framework File cache adapter (link to file cache adapter in Zend Framework.) However others are available, and offer greater performance. Consult the Zend Framework documentation about alternate cache adapters.

For example, the APC adapter provides significant performance improvements to built-in file caching, if your server has the APC accelerator software installed.

Defining the Cache Library

If you'd like to use a different cache library, add the following to your config/site.php

define('CACHE_LIBRARY', 'cachename');

Where cachename is "apc", "memcached", etc...

Setting Additional Zend_Cache Parameters

Certain Zend Cache adapters require additional options. These can be defined in config/site.php as follows:

$backendOptions = array('param1' => 'item', 'param2' => 'item'); $frontendOptions = array('param1' => 'item', 'param2' => 'item');  define('CACHE_BACKEND_OPTIONS', serialize($backendOptions)); define('CACHE_FRONTEND_OPTIONS', serialize($frontendOptions));

Wherever the Zend Framework documentation refers to backend and frontend options, simply make sure they are specified in an array, and then serialized and defined() properly.

Loading Conversation