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.

error_reporting(E_ALL|E_STRICT);

ini_set('display_errors', true);

$mem = new memcached();

$mem->addServer("127.0.0.1", 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:

127.0.0.1/memcache_test.php

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.


Var den här artikeln till hjälp?
Tack för din feedback. Ring vårt supportnummer eller starta chattalternativet ovan om du vill prata med en medarbetare på kundtjänst.
Vi är glada att vi kunde hjälpa till! Finns det något mer vi kan göra för dig?
Det var tråkigt att höra. Berätta vad som var krångligt eller varför lösningen inte hjälpte dig med problemet.