Skip to content

Commit 09c8ab0

Browse files
xuliangTangTeakowa
andauthored
feat: 完善facede (#2)
* feat: 完善facede * style: fix typo [skip ci] Signed-off-by: Teakowa Gatanothor O'deorain <hub+git@teakowa.dev> Co-authored-by: Teakowa Gatanothor O'deorain <hub+git@teakowa.dev>
1 parent feae9ee commit 09c8ab0

File tree

8 files changed

+84
-20
lines changed

8 files changed

+84
-20
lines changed

config/redis.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
return [
46
'host' => getenv('REDIS_HOST'),
57
'password' => getenv('REDIS_PASSWORD'),
68
'port' => env('REDIS_PORT', '6379'),
79
'database' => env('REDIS_DB', 0),
8-
];
10+
];

src/Facades/Redis.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace XNXK\LaravelRedisHelper\Facades;
6+
7+
use Illuminate\Support\Facades\Facade;
8+
9+
class Redis extends Facade
10+
{
11+
public static function getFacadeAccessor(): string
12+
{
13+
return 'laravel-redis-helper';
14+
}
15+
}

src/Redis.php

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
namespace XNXK\LaravelRedisHelper;
36

47
use Predis\Client;
@@ -17,59 +20,91 @@ public function __construct(array $config = [])
1720
$this->expire = -1;
1821
$this->client = new Client([
1922
'scheme' => 'tcp',
20-
'host' => $config['host'] ?? '127.0.0.1',
21-
'port' => $config['port'] ?? 6379,
23+
'host' => $config['host'] ?? '127.0.0.1',
24+
'port' => $config['port'] ?? 6379,
2225
]);
2326
}
2427

28+
/**
29+
* 调用predis方法.
30+
*
31+
* @param array $arguments
32+
*/
33+
public function __call(string $name, array $arguments): void
34+
{
35+
$this->client->$name(...$arguments);
36+
}
37+
38+
/**
39+
* 设置操作的数据类型.
40+
*
41+
* @return $this
42+
*/
2543
public function type(string $type): static
2644
{
2745
$this->type = $type;
46+
2847
return $this;
2948
}
3049

50+
/**
51+
* 设置zset的score.
52+
*
53+
* @return $this
54+
*/
3155
public function score(int $score): static
3256
{
3357
$this->score = $score;
58+
3459
return $this;
3560
}
3661

62+
/**
63+
* 设置过期时间.
64+
*
65+
* @return $this
66+
*/
3767
public function expire(int $expire): static
3868
{
3969
$this->expire = $expire;
70+
4071
return $this;
4172
}
4273

43-
public function remember(string $key, callable $callback)
74+
/**
75+
* Get an item from the cache, or execute the given Closure and store the result.
76+
*/
77+
public function remember(string $key, callable $callback): mixed
4478
{
4579
$map = [
4680
'string' => [
4781
'get' => function () use ($key) {
4882
return $this->client->get($key);
4983
},
50-
'put' => function ($value) use ($key) {
84+
'put' => function ($value) use ($key): void {
5185
$this->client->set($key, $value);
52-
}
86+
},
5387
],
5488
'zset' => [
5589
'get' => function () use ($key, $callback) {
5690
$zset = $this->client->zrangebyscore($key, $this->score, $this->score);
57-
if (is_array($zset) && count($zset) == 1) {
91+
if (is_array($zset) && count($zset) === 1) {
5892
return json_decode(current($zset), true);
5993
}
6094

6195
// 没有匹配写入cache
6296
$return = $callback();
6397
$this->client->zadd($key, $this->score, json_encode($return));
98+
6499
return $return;
65100
},
66-
'put' => function ($value) use ($key) {
101+
'put' => function ($value) use ($key): void {
67102
$this->client->zadd($key, $this->score, json_encode($value));
68-
}
69-
]
103+
},
104+
],
70105
];
71106

72-
$getType = (string)$this->client->type($key);
107+
$getType = (string) $this->client->type($key);
73108
if (array_key_exists($getType, $map)) {
74109
return $map[$getType]['get']();
75110
}
@@ -80,12 +115,11 @@ public function remember(string $key, callable $callback)
80115
$setType = $this->type ?? '';
81116
if (array_key_exists($setType, $map)) {
82117
$map[$setType]['put']($getValue);
83-
if($this->expire !== -1) {
118+
if ($this->expire !== -1) {
84119
$this->client->expire($key, $this->expire);
85120
}
86121
}
87122

88123
return $getValue;
89124
}
90125
}
91-

src/ServiceProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
namespace XNXK\LaravelRedisHelper;
36

47
class ServiceProvider extends \Illuminate\Support\ServiceProvider
@@ -25,4 +28,4 @@ public function boot(): void
2528
__DIR__ . '/../config/redis.php.php' => config_path('redis.php'),
2629
], 'config');
2730
}
28-
}
31+
}

src/helpers.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
use XNXK\LaravelRedisHelper\Redis;
46

57
if (!function_exists('redis')) {
6-
function redis() {
8+
function redis()
9+
{
710
return app(Redis::class);
811
}
912
}

tests/Pest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
|--------------------------------------------------------------------------
57
| Test Case

tests/RedisTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
2-
it('remember', function (){
2+
3+
declare(strict_types=1);
4+
it('remember', function () {
35
$response = redis()->type('zset')
46
->score(101)
57
->expire(3600)
68
->remember('test_name', function () {
7-
return 'test';
9+
return 'test101';
810
});
911

10-
expect($response)->toEqual('test');
11-
});
12+
expect($response)->toEqual('test101');
13+
});

tests/TestCase.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
namespace Tests;
36

47
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
@@ -23,4 +26,4 @@ protected function getEnvironmentSetUp($app)
2326
$app->bootstrapWith([LoadEnvironmentVariables::class]);
2427
parent::getEnvironmentSetUp($app);
2528
}
26-
}
29+
}

0 commit comments

Comments
 (0)