From 00fdc2951002debd1755acdcd23ffbfdc7a473dd Mon Sep 17 00:00:00 2001 From: tangxuliang <1297441127@qq.com> Date: Mon, 28 Mar 2022 11:43:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84hash=E6=93=8D?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Redis.php | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/Redis.php b/src/Redis.php index 80ae5c1..e09a1df 100644 --- a/src/Redis.php +++ b/src/Redis.php @@ -11,12 +11,14 @@ class Redis public Client $client; private string $type; private int $score; + private string $hashKey; private int $expire; public function __construct(array $config = []) { $this->type = 'string'; $this->score = 0; + $this->hashKey = ''; $this->expire = -1; $options = [ @@ -67,6 +69,18 @@ public function score(int $score): static return $this; } + /** + * 设置hash的key + * + * @return $this + */ + public function hashKey(string $key): static + { + $this->hashKey = $key; + + return $this; + } + /** * 设置过期时间. * @@ -97,19 +111,34 @@ public function remember(string $key, callable $callback): mixed 'get' => function () use ($key, $callback) { $zset = $this->client->zrangebyscore($key, $this->score, $this->score); if (is_array($zset) && count($zset) === 1) { - return json_decode(current($zset), true); + return current($zset); } // 没有匹配写入cache $return = $callback(); - $this->client->zadd($key, $this->score, json_encode($return)); - + $this->client->zadd($key, $this->score, $return); return $return; }, 'put' => function ($value) use ($key): void { - $this->client->zadd($key, $this->score, json_encode($value)); + $this->client->zadd($key, $this->score, $value); }, ], + 'hash' => [ + 'get' => function () use ($callback, $key) { + $hash = $this->client->hget($key, $this->hashKey); + if ($hash) { + return $hash; + } + + // 没有匹配写入cache + $return = $callback(); + $this->client->hset($key, $this->hashKey, $return); + return $return; + }, + 'put' => function ($value) use ($key) { + $this->client->hset($key, $this->hashKey, $value); + } + ] ]; $getType = (string) $this->client->type($key); From f3d1f93cec88d7163768f48d29512beb28f47147 Mon Sep 17 00:00:00 2001 From: tangxuliang <1297441127@qq.com> Date: Mon, 28 Mar 2022 11:43:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test:=20hash=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/RedisTest.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/RedisTest.php b/tests/RedisTest.php index f115140..973ea92 100644 --- a/tests/RedisTest.php +++ b/tests/RedisTest.php @@ -1,13 +1,26 @@ type('zset') ->score(101) ->expire(3600) - ->remember('test_name', function () { - return 'test101'; + ->remember('test_zset', function () { + return json_encode([ + 'name' => 'test101' + ]); }); - expect($response)->toEqual('test101'); + expect($response)->toEqual('{"name":"test101"}'); }); + +it('rememberHash', function () { + $response = redis()->type('hash') + ->hashKey('business_uuid') + ->expire(3600) + ->remember('test_hash', function () { + return 'jqw4iej6uj48dw8'; + }); + + expect($response)->toEqual('jqw4iej6uj48dw8'); +}); \ No newline at end of file