Skip to content

Instantly share code, notes, and snippets.

@duggan
Created May 9, 2012 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duggan/2643820 to your computer and use it in GitHub Desktop.
Save duggan/2643820 to your computer and use it in GitHub Desktop.
Test a memcache connection
<?php
/**
* Some basic connectivity tests for a memcache server.
*
* $host string Memcache server address to connect to
* $port int Memcache server port
* $key string A key for setting a test value
* $var string A test value to be set
*/
$host = "localhost";
$port = 11211;
$key = "TestKey";
$var = "TestValue";
print "<pre>" . PHP_EOL;
print "Memcache Test" . PHP_EOL;
print "-------------" . PHP_EOL;
try {
$cache = new Memcache();
if ($cache->connect($host, $port)) {
print "Connected to memcache." . PHP_EOL;
} else {
print "Failed to connect." . PHP_EOL;
}
$result = $cache->get($key);
if ($result) {
print "Retrieved value: $result" . PHP_EOL;
} else {
print "Could not retrieve key: $key" . PHP_EOL;
}
if ($cache->set($key, $var)) {
print "Successfully set key." . PHP_EOL;
} else {
print "Failed to set key." . PHP_EOL;
}
} catch (Exception $e) {
print $e->getMessage();
}
print "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment