Χρήση της Alternative PHP Cache (APC) στις εφαρμογές σας για το Elxis.
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 functionfetch($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 functionadd($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 functionstore($name, $group, $data, $ttl=0)
delete
Delete cached item. Returns true/false.
public static functiondelete($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 functiondeleteAll($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 functiongetInfo()
Example
Let's say we have a Testmodule (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 functiongetArticles() {
$elxis = eFactory::getElxis();
//If APC is enabled get the list of articles from APC
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.
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.
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.
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.