Site Cloner PHP Script
Bargain Hunter PHP Script
Job Hunter PHP Script
Site Login and Access Control PHP Script

Memcache With PHP and Linux Memcached

Using Memcache with PHP programming allows you to cache output and mySQL queries. Essentially, you can cache whatever you want. There are obvious benefits to caching; like retrieving web pages mush faster while using less server resources. If you dig some data up around the web, you will see that some very large websites use memcached.

In order to use memcache with php you need to install the memcached server on your Linux system and you need to install memcache module for PHP. There are zillions of tutorials which explain how to do this. You can Google your operating system and find the method for your chosen Linux distro.

Installing memcached server from source can be a solution if quick installation methods like ‘sudo apt-get install memcached’ or ‘yum install memcached’ fail. One tutorial can be found at http://www.cyberciti.biz/faq/howto-install-memcached-under-rhel-fedora-centos/. Another memcached server method is shown below. Be patient and you will have a successful installation.

Sample Memcached Server Installation

root# Install memcached pecl
root# yum install memcached php-pecl-memcache

Once you have memcached installed and working you can start and stop its services with the following commands:

root# /etc/init.d/memcached start
root# /etc/init.d/memcached stop
root# /etc/init.d/memcached restart

In addition to running the service, you can use chkconfig to start it upon boot.


To add the service to chkconfig,

root# /sbin/chkconfig --add memcached

To see if the service was added to chkconfig,

root# /sbin/chkconfig --list memcached
memcached           0:off   1:off   2:off   3:off   4:off   5:off   6:off

To add the service to start upon boot,

root# /sbin/chkconfig memcached on

To check that the service was changed with chkconfig,

root# /sbin/chkconfig --list memcached
memcached           0:off   1:off   2:on    3:on    4:on    5:on    6:off

Installing memcache for PHP can also be done via the command or using EasyApache with Web Host Manager.

Alternatively, you can install memcache from source. An example installation is shown below. The ‘foldername’ is one you choose and the path can be added to the php.ini file; which will be explained later in this tutorial.

root# mkdir tmp
root# cd tmp
root# wget http://pecl.php.net/get/memcache-2.2.7.tgz
root# tar -zxvf memcached-2.2.7.tgz
root# cd memcached-2.2.4
root# phpize && ./configure --enable-memcache && make
Find where php stores it modules from php.ini ie) /usr/local/lib/php/extensions/foldername
Copy modules/memcache.so to /usr/local/lib/php/extensions/foldername
root# cp modules/memcache.so /usr/local/lib/php/extensions/foldername

Once you have installed memcache you may have to add the extension to your php.ini file. Below are two methods that will point to the memcache.so file; one uses a typical path and the other uses the absolute path. In some cases, you will need to use the absolute path in order to make it usable.

extension = "memcache.so"
extension = "/usr/local/lib/php/extensions/foldername/memcache.so"

To see if memcache is usable you can use the command echo phpinfo(); and find memcache in the list.

PHP Memcache and Linux Memcached

Finally, if you are still with me, you try some simple scripts below to test your memcache. To make things easy, the cache is only set for 30 seconds so that you can see your data does in fact cache. If you hit a page on the first go, you should receive the first query from mySQL.

On the second go, you should receive the query from memcache. After 30 seconds, you can try again and it should hit mySQL again. On a production site, you probably want to set the cache for longer time periods.

Code

include('../public.inc'); //include the file that connects to database
session_start();
$db = connect();


/**
 * First Simple Example
 */

$mem = new Memcache();
$mem->addServer('localhost', 11211);
$mem->set('key', 'my_cached_string', 0, 30);
$test = $mem->get('key');
var_dump($test);
echo "<br/>";


/**
 * Second Simple Example
 */

$mem_instance = new Memcache();
$mem_instance->addServer('localhost', 11211);

$query = "SELECT * FROM tablename ORDER BY id ASC LIMIT 1";
$command2 = mysqli_query($db, $query);
$my_key = "KEY" . sha1("CAN BE ANYTHING. DUMMY FUNCTION SO THE GET METHOD WON'T FAIL");
//GET QUERY KEY
$result = $mem_instance->get($my_key);

if (!$result) {
    $command = "SELECT * FROM tablename ORDER BY id ASC LIMIT 1";
    $result = mysqli_query($db, $command);
    $row = mysqli_fetch_assoc($result);
    // SET QUERY KEY
    $mem_instance->set($my_key, $row, 0, 30);
    echo "Result from mysql<br/>";
    return false;
}

echo "Result from memcache:<br/>";
echo var_dump($result);
return 0;

?>

In a addition to memcached, you can use the mysql query cache to cache queries too. On top of all of this, you can use the Varnish cache which will bypass all of this and pull a cached Varnish page.

Troubleshooting Memcached Server

If you are testing the memcached server, you may want to flush the cache. The snippet below explains how to clear it.

telnet localhost 11211
flush_all
quit