Nov 132014
Article PHP

If there is enough main memory available in the server where an application is running, a memory cache can significantly improve the performance of the application.

This post is a tutorial on how to install a “memcached” service on a server running linux Debian, and how to use the service in a PHP script.

1. Installing the memcached service

memcached can be found as a module in the standard Debian repository. Therefore, it can be easily installed with “apt-get”:

memcached is installed as a service, and can thus be started and stopped using the “service” command:

Additionally, the installation adds a script /etc/init.d/memcached, to allow the service to start on system boot.

2. Using memcached from a PHP script

2.1. Installing the php5-memcached module

To use the memcached service in a PHP script, there are two options: Use the older “php-memcache” extension, or else the new “php-memcached” extension, that adds extra functionality. This post explains how to use the second module.

The first step is the installation of the extension. As before, a call to apt-get can be used for this installation:

 2.2. Test script

To test the functionality, a simple script can be used, as follows:

The fist time the test script is run, the output is:

The second time the test script is run, the output is:

3. Configuring memcached

The configuration of the memcached service is in the file /etc/memcached.conf

Among other, the main parameters that can be tweaked in that file are:

– The maximum amount of RAM memory used by the service. The default value is 64 MB (-m 64)

– The IP address and TCP port where memcached will listen for requests. The default is localhost (-l, port 11211 (-p 11211)



Index of posts on PHP programming


 Posted by at 4:52 pm

 Leave a Reply