Skip to content

Commit bf3a87e

Browse files
author
qbhy
committed
feat:优化性能
1 parent 51c08be commit bf3a87e

File tree

6 files changed

+60
-17
lines changed

6 files changed

+60
-17
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ composer.lock
44
*.log
55
/hooks
66
.idea
7-
/runtime
7+
runtime
88
config

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@
4646
"hyperf/session": "^2.0",
4747
"hyperf/testing": "^2.0",
4848
"hyperf/utils": "^2.2",
49+
"itsgoingd/clockwork": "^5.0",
4950
"phpstan/phpstan": "^0.12",
50-
"swoft/swoole-ide-helper": "dev-master"
51+
"swoft/swoole-ide-helper": "dev-master",
52+
"symfony/var-dumper": "^5.3"
5153
},
5254
"config": {
5355
"sort-packages": true

src/Guard/JwtGuard.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function parseToken()
6666

6767
public function login(Authenticatable $user)
6868
{
69-
$token = $this->jwtManager->make([
69+
$token = $this->getJwtManager()->make([
7070
'uid' => $user->getId(),
7171
's' => str_random(),
7272
])->token();
@@ -100,7 +100,7 @@ public function user(?string $token = null): ?Authenticatable
100100

101101
try {
102102
if ($token) {
103-
$jwt = $this->jwtManager->parse($token);
103+
$jwt = $this->getJwtManager()->parse($token);
104104
$uid = $jwt->getPayload()['uid'] ?? null;
105105
$user = $uid ? $this->userProvider->retrieveByCredentials($uid) : null;
106106
Context::set($key, $user ?: 0);
@@ -150,14 +150,14 @@ public function refresh(?string $token = null): ?string
150150
Context::set($this->resultKey($token), null);
151151

152152
try {
153-
$jwt = $this->jwtManager->parse($token);
153+
$jwt = $this->getJwtManager()->parse($token);
154154
} catch (TokenExpiredException $exception) {
155155
$jwt = $exception->getJwt();
156156
}
157157

158-
$this->jwtManager->addBlacklist($jwt);
158+
$this->getJwtManager()->addBlacklist($jwt);
159159

160-
return $this->jwtManager->refresh($jwt)->token();
160+
return $this->getJwtManager()->refresh($jwt)->token();
161161
}
162162

163163
return null;
@@ -167,8 +167,8 @@ public function logout($token = null)
167167
{
168168
if ($token = $token ?? $this->parseToken()) {
169169
Context::set($this->resultKey($token), null);
170-
$this->jwtManager->addBlacklist(
171-
$this->jwtManager->parse($token)
170+
$this->getJwtManager()->addBlacklist(
171+
$this->getJwtManager()->parse($token)
172172
);
173173
return true;
174174
}
@@ -182,14 +182,15 @@ public function getJwtManager(): JWTManager
182182

183183
/**
184184
* 获取 token 标识.
185+
* 为了性能,直接 md5.
185186
*
187+
* @throws \Qbhy\SimpleJwt\Exceptions\TokenExpiredException
186188
* @throws \Qbhy\SimpleJwt\Exceptions\InvalidTokenException
187189
* @throws \Qbhy\SimpleJwt\Exceptions\SignatureException
188-
* @throws \Qbhy\SimpleJwt\Exceptions\TokenExpiredException
189190
* @return mixed|string
190191
*/
191192
protected function getJti(string $token): string
192193
{
193-
return $this->getJwtManager()->justParse($token)->getPayload()['jti'] ?? md5($token);
194+
return md5($token);
194195
}
195196
}

src/helper.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
if (! function_exists('auth')) {
1616
/**
1717
* 建议视图中使用该函数,其他地方请使用注入.
18-
* @throws \Qbhy\HyperfAuth\Exception\GuardException
1918
* @throws \Qbhy\HyperfAuth\Exception\UserProviderException
19+
* @throws \Qbhy\HyperfAuth\Exception\GuardException
2020
* @return AuthManager|mixed|\Qbhy\HyperfAuth\AuthGuard
2121
*/
2222
function auth(?string $guard = null)
@@ -37,9 +37,20 @@ function str_random($num = 6): string
3737
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
3838
$randomString = '';
3939
for ($i = 0; $i < $num; ++$i) {
40-
$index = rand(0, strlen($characters) - 1);
40+
$index = rand(0, 61);
4141
$randomString .= $characters[$index];
4242
}
4343
return $randomString;
4444
}
4545
}
46+
47+
if (! function_exists('dev_clock')) {
48+
function dev_clock(string $title, callable $handler)
49+
{
50+
$start = microtime(true);
51+
$result = $handler();
52+
$end = microtime(true);
53+
dump($title . ' 用时:' . (($end - $start) * 1000) . 'ms');
54+
return $result;
55+
}
56+
}

tests/Cases/ExampleTest.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,36 @@ public function testAuthFunc()
4141
$this->assertTrue(auth() instanceof AuthManager);
4242
}
4343

44+
/**
45+
* 大概用时:0.028848648071289 ms.
46+
*/
47+
public function testRandomStr()
48+
{
49+
dev_clock('随机字符串', function () {
50+
str_random();
51+
});
52+
$this->assertTrue(true);
53+
}
54+
4455
public function testJwtGuard()
4556
{
4657
/** @var AuthManager|JwtGuard $auth */
4758
$auth = $this->auth();
4859
/** @var JwtGuard $guard */
4960
$guard = $auth->guard();
50-
$token = $auth->login($this->user());
51-
$this->assertTrue($auth->check($token));
52-
$this->assertTrue(! $auth->guest($token));
61+
$user = $this->user();
62+
63+
$token = dev_clock('jwt login 方法', function () use ($auth, $user) {
64+
return $auth->login($user);
65+
});
66+
67+
$this->assertTrue(dev_clock('jwt check 方法', function () use ($auth, $token) {
68+
return $auth->check($token);
69+
}));
70+
71+
$this->assertTrue(dev_clock('jwt guest 方法', function () use ($auth, $token) {
72+
return ! $auth->guest($token);
73+
}));
5374

5475
// 测试默认 guard
5576
$this->assertTrue($guard instanceof AuthGuard);
@@ -59,7 +80,9 @@ public function testJwtGuard()
5980
$jwtManager = $guard->getJwtManager();
6081
$token = $guard->login($this->user());
6182

62-
$this->assertTrue(is_string($newToken = $guard->refresh($token))); // 测试刷新 token
83+
$this->assertTrue(is_string($newToken = dev_clock('jwt refresh 方法', function () use ($guard, $token) {
84+
return $guard->refresh($token);
85+
}))); // 测试刷新 token
6386

6487
try {
6588
$this->assertTrue($guard->guest($token)); // 试试新 token 是否生效
@@ -96,6 +119,7 @@ public function testSsoGuard()
96119
$guard = $this->auth()->guard('sso');
97120
$this->assertTrue($guard instanceof SsoGuard);
98121
$this->assertTrue($guard->getProvider() instanceof EloquentProvider);
122+
99123
$user = $this->user(10);
100124
$token = $guard->login($user, 'pc');
101125
$this->assertTrue(is_string($token));

tests/bootstrap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
'secret' => 'test.secret',
4949
'provider' => 'test-provider', // 不设置的话用上面的 default.provider 或者用 'default'
5050
'encoder' => null,
51+
/*
52+
* 可选配置
53+
* 默认使用的加密类
54+
*/
55+
'default' => \Qbhy\SimpleJwt\EncryptAdapters\SHA1Encrypter::class,
5156
'cache' => new FilesystemCache(sys_get_temp_dir()), // 如果需要分布式部署,请选择 redis 或者其他支持分布式的缓存驱动
5257
];
5358

0 commit comments

Comments
 (0)