Store
Community Documentation

v3 Knowledgebase

Introduction to Cache

Introduction to Caching: June 2013

This article will discuss the two most basic types of cache available for use with phpfox. These are quite easy to implement and give a good performance boost to you site. Whilst also reducing the load on your MySQL sever, delaying or totally putting of the need to upgrade your MySQL configuration.

If you have site performance issues or just want to speed up your site the first place to start is with implementing a caching solution.

PHP Accelerators

PHP is not a compiled language, so every time you make a request to run php code, the file is compiled then executed. PHP accelerators work, by placing the compiled code into cache (shared memory) and serving those request from that cache, the next time a user needs the same file it servered straight from the cache. So its quicker and places less load on the server. The data stored in the cache is not persistant e.g if you reboot the server, the cache is cleared.

If your on a shared host your hosting provider will almost certainly be running some form of PHP accelerator. So you should get in touch with them, about your options.

If your running on a VPS or dedicated host your site would most certainly benefit by using a php accelerator. The remainder of this article assumes you are running a VPS or a dedicated server.

One of the most popular PHP-Accelerators is PHP-APC though a few exist. PHP-APC is probably a good choice as it will be included in the release of PHP 6 and most linux distros support it.

For example to install in on Ubantu 12.04 just type:

sudo apt-get install php-apc

After you have installed it needs to be configured.

You need to configure the apc.ini file.

On Ubuntu 12.04 systems its usually located at /etc/php5/conf.d/apc.ini

or you can find it by typing:

updatedb
locate apc.ini


This should display the path to the apc.ini file.

At a min you should include the following in your apc.ini

extension=apc.so
apc.enabled=1
apc.shm_segments=1
apc.shm_size=32M
apc.optimization=2
apc.num_files_hint=1000
apc.user_entries_hint=1000
apc.ttl=288000
apc.gc_ttl=3600
apc.max_file_size=2M
apc.stat=1
apc.write_lock=1


Key Settings

extension=apc.so
The apc module that php loads (basically turns it on)

apc.shm_size=xxM
The size of the cache file. If you set this to small you don't get full benefit of apc. To big and you waste memory. Install php.apc and monitor your memory usage to determine the right size.

apc.write_lock=1
If you run a large busy site you should make sure that apc.write_lock is set to 1 this stops the same files being cached multiple times.

The above is a start and you should tune this file to your sites needs. If you read the configuration options information and understand them you will be able to tune APC to suit your requirements. A properly configured apc.ini file can make quite a difference to your sites performance. Read the apc config settings to better understand what all the options are and what they do. http://www.php.net/manual/en/apc.setup.php

Memcached.

Memcached is the next cache solution you should implement, if your on a shared host the hosting provider may already have memcached servers set up for you. If your on a VPS or use dedicated hosting same thing applies or you may want to install and configure your own memcached server(s).

Memcached is different from APC, in that it can store data retrieved from request to the database and server them up to user at a latter time. So it reduces the load on your MySql db. It’s a very standard part of any modern web application. Its used by small and super large sites alike. Like APC memcached data is not persistant and will be cleared if the memcached server is restarted. Though solutions to this are available. see: http://www.phpfox.com/kb/article/524/couchbase-cac...

For more info on memcached see:

http://memcached.org/
https://code.google.com/p/memcached/wiki/NewStart


Once you have installed memcached follow this guide http://www.phpfox.com/kb/article/94/how-to-enable-... to enable it in phpfox

PHP APC can and should be used with memcached as they work together and in different ways to speed up your site.