Skip to content

Instantly share code, notes, and snippets.

@ZuZuD
Created March 27, 2018 17:10
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 ZuZuD/c281df71a79a4a14137c187715988b3d to your computer and use it in GitHub Desktop.
Save ZuZuD/c281df71a79a4a14137c187715988b3d to your computer and use it in GitHub Desktop.
ElasticCache memcached (memcached.so) PHP5 auto-discovery getAllKeys() workaround
# workaround for bug on getallKeys() on version superior to 1.4.22
# https://bugs.launchpad.net/libmemcached/+bug/1534062
<?php
function listKeys($srv,$max)
{
foreach($srv as $node) {
echo "Host: ".$node['host']." Port: ".$node['port']."\n";
$mem = new Memcached();
$mem->addServer($node['host'], $node['port']);
for ($j = 0; $j < $max; $j++) {
if ($mem->get($j)) { echo "Key: ".$j." Value: ".$mem->get($j)."\n";}
}
}
}
function removeKeys($srv,$port,$max)
{
$mem = new Memcached();
$mem->addServer($srv,$port);
$mem->setOption(Memcached::OPT_CLIENT_MODE, Memcached::DYNAMIC_CLIENT_MODE);
$mem->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
for ($j = 0; $j < $max; $j++) {
$mem->delete($j);
}
}
$server_endpoint = "<elasticcache cfg cluster>";
$server_port = 11211;
$dynamic_client = new Memcached();
$dynamic_client->setOption(Memcached::OPT_CLIENT_MODE, Memcached::DYNAMIC_CLIENT_MODE);
$dynamic_client->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
$dynamic_client->addServer($server_endpoint, $server_port);
$max = 10;
for($i = 0; $i < $max;$i++) {
$dynamic_client->set($i, "myvalue_".$i);
}
$serverlist = $dynamic_client->getServerList();
listKeys($serverlist,$max);
removeKeys($server_endpoint,$server_port,$max);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment