|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the bugloos/fault-tolerance-bundle project. |
|
5
|
|
|
* (c) Bugloos <https://bugloos.com/> |
|
6
|
|
|
* For the full copyright and license information, please view |
|
7
|
|
|
* the LICENSE file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Bugloos\FaultToleranceBundle\RequestCache\Storage; |
|
11
|
|
|
|
|
12
|
|
|
use Psr\Cache\InvalidArgumentException; |
|
13
|
|
|
use Symfony\Component\Cache\Adapter\RedisAdapter; |
|
14
|
|
|
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Mojtaba Gheytasi <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class RedisStorage implements StorageInterface |
|
20
|
|
|
{ |
|
21
|
|
|
private RedisTagAwareAdapter $adapter; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(string $redisUrl) |
|
24
|
|
|
{ |
|
25
|
|
|
$client = RedisAdapter::createConnection($redisUrl); |
|
26
|
|
|
$this->adapter = new RedisTagAwareAdapter($client); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @throws InvalidArgumentException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function get(string $bucket, string $key) |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->adapter->getItem($bucket . '.' . $key)->get(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @throws InvalidArgumentException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function set(string $bucket, string $key, $value, int $expiresAfterSeconds): void |
|
41
|
|
|
{ |
|
42
|
|
|
$item = $this->adapter->getItem($bucket . '.' . $key); |
|
43
|
|
|
$item->set($value); |
|
44
|
|
|
$item->expiresAfter($expiresAfterSeconds); |
|
45
|
|
|
$item->tag($bucket); |
|
46
|
|
|
$this->adapter->save($item); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @throws InvalidArgumentException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function exists(string $bucket, string $key): bool |
|
53
|
|
|
{ |
|
54
|
|
|
return (bool) $this->adapter->getItem($bucket . '.' . $key)->get(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @throws InvalidArgumentException |
|
59
|
|
|
*/ |
|
60
|
|
|
public function remove(string $bucket, string $key): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->adapter->deleteItem($bucket . '.' . $key); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @throws InvalidArgumentException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function removeBucket(string $bucket): void |
|
69
|
|
|
{ |
|
70
|
|
|
$this->adapter->invalidateTags([$bucket]); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|