diff --git a/app/Amqp/Consumer/ChatMessageConsumer.php b/app/Amqp/Consumer/ChatMessageConsumer.php index 9e0992e..9571e7b 100644 --- a/app/Amqp/Consumer/ChatMessageConsumer.php +++ b/app/Amqp/Consumer/ChatMessageConsumer.php @@ -11,11 +11,9 @@ declare(strict_types=1); namespace App\Amqp\Consumer; -use App\Model\Group\Group; use Hyperf\Amqp\Annotation\Consumer; use Hyperf\Amqp\Result; use Hyperf\Amqp\Message\ConsumerMessage; -use Hyperf\Redis\Redis; use Hyperf\Amqp\Message\Type; use Hyperf\Amqp\Builder\QueueBuilder; use PhpAmqpLib\Message\AMQPMessage; @@ -26,9 +24,11 @@ use App\Model\Chat\ChatRecordsCode; use App\Model\Chat\ChatRecordsFile; use App\Model\Chat\ChatRecordsInvite; use App\Model\Chat\ChatRecordsForward; +use App\Model\Group\Group; use App\Service\SocketClientService; use App\Service\SocketRoomService; use App\Constants\SocketConstants; +use App\Cache\Repository\LockRedis; /** * 消息推送消费者队列 @@ -125,11 +125,9 @@ class ChatMessageConsumer extends ConsumerMessage public function consumeMessage($data, AMQPMessage $message): string { if (isset($data['event'])) { - $redis = container()->get(Redis::class); - // [加锁]防止消息重复消费 - $lockName = sprintf('ws:message-lock:%s:%s', SERVER_RUN_ID, $data['uuid']); - if (!$redis->set($lockName, 1, ['nx', 'ex' => 60])) { + $lockName = sprintf('ws-message:%s-%s', SERVER_RUN_ID, $data['uuid']); + if (LockRedis::getInstance()->lock($lockName, 60)) { return Result::ACK; } diff --git a/app/Cache/Repository/LockRedis.php b/app/Cache/Repository/LockRedis.php index 0233a0c..bc50c8f 100644 --- a/app/Cache/Repository/LockRedis.php +++ b/app/Cache/Repository/LockRedis.php @@ -69,6 +69,6 @@ class LockRedis implements LockRedisInterface end LAU; - return $this->redis()->eval($script, [$this->getCacheKey($key), $this->lockValue,], 1); + return $this->redis()->eval($script, [$this->getCacheKey($key), $this->lockValue], 1); } } diff --git a/app/Controller/Api/V1/ArticleController.php b/app/Controller/Api/V1/ArticleController.php index 37c43d9..daa8e90 100644 --- a/app/Controller/Api/V1/ArticleController.php +++ b/app/Controller/Api/V1/ArticleController.php @@ -17,9 +17,9 @@ use Hyperf\HttpServer\Annotation\Middleware; use App\Middleware\JWTAuthMiddleware; use App\Service\ArticleService; use App\Service\UploadService; -use App\Support\RedisLock; use Hyperf\Utils\Str; use Psr\Http\Message\ResponseInterface; +use App\Cache\Repository\LockRedis; /** * Class ArticleController @@ -177,14 +177,14 @@ class ArticleController extends CController 'sort_type' => 'required|in:1,2' ]); - $lockKey = "article_class_sort:{$params['class_id']}_{$params['sort_type']}"; + $lockKey = "article:sort_{$params['class_id']}_{$params['sort_type']}"; // 获取Redis锁 - if (RedisLock::lock($lockKey, 1, 3)) { + $lock = LockRedis::getInstance(); + if ($lock->lock($lockKey, 3, 500)) { $isTrue = $this->articleService->articleClassSort($this->uid(), (int)$params['class_id'], (int)$params['sort_type']); - // 释放Redis锁 - RedisLock::release($lockKey, 1); + $lock->delete($lockKey); } else { $isTrue = false; } diff --git a/app/Helper/StringHelper.php b/app/Helper/StringHelper.php index 87e9d4e..d1f1b2d 100644 --- a/app/Helper/StringHelper.php +++ b/app/Helper/StringHelper.php @@ -19,39 +19,5 @@ namespace App\Helper; */ class StringHelper { - /** - * 将字符串转换成二进制 - * - * @param string $str - * @return string - */ - public static function str2Bin(string $str): string - { - //列出每个字符 - $arr = preg_split('/(?rawCommand('SET', self::getLockKey($key), $requestId, 'NX', 'EX', $lockSecond); - if ($acquired) { - break; - } - - if ($timeout === 0) { - break; - } - - \Swoole\Coroutine\System::sleep($sleep); - } while (!is_numeric($timeout) || (self::getMicroTime()) < ($start + ($timeout * 1000000))); - - return (bool)$acquired; - } - - /** - * 释放锁 - * - * @param string $key 被加锁的KEY - * @param string|int $requestId 客户端请求唯一ID - * @return bool - */ - public static function release(string $key, $requestId) - { - if (strlen($key) === 0) { - return false; - } - - $lua = <<rawCommand('eval', $lua, 1, self::getLockKey($key), $requestId); - } - - /** - * 获取锁 Key - * - * @param string $key 需要加锁的KEY - * @return string - */ - public static function getLockKey(string $key) - { - return self::PREFIX . ':' . $key; - } - - /** - * 获取当前微秒 - * - * @return string - */ - protected static function getMicroTime() - { - return bcmul(microtime(true), 1000000); - } -}