Created
May 9, 2012 11:16
-
-
Save duggan/2643820 to your computer and use it in GitHub Desktop.
Test a memcache connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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