Skip to content

Commit f4fa8ec

Browse files
authored
feat: 完善hash操作 (#7)
* feat: 完善hash操作 * test: hash测试
1 parent ead6776 commit f4fa8ec

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/Redis.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ class Redis
1111
public Client $client;
1212
private string $type;
1313
private int $score;
14+
private string $hashKey;
1415
private int $expire;
1516

1617
public function __construct(array $config = [])
1718
{
1819
$this->type = 'string';
1920
$this->score = 0;
21+
$this->hashKey = '';
2022
$this->expire = -1;
2123

2224
$options = [
@@ -67,6 +69,18 @@ public function score(int $score): static
6769
return $this;
6870
}
6971

72+
/**
73+
* 设置hash的key
74+
*
75+
* @return $this
76+
*/
77+
public function hashKey(string $key): static
78+
{
79+
$this->hashKey = $key;
80+
81+
return $this;
82+
}
83+
7084
/**
7185
* 设置过期时间.
7286
*
@@ -97,19 +111,34 @@ public function remember(string $key, callable $callback): mixed
97111
'get' => function () use ($key, $callback) {
98112
$zset = $this->client->zrangebyscore($key, $this->score, $this->score);
99113
if (is_array($zset) && count($zset) === 1) {
100-
return json_decode(current($zset), true);
114+
return current($zset);
101115
}
102116

103117
// 没有匹配写入cache
104118
$return = $callback();
105-
$this->client->zadd($key, $this->score, json_encode($return));
106-
119+
$this->client->zadd($key, $this->score, $return);
107120
return $return;
108121
},
109122
'put' => function ($value) use ($key): void {
110-
$this->client->zadd($key, $this->score, json_encode($value));
123+
$this->client->zadd($key, $this->score, $value);
111124
},
112125
],
126+
'hash' => [
127+
'get' => function () use ($callback, $key) {
128+
$hash = $this->client->hget($key, $this->hashKey);
129+
if ($hash) {
130+
return $hash;
131+
}
132+
133+
// 没有匹配写入cache
134+
$return = $callback();
135+
$this->client->hset($key, $this->hashKey, $return);
136+
return $return;
137+
},
138+
'put' => function ($value) use ($key) {
139+
$this->client->hset($key, $this->hashKey, $value);
140+
}
141+
]
113142
];
114143

115144
$getType = (string) $this->client->type($key);

tests/RedisTest.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
<?php
22

33
declare(strict_types=1);
4-
it('remember', function () {
4+
it('rememberZset', function () {
55
$response = redis()->type('zset')
66
->score(101)
77
->expire(3600)
8-
->remember('test_name', function () {
9-
return 'test101';
8+
->remember('test_zset', function () {
9+
return json_encode([
10+
'name' => 'test101'
11+
]);
1012
});
1113

12-
expect($response)->toEqual('test101');
14+
expect($response)->toEqual('{"name":"test101"}');
1315
});
16+
17+
it('rememberHash', function () {
18+
$response = redis()->type('hash')
19+
->hashKey('business_uuid')
20+
->expire(3600)
21+
->remember('test_hash', function () {
22+
return 'jqw4iej6uj48dw8';
23+
});
24+
25+
expect($response)->toEqual('jqw4iej6uj48dw8');
26+
});

0 commit comments

Comments
 (0)