Created
February 7, 2017 14:50
-
-
Save nrk/e07311af2316ea7e51719735274ded94 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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