Testing the functionality of Memcache using PHP

Memcache is a great tool to use to speed up your website. This article will show you how to test the functionality of memcache using a simple php script.
DIFFICULTY Basic - 1 | Medium - 2 | Advanced - 3
TIME REQUIRED 15-30+ min
RELATED PRODUCTS Linux-based VPS or dedicated servers

This article will show you how to test the functionality of memcache using a php script. If you do not have memcache installed, please see one of these supporting articles.

How to install Memcache on CentOS

How to install Memcache on Ubuntu

For the example, we will be using an Ubuntu server, and testing the script from the default document root; the file name will be memcache_test.php.

To begin change directory to the document root:

cd /var/www/html/

Then, lets create a new file and call it memcache_test.php

vi memcache_test.php

In this example we are using vi, but you can use vim or nano

Using this snippet of code you can test the functionality of memcache, we have included error reporting to provide troubleshooting steps if your code does not work.


ini_set('display_errors', true);

$mem = new memcached();

$mem->addServer("", 11211);

$result = $mem->get("Test");

if ($result) {

echo $result;

} else {

echo "Test key not found, adding key";

$mem->set("Test", "I found a match, memcache is working") or die("Nothing Saved...");



To test the code, simply visit your servers IP in the browser followed by the file name:

If the script worked, you should see Test key not found, adding key. On refresh it should say I found a match, memcache is working

Congratulations, you have successfully verified that memcache is working.

Oliko tästä artikkelista hyötyä?
Kiitoksia palautteestasi. Jos haluat puhua asiakaspalveluedustajan kanssa, käytä yllä näkyvää puhelinnumeroa tai chat-vaihtoehtoa.
Kiva, että meistä oli apua. Voimmeko tehdä jotain muuta?
Olen pahoillani asiasta. Kerro meille, mikä on epäselvää tai miksi ratkaisu ei toiminut oman ongelmasi kohdalla.