Skip to content

Instantly share code, notes, and snippets.

@nrk
Created February 7, 2017 14:50
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 nrk/e07311af2316ea7e51719735274ded94 to your computer and use it in GitHub Desktop.
Save nrk/e07311af2316ea7e51719735274ded94 to your computer and use it in GitHub Desktop.
<?php
require __DIR__.'/../autoload.php';
function group_keys_by_slot(\Predis\Cluster\StrategyInterface $strategy, array $keys) {
$keysBySlot = [];
foreach ($keys as $key) {
$slot = $strategy->getSlotByKey($key);
if (isset($keysBySlot[$slot])) {
$keysBySlot[$slot][] = $key;
} else {
$keysBySlot[$slot] = [$key];
}
}
return $keysBySlot;
}
$client = new \Predis\Client($parameters, ['cluster' => 'redis']);
$strategy = $client->getConnection()->getClusterStrategy();
$keys = ['{tag1}:abc', '{tag2}:123', '{tag1}:def', '{tag2}:456'];
$keysBySlot = group_keys_by_slot($strategy, $keys);
foreach ($keysBySlot as $slot => $keys) {
$client->del($keys);
}
/*
array(2) {
[4112]=>
array(2) {
[0]=>
string(10) "{tag1}:abc"
[1]=>
string(10) "{tag1}:def"
}
[8307]=>
array(2) {
[0]=>
string(10) "{tag2}:123"
[1]=>
string(10) "{tag2}:456"
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment