From 43556c00131112f58d7f04174c1b186f6eb46515 Mon Sep 17 00:00:00 2001 From: tangxuliang <1297441127@qq.com> Date: Thu, 24 Mar 2022 14:05:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84facede?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Facades/Redis.php | 13 +++++++++++++ src/Redis.php | 38 ++++++++++++++++++++++++++++++++++++++ tests/RedisTest.php | 5 +++-- 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/Facades/Redis.php diff --git a/src/Facades/Redis.php b/src/Facades/Redis.php new file mode 100644 index 0000000..9df6c43 --- /dev/null +++ b/src/Facades/Redis.php @@ -0,0 +1,13 @@ +type = $type; return $this; } + /** + * 设置zset的score + * + * @param int $score + * @return $this + */ public function score(int $score): static { $this->score = $score; return $this; } + /** + * 设置过期时间 + * + * @param int $expire + * @return $this + */ public function expire(int $expire): static { $this->expire = $expire; return $this; } + /** + * Get an item from the cache, or execute the given Closure and store the result. + * + * @param string $key + * @param callable $callback + * @return mixed + */ public function remember(string $key, callable $callback) { $map = [ @@ -87,5 +112,18 @@ public function remember(string $key, callable $callback) return $getValue; } + + /** + * 调用predis方法 + * + * @param string $name + * @param array $arguments + * @return void + */ + public function __call(string $name, array $arguments) + { + $this->client->$name(...$arguments); + } + } diff --git a/tests/RedisTest.php b/tests/RedisTest.php index f2fd105..1385071 100644 --- a/tests/RedisTest.php +++ b/tests/RedisTest.php @@ -4,8 +4,9 @@ ->score(101) ->expire(3600) ->remember('test_name', function () { - return 'test'; + return 'test101'; }); - expect($response)->toEqual('test'); + expect($response)->toEqual('test101'); + }); \ No newline at end of file From f1e700745361e04c694f98078c1969951de8d65a Mon Sep 17 00:00:00 2001 From: Teakowa Gatanothor O'deorain Date: Thu, 24 Mar 2022 14:09:30 +0800 Subject: [PATCH 2/2] style: fix typo [skip ci] Signed-off-by: Teakowa Gatanothor O'deorain --- config/redis.php | 4 ++- src/Facades/Redis.php | 2 ++ src/Redis.php | 66 +++++++++++++++++++---------------------- src/ServiceProvider.php | 5 +++- src/helpers.php | 5 +++- tests/Pest.php | 2 ++ tests/RedisTest.php | 7 +++-- tests/TestCase.php | 5 +++- 8 files changed, 54 insertions(+), 42 deletions(-) diff --git a/config/redis.php b/config/redis.php index 3e5d32b..729cb29 100644 --- a/config/redis.php +++ b/config/redis.php @@ -1,8 +1,10 @@ getenv('REDIS_HOST'), 'password' => getenv('REDIS_PASSWORD'), 'port' => env('REDIS_PORT', '6379'), 'database' => env('REDIS_DB', 0), -]; \ No newline at end of file +]; diff --git a/src/Facades/Redis.php b/src/Facades/Redis.php index 9df6c43..e626ac8 100644 --- a/src/Facades/Redis.php +++ b/src/Facades/Redis.php @@ -1,5 +1,7 @@ expire = -1; $this->client = new Client([ 'scheme' => 'tcp', - 'host' => $config['host'] ?? '127.0.0.1', - 'port' => $config['port'] ?? 6379, + 'host' => $config['host'] ?? '127.0.0.1', + 'port' => $config['port'] ?? 6379, ]); } /** - * 设置操作的数据类型 + * 调用predis方法. + * + * @param array $arguments + */ + public function __call(string $name, array $arguments): void + { + $this->client->$name(...$arguments); + } + + /** + * 设置操作的数据类型. * - * @param string $type * @return $this */ public function type(string $type): static { $this->type = $type; + return $this; } /** - * 设置zset的score + * 设置zset的score. * - * @param int $score * @return $this */ public function score(int $score): static { $this->score = $score; + return $this; } /** - * 设置过期时间 + * 设置过期时间. * - * @param int $expire * @return $this */ public function expire(int $expire): static { $this->expire = $expire; + return $this; } /** * Get an item from the cache, or execute the given Closure and store the result. - * - * @param string $key - * @param callable $callback - * @return mixed */ - public function remember(string $key, callable $callback) + public function remember(string $key, callable $callback): mixed { $map = [ 'string' => [ 'get' => function () use ($key) { return $this->client->get($key); }, - 'put' => function ($value) use ($key) { + 'put' => function ($value) use ($key): void { $this->client->set($key, $value); - } + }, ], 'zset' => [ 'get' => function () use ($key, $callback) { $zset = $this->client->zrangebyscore($key, $this->score, $this->score); - if (is_array($zset) && count($zset) == 1) { + if (is_array($zset) && count($zset) === 1) { return json_decode(current($zset), true); } // 没有匹配写入cache $return = $callback(); $this->client->zadd($key, $this->score, json_encode($return)); + return $return; }, - 'put' => function ($value) use ($key) { + 'put' => function ($value) use ($key): void { $this->client->zadd($key, $this->score, json_encode($value)); - } - ] + }, + ], ]; - $getType = (string)$this->client->type($key); + $getType = (string) $this->client->type($key); if (array_key_exists($getType, $map)) { return $map[$getType]['get'](); } @@ -105,25 +115,11 @@ public function remember(string $key, callable $callback) $setType = $this->type ?? ''; if (array_key_exists($setType, $map)) { $map[$setType]['put']($getValue); - if($this->expire !== -1) { + if ($this->expire !== -1) { $this->client->expire($key, $this->expire); } } return $getValue; } - - /** - * 调用predis方法 - * - * @param string $name - * @param array $arguments - * @return void - */ - public function __call(string $name, array $arguments) - { - $this->client->$name(...$arguments); - } - } - diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index dbc5508..16c59d9 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -1,4 +1,7 @@ config_path('redis.php'), ], 'config'); } -} \ No newline at end of file +} diff --git a/src/helpers.php b/src/helpers.php index 5d9488b..c2779aa 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,9 +1,12 @@ type('zset') ->score(101) ->expire(3600) @@ -8,5 +10,4 @@ }); expect($response)->toEqual('test101'); - -}); \ No newline at end of file +}); diff --git a/tests/TestCase.php b/tests/TestCase.php index b5a9a0c..1c67965 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,4 +1,7 @@ bootstrapWith([LoadEnvironmentVariables::class]); parent::getEnvironmentSetUp($app); } -} \ No newline at end of file +}