Skip to content

Instantly share code, notes, and snippets.

@ZuZuD
Last active March 27, 2018 17:19
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/68725dd8e2227be0d35e0e3b69798d3a to your computer and use it in GitHub Desktop.
Save ZuZuD/68725dd8e2227be0d35e0e3b69798d3a to your computer and use it in GitHub Desktop.
ElasticCache auto discovery PHP 7.0 - POC non-consistency getAllKeys() method
<?php
function printkv($store) {
if (is_array($store)) {
foreach($store as $item) {
echo "Key: ".$item['key']." Value: ".$item['value']."\n";
}
}
}
function listKeys($mem)
{
$keys = $mem->getAllKeys();
$mem->getDelayed($keys);
$store = $mem->fetchAll();
return $store;
}
function printNodeKeys($srv)
{
foreach($srv as $node) {
echo "Host: ".$node['host']." Port: ".$node['port']."\n";
$mem = new Memcached();
$mem->addServer($node['host'], $node['port']);
$store = listKeys($mem);
#if (sizeOf($store) > 0) { printkv($store); }
printkv($store);
}
}
$server_endpoint = "<cluster cfg endpoint>";
$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);
# set amount of keys
$max = 4;
for($i = 0; $i <= $max;$i++) {
$dynamic_client->set($i, "myvalue_".$i);
}
# Display keys from the cluster
echo "-------------- getallKeys(cluster)\n";
$store = listKeys($dynamic_client);
printkv($store);
# Display keys by node
$serverlist = $dynamic_client->getServerList();
echo "--------------getAllKeys(node)\n";
printNodeKeys($serverlist);
# Display keys from the cluster
echo "-------------- getallKeys(cluster)\n";
$store = listKeys($dynamic_client);
printkv($store);
echo "--------------getKeyOneByOne(cluster)\n";
for($i = 0; $i <= $max;$i++) {
echo $i." ".$dynamic_client->get($i)."\n";
}
# Delete all the keys from cluster
echo "--------------DeleteAllKeys(cluster)\n";
foreach($store as $item) {
$dynamic_client->delete($item['key']);
}
# Check each nodes are empty
echo "--------------CheckDeleted(cluster)\n";
$store = listKeys($dynamic_client);
printkv($store);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment