1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of Phpfastcache. |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files. |
10
|
|
|
* |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types=1); |
16
|
|
|
|
17
|
|
|
namespace Phpfastcache\Drivers\Redis; |
18
|
|
|
|
19
|
|
|
use Phpfastcache\Core\Item\ExtendedCacheItemInterface; |
20
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; |
21
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheLogicException; |
22
|
|
|
use Redis as RedisClient; |
23
|
|
|
use RedisCluster as RedisClusterClient; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @property RedisClient|RedisClusterClient $instance |
27
|
|
|
* @method Config getConfig() |
28
|
|
|
*/ |
29
|
|
|
trait RedisDriverTrait |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @param ExtendedCacheItemInterface $item |
33
|
|
|
* @return ?array<string, mixed> |
34
|
|
|
*/ |
35
|
|
|
protected function driverRead(ExtendedCacheItemInterface $item): ?array |
36
|
|
|
{ |
37
|
|
|
$val = $this->instance->get($item->getKey()); |
38
|
|
|
if (!$val) { |
39
|
|
|
return null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $this->decode($val); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param ExtendedCacheItemInterface ...$items |
47
|
|
|
* @return array<array<string, mixed>> |
48
|
|
|
* @throws \Phpfastcache\Exceptions\PhpfastcacheDriverException |
49
|
|
|
* @throws \RedisException |
50
|
|
|
*/ |
51
|
|
|
protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
52
|
|
|
{ |
53
|
|
|
$keys = $this->getKeys($items); |
|
|
|
|
54
|
|
|
|
55
|
|
|
return array_combine($keys, array_map( |
56
|
|
|
fn($val) => $val ? $this->decode($val) : null, |
57
|
|
|
$this->instance->mGet($keys) |
58
|
|
|
)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param ExtendedCacheItemInterface $item |
63
|
|
|
* @return mixed |
64
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
65
|
|
|
* @throws PhpfastcacheLogicException |
66
|
|
|
*/ |
67
|
|
|
protected function driverWrite(ExtendedCacheItemInterface $item): bool |
68
|
|
|
{ |
69
|
|
|
$this->assertCacheItemType($item, self::getItemClass()); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$ttl = $item->getExpirationDate()->getTimestamp() - time(); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @see https://redis.io/commands/setex |
75
|
|
|
* @see https://redis.io/commands/expire |
76
|
|
|
*/ |
77
|
|
|
if ($ttl <= 0) { |
78
|
|
|
return $this->instance->expire($item->getKey(), 0); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item))); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $key |
86
|
|
|
* @param string $encodedKey |
87
|
|
|
* @return bool |
88
|
|
|
* @throws \RedisException |
89
|
|
|
*/ |
90
|
|
|
protected function driverDelete(string $key, string $encodedKey): bool |
91
|
|
|
{ |
92
|
|
|
return (bool) $this->instance->del($key); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string[] $keys |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
protected function driverDeleteMultiple(array $keys): bool |
100
|
|
|
{ |
101
|
|
|
return (bool) $this->instance->del(...$keys); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|