【码】删除Redis有序列表中的某个元素
in 代码片段 with 0 comment

【码】删除Redis有序列表中的某个元素

in 代码片段 with 0 comment

代码来源:自己整理编写
说明:删除指定的一个元素,后别的元素的score会依次递减【必须在命令行下运行】

<?php

if (!isset($argc) || empty($argc))
    die ('allow run in console only.');
$serverId = getenv('serverId');
if (empty($serverId))
    die("need serverId.\n");

$delScore = $argv[1];
if( empty($delScore) ){
    die("need rank\n");
}

print "\nStart Refresh the Arena\n";
$config = \lib\Config::get('onlineServer');
if (empty($config)) {
    $host = '127.0.0.1';
    $port = 6379;
} else {
    $host = $config['host'];
    $port = $config['port'];
}
$redis = \lib\RedisCache::getInstance($host, $port)->connect()->connHandle();

$redisKey = 'key';
$redis->zRemRangeByScore($redisKey, $delScore, $delScore);

$start = $delScore+1;
$now = $redis->zRangeByScore($redisKey, $start, "+inf", ['withscores' => TRUE]);

foreach ($now as $value => $score) {
    $score = $score-1;
    $redis->zAdd($redisKey, $score, $value);
}

print "\nRefresh the Arena Success\n";
Comments are closed.