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:
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.
- (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.
- (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.
delete
Delete cached item. Returns true/false.
- (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.
- (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.
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.
Tips / Final notes
- 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.
Thanks for reading,
Ioannis Sannos