1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target\Local\DrivingFile\Storage; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\UploadPerPartes\Interfaces; |
7
|
|
|
use kalanis\UploadPerPartes\Target\Local\DrivingFile; |
8
|
|
|
use kalanis\UploadPerPartes\Traits\TLang; |
9
|
|
|
use kalanis\UploadPerPartes\UploadException; |
10
|
|
|
use Predis as lib; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Predis |
15
|
|
|
* @package kalanis\UploadPerPartes\Target\Local\DrivingFile\Storage |
16
|
|
|
* Processing info file in Redis |
17
|
|
|
* @codeCoverageIgnore |
18
|
|
|
*/ |
19
|
|
|
class Predis implements Interfaces\IDrivingFile |
20
|
|
|
{ |
21
|
|
|
use TLang; |
22
|
|
|
|
23
|
|
|
protected lib\Client $redis; |
24
|
|
|
protected int $timeout = 0; |
25
|
|
|
|
26
|
|
|
public function __construct(lib\Client $redis, int $timeout = 3600, ?Interfaces\IUppTranslations $lang = null) |
27
|
|
|
{ |
28
|
|
|
// path is not a route but redis key |
29
|
|
|
$this->setUppLang($lang); |
30
|
|
|
$this->redis = $redis; |
31
|
|
|
$this->timeout = $timeout; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $key |
36
|
|
|
* @return bool |
37
|
|
|
* @codeCoverageIgnore |
38
|
|
|
*/ |
39
|
|
|
public function exists(string $key): bool |
40
|
|
|
{ |
41
|
|
|
return (0 < $this->redis->exists($key)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $key |
46
|
|
|
* @return string |
47
|
|
|
* @codeCoverageIgnore |
48
|
|
|
*/ |
49
|
|
|
public function get(string $key): string |
50
|
|
|
{ |
51
|
|
|
return strval($this->redis->get($key)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $key |
56
|
|
|
* @param string $data |
57
|
|
|
* @return string |
58
|
|
|
* @codeCoverageIgnore |
59
|
|
|
*/ |
60
|
|
|
public function store(string $key, string $data): string |
61
|
|
|
{ |
62
|
|
|
if (empty($this->timeout)) { |
63
|
|
|
$this->redis->set($key, $data); |
64
|
|
|
} else { |
65
|
|
|
$this->redis->set($key, $data, 'EX', $this->timeout); |
66
|
|
|
} |
67
|
|
|
return $key; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $key |
72
|
|
|
* @throws UploadException |
73
|
|
|
* @return bool |
74
|
|
|
* @codeCoverageIgnore |
75
|
|
|
*/ |
76
|
|
|
public function remove(string $key): bool |
77
|
|
|
{ |
78
|
|
|
return boolval($this->redis->del($key)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function checkKeyEncoder(DrivingFile\KeyEncoders\AEncoder $encoder): bool |
82
|
|
|
{ |
83
|
|
|
if (!$encoder instanceof Interfaces\Storages\ForKV) { |
84
|
|
|
throw new UploadException($this->getUppLang()->uppKeyEncoderVariantIsWrong(get_class($encoder))); |
85
|
|
|
} |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|