优化代码

main
gzydong 2021-05-21 17:43:41 +08:00
parent 20735e551e
commit 0a9dcb11af
6 changed files with 14 additions and 20 deletions

View File

@ -127,7 +127,7 @@ class ChatMessageConsumer extends ConsumerMessage
if (isset($data['event'])) {
// [加锁]防止消息重复消费
$lockName = sprintf('ws-message:%s-%s', SERVER_RUN_ID, $data['uuid']);
if (LockRedis::getInstance()->lock($lockName, 60)) {
if (!LockRedis::getInstance()->lock($lockName, 60)) {
return Result::ACK;
}

View File

@ -63,6 +63,6 @@ class ChatMessageProducer extends ProducerMessage
*/
private function uuid()
{
return Str::random() . mt_rand(100000, 999999) . uniqid();
return Str::random(8) . uniqid();
}
}

View File

@ -25,18 +25,11 @@ class HashRedis implements HashRedisInterface
*/
public function get(string ...$key)
{
$func = function ($k) {
return (string)$this->redis()->hGet($this->getKeyName(), $k);
};
if (func_num_args() == 1) return $func($key[0]);
$array = [];
foreach ($key as $arg) {
$array[$arg] = $func($arg);
if (func_num_args() == 1) {
return (string)$this->redis()->hGet($this->getKeyName(), $key[0]);
}
return $array;
return $this->redis()->hMGet($this->getKeyName(), $key);
}
/**

View File

@ -49,7 +49,7 @@ class RemoveWsCacheCommand extends HyperfCommand
foreach ($arr as $run_id => $value) {
go(function () use ($socket, $run_id) {
$socket->removeRedisCache($run_id);
$socket->removeRedisCache(strval($run_id));
});
}

View File

@ -53,7 +53,7 @@ class TestCommand extends HyperfCommand
//var_dump($string->ttl('yuandong'));
//var_dump($string->isExist('yuandong'));
//$hash = HashRedis::getInstance();
$hash = HashRedis::getInstance();
//for ($i = 0; $i < 10; $i++) {
// $hash->add('user:' . $i, (string)rand(0, 100));
//}
@ -61,7 +61,7 @@ class TestCommand extends HyperfCommand
//var_dump($hash->all());
//var_dump($hash->isMember('user:1'));
//var_dump($hash->rem('user:3'));
//var_dump($hash->get('user:6'));
//var_dump($hash->get('user:6','user:7'));
//$hash->incr('user:6',11);
//var_dump($hash->get('user:6'));
@ -79,11 +79,12 @@ class TestCommand extends HyperfCommand
//var_dump($set->randMember(2));
//$zset = ZSetRedis::getInstance();
//for ($i = 0; $i < 10; $i++) {
// $zset->add('user:' . $i, rand(0, 100));
//for ($i = 1; $i < 100; $i++) {
// $zset->add('user:' . $i, $i);
//}
//$zset->delete();
//var_dump($zset->count());
//var_dump($zset->all());
//var_dump($zset->rank(2,10));
//var_dump($zset->getMemberScore('user:2'));
//var_dump($zset->getMemberRank('user:2'));
//var_dump($zset->rank());
@ -126,6 +127,6 @@ class TestCommand extends HyperfCommand
//$model = new TalkService();
//$model->talks(2054);
var_dump(FriendRemark::getInstance()->read(2054,2055));
//var_dump(FriendRemark::getInstance()->read(2054,2055));
}
}

View File

@ -86,7 +86,7 @@ class SocketClientService
*/
public function isOnlineAll(int $user_id, array $run_ids = [])
{
if (empty($run_ids)) $run_ids = $this->getServerRunIdAll();
$run_ids = $run_ids ?: $this->getServerRunIdAll();
foreach ($run_ids as $run_id => $time) {
if ($this->isOnline($user_id, $run_id)) return true;