Cache is a component that stores data for future requests, so it can be served faster. If requested data is contained in cache, this request can be done by just reading the cache, no need to process it from beginning which is comparatively faster. A PHP website application’s ability is to display information data from a database. It’s one of most crucial core functions. The speed at which this data is retrieved is overall site performance. So, you can try to make your database calls as efficient as possible by Improve website performance through caching.
If your website were to have a caching mechanism, you can run these database queries faster because it’s run on a less frequent basis, Caching same data into a cache can be obtained from database for each access. This will Improve website performance faster response time because you access the database less. It’s possible to display the blog entries using a simple data from cache instead of accessing database using a complex query. A common problem with modern web application is C10k problem. There are two primary components that help solve this problem.
- Use asynchronous/non-blocking calls
- Effective caching
In this article, we will discussing Improve website performance through caching.
Using Eaccelerator and APC to Improve Website Performance
Most PHP intermediate code caching (OpCode caching), take the generated code by PHP compiler and store it in its final form (Compiled result). The result of this PHP file no longer needs to be compiled on each request. There are two popular opcode caching extensions : APC and eAccelerator.
Both extensions store the output of PHP scripts and both also have ability of allowing for arbitrary values to be stored in the cache too. APC and eAccelerator using system memory and the disk for caching. Memory caching provides better performance over hard drive based access. Both extensions will try to use memory first and most compiled PHP will fit nicely without having to resort to the disk.
Installing one of the opcode caching extensions is always a good idea. Not having one installed not only decrease ability for a server to handle load as a result, but also increase page load time by having to compile the PHP script on every execution. Because compiling a PHP script inherently requires a lot of computation and disk access. A server using opcode caching will always faster than a server without caching.
Installing and Configuring OpCode Cache
APC (Alternative PHP Cache) was originally developed by Community Connect Inc. APC is developed and deployed as a PECL extension and as such follow a standard method of installation and configuration. You can install APC by using command pecl install APC. You can download it from SVN and install it manually by using that command. It’s necessary to enable the extension in the php.ini file.
eAccelerator is another popular opcode caching solution. It’s open source project (Turck MMCache). It’s Licensed under the GNU General Public License and like APC they provide its full source code. However, its mantained independently from PHP community / Public.
To Install eaccelerator on a Linux based system, use these commands :
1 ./configure --with-eaccelerator-shared-memory
1 make install
1 mkdir -p /var/cache/eaccelerator
1 chmod 0777 /var/cache/eaccelerator
The parameter passed to configuration for enabling user caching. if you don’t user cache, you can exclude it. The next step is configuring eaccelerator. You can do it manually by adding few lines to php.ini file :
Like APC, eaccelerator does not automatically evict data form the cache unless a time is specified. Biggest disadvantage of a local user cache is that it produces same copies data across multiple servers. Local user cache is not good if you have multiple server. To solve that problem maybe you can try to use Memcached.