?? ?? ??? ?? Hyperf ?????? ???? ??
Oct 28, 2023 am 09:37 AM?? ?? ??? ?? Hyperf ?????? ???? ??
??:
?? ?????? ??? ??? ???? ?? ??? ?? ?? ??? ??? ?? ???? ????? ???. ??? ???, ??? ?? ? ?? ??. ? ??? ????? ???? ???? ???? ?? ?? ?? ????? ???? ?? ??? ????. Hyperf ?????? ?? ??? ???? ??? ??? ?????.
1. Hyperf ????? ??
Hyperf? PHP ???? ???? ?? ??? ??? ??????, ??? ?? ??????? ??? ???? ? ?????. ?? ???, ??? ??? ??, ??? IoC ????, ??? ? ??? ?? ?? ??? ??? ??? ????.
2. ?? ?? ??
?? ???? ????? ?????? ??? ?? ??? ? ?? ?? ??? ????. ?????? ?? ?? ?? ??? ??? ????? ??? ????. ?? ?? ?? ??? ????? ?? ??? ???? ?? Redis ?? Memcached? ?? ??? ?? ???? ???? ?????.
3. Hyperf ?????? Redis? ?????
- Redis ?? ??
PHP ???? Redis ??? ????? Redid ?? ??? ?? ???? ???.
pecl install redis
- Redis ?? ??
Hyperf ???? config/autoload/redis.php
? ?? ??? Redis ?? ???? ??: config/autoload/redis.php
中添加Redis的連接參數(shù):
<?php declare(strict_types=1); return [ 'default' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'auth' => env('REDIS_AUTH', null), 'port' => (int) env('REDIS_PORT', 6379), 'db' => (int) env('REDIS_DB', 0), 'pool' => [ 'max_connections' => (int) env('REDIS_MAX_CONNECTIONS', 10), 'min_connections' => (int) env('REDIS_MIN_CONNECTIONS', 1), 'connect_timeout' => (float) env('REDIS_CONNECT_TIMEOUT', 1.0), 'wait_timeout' => (float) env('REDIS_WAIT_TIMEOUT', 3.0), 'heartbeat' => (int) env('REDIS_HEARTBEAT', -1), 'max_idle_time' => (float) env('REDIS_MAX_IDLE_TIME', 60), ], ], ];
- 配置Redis連接信息
在根目錄下的.env
文件中添加以下Redis連接信息,注意根據(jù)實(shí)際情況修改參數(shù):
REDIS_HOST=127.0.0.1 REDIS_PORT=6379 REDIS_DB=0
四、使用Hyperf框架進(jìn)行分布式鎖
- 創(chuàng)建鎖服務(wù)類
在Hyperf的app/Utils
目錄下創(chuàng)建LockService.php
<?php declare(strict_types=1); namespace AppUtils; use HyperfRedisRedisFactory; use HyperfUtilsApplicationContext; use RedisException; class LockService { /** * 獲取鎖 * @param string $key 鎖的key * @param int $expire 過期時(shí)間,單位為秒 * @return bool */ public function lock(string $key, int $expire): bool { $redis = $this->getRedis(); try { return $redis->set($key, 1, ['nx', 'ex' => $expire]) ? true : false; } catch (RedisException $exception) { return false; } } /** * 解鎖 * @param string $key 鎖的key * @return bool */ public function unlock(string $key): bool { $redis = $this->getRedis(); try { return $redis->del([$key]) > 0; } catch (RedisException $exception) { return false; } } /** * 獲取Redis實(shí)例 * @return mixed */ private function getRedis() { $container = ApplicationContext::getContainer(); return $container->get(RedisFactory::class)->get('default'); } }
- Redis ?? ?? ??
- ?? ?????
.env
??? ?? Redis ?? ??? ?????. ?? ??? ?? ????? ????? ?????. <?php declare(strict_types=1); namespace AppController; use AppUtilsLockService; use HyperfHttpServerAnnotationAutoController; /** * @AutoController() */ class DemoController { public function index(LockService $lockService) { // 獲取鎖 $lockKey = 'demo_lock'; $expire = 10; // 過期時(shí)間10秒 if ($lockService->lock($lockKey, $expire)) { // 獲得鎖,執(zhí)行業(yè)務(wù)邏輯 // TODO: 處理業(yè)務(wù)邏輯 // 釋放鎖 $lockService->unlock($lockKey); } else { // 未獲得鎖,返回重試或失敗的響應(yīng) } } }
4. Hyperf ?????? ???? ?? ?? ??
?? ??? ???
app/Utils
????? LockService.php
??? ???? ?? ?? ?? ???? ??????. ??rrreee ?????? ??? ??? ?? ?????? ?? ??? ???? ?? ?? ??? ??? ?? ?? ??? ???? ???? ?? ?????. ?? ???? ?? ??? ???? ??? ?? ??? ???? ??? ?????. ??rrreee?? 5. ?? ??Redis? ???? Hyperf ?????? ?? ?? ??? ???? ??????? ???? ???? ??? ?? ??? ???? ?? ????? ?? ???? ???? ??? ???? ???? ??? ? ????. ??? ???? ?? ?? ??? ?? ?? ???? ?????. ?? ??? ?? ?? ?????? ?? ?????. ? ??? ??? ??? ?? ??? ? ? ???? ???? ? ??? ??? ????. ??? ??? ?? ?? ??? ?? Hyperf ?????? ???? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

Redis? ???? ?? ???? ??? ???? ?? ??: ???? ??? ???? ?? ?? ???? ??? ?? ? ?? ???? ????. ?? ????? ???? ??? ??? ?????. ??? ???? ?? ??? ?? ????? ???? ??? ???????. Redis? ??? ???? ?? ???? ??? ?? ???? ???? ??? ???? ???? ? ????. 1. Redis ?? Redis? ???? ?? ? ?? ??? ??? ???? ?? ??? ?? ??? ?? ??????.

?? ??? ?? Hyperf ?????? ????? ???? ?? ??? ?????. Hyperf? Swoole ??? ???? ??? ??? PHP ??????, ???, ??? ??, AOP, ????, ??? ?? ?? ??? ??? ??? ????. ???, ???? ?? ??? ? ?????? ? ???????? ???? ? ?????. ?? ??????? ??? ???? ???? ?? ??? ????. Hyperf ?????? ?? ?? ??? ????? ? ??? ?? ? ?? ??? ?? ??? ??? ?????. ? ????? ?? ??? ?????.

?? ??? ?? Hyperf ?????? ????? ?? ?? ??? ?????. ??: ????? ?? ?????? ??? ??? ??? ???? ???? ???? ???. ??? PHP ?? ?????? Hyperf ?????? ???? ?? ??? ???? ? ??? ?? ??? ??? ??? ?????. ? ????? ?? ??? ?? Hyperf ?????? ???? ??? ???? ?? ?? ??? ?? ?? ?????. 1. ?? ?? ?? ?? Hyperf ?????? ? ?? ???? ??? ?????.

Java? ?? ?? ?? ??? ???? ??? ?????? ?? ?? ????? ??? ?? ??? ?? ??? ?????. ?? ??? ?????? ??? ??? ?? ?? ??? ? ? ??????, ??? ??? ??? ????, ? ??? ???? ?? ???? ?? ??? ?? ? ????. ? ????? Java ????? ??? ???? ?? ?? ?? ??? ???? ??? ?? ?????. ??, ?? ??? ??? ??? ???? ???. ????? ??? ??? ??? ?? ??? ? ????.

?? ??? ?? Hyperf ?????? ???? ?? ??: Hyrpf? ??? ?? ??? ??? ?? PHP ?? ??? ???, ?? ??? ??? ????????. ?? ??? ?? ????? ???? ?????. ? ????? ?? ??? ?? Hyperf ?????? ???? ??? ???? ?? ?? ??? ?????. 1. Hyperf ????? ?? ?? Hyperf ?????? ???? ???. Composer? ?? ??? ? ???, ??? ??? ?? ?? ??? ?????.

JWT ??? ?? Hyperf ?????? ???? ?? ??: Hyperf? Swoole ??? ??? ??? ?????? ??? ??? ??? ???? ?????. JWT(JSONWebToken)? ??? ???? ???? ?? ??? ?????. ? ????? Hyperf ??????? JWT ??? ???? ??? ???? ???? ?? ??? ?????. 1. ??? ??? ?? ?? hyperf/jwt ? lcobucci/jw? ???? ???.

?? ???? ?? Hyperf ?????? ???? ?? ??: ???? ????? ?? ???? ?? ????? ??????? ?? ??? ?????. ?3? ???? ?? ???? ?3? ???? ?? ?? ??? ???? ?? ????? ??????? ???? ? ???? ???? ?? ??? ??? ??? ??? ?? ???? ? ????. ? ????? ?? ?? ??? ?? Hyperf ?????? ???? ?? ??? ??? ???? ??? ?????. 1. ???? ?3? ??? ??? ???? ??,

?? ??? ?? Hyperf ????? ?? ?? 1. ?? Hyperf ?????? Swoole2.0+? ???? ?? ??? ??????? ????????. Hyperf ?????? ???? ?? ?? ???? ???? ?? ?? ?? ??? ??? ???? ?? ???? ????? ? ??? ???. ? ????? ????, ?? ? ??? ??? ??? ???? Hyperf ?????? ?? ?? ??? ???? ??? ?????. 2. ?? ? ?? Hyperf ?????? ????? ?? Composer? ?? Hyp? ???? ???.
