1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_storage\Storage; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_storage\Interfaces; |
7
|
|
|
use Traversable; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Storage |
12
|
|
|
* @package kalanis\kw_storage\Storage |
13
|
|
|
* Main connection through storage structure |
14
|
|
|
*/ |
15
|
|
|
class Storage implements Interfaces\IStorage |
16
|
|
|
{ |
17
|
|
|
protected Interfaces\Target\ITarget $target; |
18
|
|
|
protected Interfaces\Target\IKey $key; |
19
|
|
|
|
20
|
45 |
|
public function __construct(Interfaces\Target\IKey $key, Interfaces\Target\ITarget $target) |
21
|
|
|
{ |
22
|
45 |
|
$this->target = $target; |
23
|
45 |
|
$this->key = $key; |
24
|
|
|
} |
25
|
|
|
|
26
|
12 |
|
public function canUse(): bool |
27
|
|
|
{ |
28
|
12 |
|
return $this->target->check($this->key->fromSharedKey('')); |
29
|
|
|
} |
30
|
|
|
|
31
|
5 |
|
public function write(string $sharedKey, string $data, ?int $timeout = null): bool |
32
|
|
|
{ |
33
|
5 |
|
return $this->target->save($this->key->fromSharedKey($sharedKey), $data, $timeout); |
34
|
|
|
} |
35
|
|
|
|
36
|
6 |
|
public function read(string $sharedKey): string |
37
|
|
|
{ |
38
|
6 |
|
return $this->target->load($this->key->fromSharedKey($sharedKey)); |
39
|
|
|
} |
40
|
|
|
|
41
|
5 |
|
public function remove(string $sharedKey): bool |
42
|
|
|
{ |
43
|
5 |
|
return $this->target->remove($this->key->fromSharedKey($sharedKey)); |
44
|
|
|
} |
45
|
|
|
|
46
|
9 |
|
public function exists(string $sharedKey): bool |
47
|
|
|
{ |
48
|
9 |
|
return $this->target->exists($this->key->fromSharedKey($sharedKey)); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
public function lookup(string $mask): Traversable |
52
|
|
|
{ |
53
|
3 |
|
return $this->target->lookup($this->key->fromSharedKey($mask)); |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
public function increment(string $key): bool |
57
|
|
|
{ |
58
|
4 |
|
return $this->target->increment($this->key->fromSharedKey($key)); |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
public function decrement(string $key): bool |
62
|
|
|
{ |
63
|
4 |
|
return $this->target->decrement($this->key->fromSharedKey($key)); |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
public function removeMulti(array $keys): array |
67
|
|
|
{ |
68
|
3 |
|
return $this->target->removeMulti(array_map([$this, 'multiKey'], $keys)); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function isFlat(): bool |
72
|
|
|
{ |
73
|
1 |
|
return $this->target instanceof Interfaces\Target\ITargetFlat; |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
public function multiKey(string $key): string |
77
|
|
|
{ |
78
|
3 |
|
return $this->key->fromSharedKey($key); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|