Moodle Plugins directory: Alternative PHP cache (APC) | Moodle.org
Alternative PHP cache (APC)
Information and advice on using APC within Moodle
APC provides a shared application cache that is usually very limited in size but provides excellent performance. It doesn't provide the ability to configure multiple instances of itself and as such within Moodle you are only able to create a single APC cache store instance. Because of its incredible performance but very limited size it is strongly suggested that you map only small, crucial caches to the APC store.
Another important thing to understand about the APC store is that it provides no garbage cleaning, or storage reclamation facilities. As such cache data will persist there until APC is restarted or the store is purged. On top of that once the store is full requests to store information within the cache fail until there is once more sufficient space. Because of this it is recommended that you regularly purge or restart APC. Also recommended is to map a secondary application cache instance to any definition with the APC mapped. This ensures that if it does indeed full up that an alternative cache is available.
Installation of APC
It is recommended that you read through the APC documentation http://www.php.net/manual/en/book.apc.php before beginning with this plugin. The above documentation recommends installing the PECL APC extension that can be found at http://pecl.php.net/package/apc. http://www.php.net/manual/en/install.pecl.php contains information on installing PECL extensions.
Its also worth noting for this those using Linux that there is usually a php5-apc package that can be installed very easily. If you have installed PHP under Linux through a package manager then this will be by far the easiest way to proceed.
Once installed ensure you restart your web server before proceeding.
Installation within Moodle
Browse to your site and log in as an administrator. Moodle should detect that a new plugin has been added and will proceed to prompt you through an upgrade process to install it. The installation of this plugin is very minimal. Once installed you will need to need to create an APC cache store instance within the Moodle administration interfaces.
Making use of APC within Moodle
Installing this plugin makes APC available to use within Moodle however it does not put it into use. The first thing you will need to do is create an APC cache store instance. This is done through the Cache configuration interface.
- Log in as an Administrator.
- In the settings block browse to Site Administration > Plugins > Caching > Configuration.
- Once the page loads locate the APC row within the Installed cache stores table.
- You should see an "Add instance" link within that row. If not then the APC extension has not being installed correctly.
- Click "Add instance".
- Give the new instance a name and click "Save changes". You should be directed back to the configuration page.
- Locate the Configured cache store instances table and ensure there is now a row for you APC instance and that it has a green tick in the ready column.
Once done you have an APC instance that is ready to be used. The next step is to map definitions to make use of the APC instance.
Locate the known cache definitions table. This table lists the caches being used within Moodle at the moment. For each cache you should be able to Edit mappings. Find a cache that you would like to map to the APC instance and click Edit mappings. One the next screen proceed to select your APC instance as the primary cache and save changes. Back in the known cache definitions table you should now see your APC instance listed under the store mappings for the cache you had selected. You can proceed to map as many or as few cache definitions to the APC instance as you see fit.
That is it! you are now using APC within Moodle.
Bugs, feature requests, help, and further information
For bug and feature requests please create issues at tracker.moodle.org. For help please visit the Hardware and performance forum. For more information please visit https://moodle.org/plugins/view.php?plugin=cachestore_apc
The data store provided by APC is extremely fast, however it is usually limited in size (of course you can configure this). If the data store becomes full requests to set any new data fail.
This ties into your questions.
In regards to selecting cache definitions I would suggest selecting a couple of really core definitions to begin with, definitions like lang strings and config are used on every single page so are probably a good couple of start with.
Once configured you can monitor APC checking up on how much storage is being used. Ensure there is always storage available. From there you should be able to either tweak the data store size of APC, or add additional definitions to it.
As for primary and final store, I'd highly recommend not selecting APC for both. The idea behind stacking the stores is that should your primary store not be available (if it were full for example) then the secondary store would be available for use.
It's not ideal to have things fall back to a secondary cache obviously, however it beats having to generate data for every request on top of trying to get it from a cache.
I hope that answers your questions.
Many thanks
Sam
Please vote for the issue if you would like to see it become part of Moodle, or comment if you have thoughts to share.
Can you confirm which directory we copy the plugin into, I assumed /mod but I get an error trying to install on 2.4
Plugin "mod_apc" is defective or outdated, can not continue, sorry.
Thanks
Rob.
PHP Warning: apc_cache_info(): No APC info available. Perhaps APC is not enabled? Check apc.enabled in your ini file in /var/www/elo/html/cache/stores/apc/lib.php on line 257
What am I doing wrong?
Notice: Undefined index: info in /srv/htdocs/cache/stores/apc/lib.php on line 259
Warning: apc_delete(): apc_delete() expects a string, array of strings, or APCIterator instance. in /srv/htdocs/cache/stores/apc/lib.php on line 259
Thanks for the heads up, I had fixed this locally but not shared it here - oops.
think that, in order to use OPCache (really recommended), instead of using the whole APC, you can just install APCu (the caching part of APC). It provides PHP API compatibility with the old APC, so should work as a simple replacement. (Disclosure: I've not tested APCu, just sharing it's supposed to be the user cache alternative for people moving to OPCache from APC).
Some links:
https://github.com/krakjoe/apcu http://pecl.php.net/package/APCu
The most recent commits remove compatibility with everything <2.9Dev, so it no longer works with 2.6, 2.7, or 2.8.