Elxis APC

Last update by Elxis Team

How to use Alternative PHP Cache (APC) in your Elxis projects.

Elxis CMS, along with the standard file cache has support for the Alternative PHP Cache (APC). APC allows you to cache intermediate PHP code (opcode) in server's memory (RAM). Getting cached data with APC is extremely fast as they are stored in RAM. Although it is not wise to store in APC large sized data it is ideal to save there things that are hard to calculate, slow down our script or needed freequently. In this guide we will present you the public methods of the elxisAPC library and a usage example.

Requirements

APC is available by default in newer PHP versions but it might not be enabled. If not, first enable APC in server's PHP installation and restart the HTTP web server. Then you can enable it in Elxis configuration. To check if APC is enabled in Elxis:

$elxis = eFactory::getElxis();
if ($elxis->getConfig('APC') == 1) {
//APC is enabled
}

elxisAPC methods

Public methods of the elxisAPC library. Note that all methods are static.

fetch

Fetch cached opcode. Returns cached data or false (boolean) in case of error.

public static function fetch($name, $group='')
  • (string) name An identification name for the cached opcode.
  • (string) group You can organize similar cached items in groups. Optional.

add

Caches a variable in the data store, only if it's not already stored. Returns true/false.

public static function add($name, $group, $data, $ttl=0)
  • (string) name An identification name for the cached opcode. Must be unique in group.
  • (string) group You can organize similar cached items in groups. If you dont want to use groups provide an empty string.
  • (string) data A variable containing the data to store in cache.
  • (string) ttl Time to live. The time, in seconds, to store data into cache. After this time expires the cached item will be refreshed. If you live this to 0 the default value 7200 (2 hours) will be used.

store

Same as add with the difference that the data with the same key can be overwritten. Returns true/false.

public static function store($name, $group, $data, $ttl=0)

delete

Delete cached item. Returns true/false.

public static function delete($name, $group='')
  • (string) name The name of the item to delete.
  • (string) group The items's group name. Optional.

deleteAll

Delete all cached items, or those belonging to a group. Returns true/false.

public static function deleteAll($group='')
  • (string) group The group name to delete. Optional.

getInfo

Get APC usage information. Returns an array containing statistical information for the server and the site (Elxis) usage of APC.

public static function getInfo()

Example

Let's say we have a Test module (mod_test) in which we want to store several variables into APC cache. Why we want to do this? Because their calculation is CPU expensive, requires many or complex queries to the database, require to open a connection to a remote host, and for any other reason. As we have more than one variable to store for this module we will use a group called modtest (you can name it what ever you like) in order the names of these variables not to conflict with other items stored in APC.

//Sample function getData returns a list of articles from APC if it is enabled or generates it.
public function getArticles() {
$elxis = eFactory::getElxis();

//If APC is enabled get the list of articles from APC
if ($elxis->getConfig('APC') == 1) {
$articles = elxisAPC::fetch('listofarticles', 'modtest');
//If $articles is not false then APC succeed and we return this variable
if ($articles !== false) { return $articles; }
}

//List of articles doesn't exist in APC or has expired.
//Calculate the list of articles here (not part of this example).
$articles = doSomething();

//If we succeed generating the list of articles ($articles), and APC in enabled, save this variable in APC for the later usage.
if ($articles && ($elxis->getConfig('APC') == 1)) {
elxisAPC::store('listofarticles', 'modtest', $articles, 14400);
}
return $articles;
}

Tips / Final notes

  1. The variable to store in APC can be of any type. A string, an integer, an array. You can even store an object. By creating a multi-dimensional array you can store many mixed things in one variable.
  2. In some cases you can check if standard file caching is enabled in a module (parameter cache) and if not use APC instead to speed up module.
  3. Modules may have multiple instances so using a name like listofarticles in the above example is not enough. A good way is to apend to this string the module instance id, which is unique, or a long random number.
  4. In Elxis configuration there is an APC ID option. This is used to separated APC items stored by variours Elxis sites on the same server. Elxis generates a random number for this option but you can set it to what ever you like. Just make sure no other Elxis site uses the same APC ID on the server.

Thanks for reading,
Ioannis Sannos

It has been read 6955 times
Elxis cache
Next article
Elxis cache

You are free to copy, distribute and transmit the articles in this site for non-commercial purposes.
Creative Commons 3.0