XCache is one of many opcode cachers that are available for PHP. There are many articles for nginx, apache, php and mysql best settings but I could not find one for XCache while I was struggling with configurations and I decided to put together this post.
I am assuming you already have XCache installed and you have either separate xcache.ini file or you are configuring inside php.ini. I would strongly urge you to install the XCache Admin panel or XCache dashboard. It helps a lot to get the best out of the settings.
You can use:
locate xcache | grep htdocs
to locate XCache Admin Panel. Once you locate it, you can copy it under your www folder. If you are using cPanel, it would be
cp /home/cpeasyapache/src/xcache/xcache-3/htdocs /home/your_user_name/public_html/xcache -R
Once you copy this folder then you need to enable the Admin configuration from the php.ini file.
extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20100525"
; password cleartext md5(your_password_and_copy_the_output_to_below)
XCache settings that need your attention
- xcache.admin.user – There is an HTTP authentication window when you access the admin page, this is the username
- xcache.admin.pass – You need to create md5 password for authentication.
- xcache.size – This is the most critical setting. xcache.size setting refers to the aggregate memory of all cache threads. So, each thread gets roughly size/count amount of memory. If you have multiple sites and around 4GB or more, 128M seems like a good number. Please beware, setting xcache.size to zero is equivalent to disabling xcache.
- xcache.count – This number divides total cache size into threads in order to increase the efficiency. If you have 128M xcache.size and you set the count as 4, that means each thread will manage 32M size of cache.
- xcache.slots – This is one of the most confused and least explained settings. According to XCache Wiki, Keeping xcache.slots small means more slots in memory thus better performance but more memory usage. I don’t really understand the logic behind it so I leave it as is.
- xcache.var_size – Variable cache is different then regular cache. Do you need this or not really depends on your application. See below for further info.
- xcache.var_count xcache.var_slots xcache.var_ttl are all alike their non-var versions so I skip them.
- xcache.ttl=0 means we want file to remain in the cache forever.
XCache settings that you can’t care less
- xcache.mmap_path=”/dev/zero” seems like beating the purpose but it is not. The reason you would write the output of a memory cache to a file is mainly to check and make sure you have the correct data. In other words, you would be doing read/write and read to make sure your data is safe. Dumping it to null means, you don’t care about the data consistency but just the performance and if something is broken in the cache, you are not too worried about it.
- xcache.readonly_protection=”Off” – According to XCache Wiki, The cache can be corrupted by accident, it might be modified along the process – before or after the cache so the cache with readonly permission won’t allow this happen.
- xcache.stat determines whether Xcache should check the filesystem to see if the file has changed or not.
XCache comes with two different caches
- Static cache that are generated by compiling your PHP scripts
- Variable cache that can be used by application through XCache API
If your application is not taking advantage of XCache variable cache then there is no need to enable variable cache so you would use xcache.var_size=”0″. W3 Cache for WordPress is able to xCache, there are other tools and plugins that are compatible with XCache but not all the software can take advantage of this storage.
I would highly recommend you to take a look at the Dashboard to see how XCache is performing for your environment and make the adjustments accordingly.
How do you reset XCache cache?
One last thing before finish, xcache var is reset when you restart the PHP service. If you are using PHP bound with Apache, restarting Apache will kill the cache. If you are using PHP like PHP-FPM, restarting PHP-FPM proxy service will clean the cache.
Please leave any comments and questions.