1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_cache\Storage; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_cache\CacheException; |
7
|
|
|
use kalanis\kw_cache\Interfaces\ICache; |
8
|
|
|
use kalanis\kw_paths\ArrayPath; |
9
|
|
|
use kalanis\kw_paths\PathsException; |
10
|
|
|
use kalanis\kw_paths\Stuff; |
11
|
|
|
use kalanis\kw_storage\Interfaces\IStorage; |
12
|
|
|
use kalanis\kw_storage\StorageException; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Dual |
17
|
|
|
* @package kalanis\kw_cache\Storage |
18
|
|
|
* Caching content in storage - file as semaphore |
19
|
|
|
*/ |
20
|
|
|
class Dual implements ICache |
21
|
|
|
{ |
22
|
|
|
protected IStorage $cacheStorage; |
23
|
|
|
protected IStorage $reloadStorage; |
24
|
|
|
/** @var string[] */ |
25
|
|
|
protected array $cachePath = [ICache::EXT_CACHE]; |
26
|
|
|
/** @var string[] */ |
27
|
|
|
protected array $reloadPath = [ICache::EXT_RELOAD]; |
28
|
|
|
|
29
|
7 |
|
public function __construct(IStorage $cacheStorage, ?IStorage $reloadStorage = null) |
30
|
|
|
{ |
31
|
7 |
|
$this->cacheStorage = $cacheStorage; |
32
|
7 |
|
$this->reloadStorage = $reloadStorage ?: $cacheStorage; |
33
|
7 |
|
} |
34
|
|
|
|
35
|
6 |
|
public function init(array $what): void |
36
|
|
|
{ |
37
|
6 |
|
$arr = new ArrayPath(); |
38
|
6 |
|
$arr->setArray($what); |
39
|
6 |
|
$this->cachePath = array_merge( |
40
|
6 |
|
$arr->getArrayDirectory(), |
41
|
6 |
|
[$arr->getFileName() . ICache::EXT_CACHE] |
42
|
|
|
); |
43
|
6 |
|
$this->reloadPath = array_merge( |
44
|
6 |
|
$arr->getArrayDirectory(), |
45
|
6 |
|
[$arr->getFileName() . ICache::EXT_RELOAD] |
46
|
|
|
); |
47
|
6 |
|
} |
48
|
|
|
|
49
|
4 |
|
public function exists(): bool |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
4 |
|
$cachePath = Stuff::arrayToPath($this->cachePath); |
53
|
4 |
|
$reloadPath = Stuff::arrayToPath($this->reloadPath); |
54
|
4 |
|
return $this->cacheStorage->exists($cachePath) && !$this->reloadStorage->exists($reloadPath); |
55
|
1 |
|
} catch (StorageException | PathsException $ex) { |
56
|
1 |
|
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
public function set(string $content): bool |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
3 |
|
$cachePath = Stuff::arrayToPath($this->cachePath); |
64
|
3 |
|
$reloadPath = Stuff::arrayToPath($this->reloadPath); |
65
|
3 |
|
$result = $this->cacheStorage->write($cachePath, $content, null); |
66
|
2 |
|
if (false === $result) { |
67
|
1 |
|
return false; |
68
|
|
|
} |
69
|
|
|
// remove signal to save |
70
|
1 |
|
if ($this->reloadStorage->exists($reloadPath)) { |
71
|
1 |
|
$this->reloadStorage->remove($reloadPath); |
72
|
|
|
} |
73
|
1 |
|
return true; |
74
|
1 |
|
} catch (StorageException | PathsException$ex) { |
75
|
1 |
|
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
public function get(): string |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
3 |
|
$cachePath = Stuff::arrayToPath($this->cachePath); |
83
|
3 |
|
return $this->exists() ? strval($this->cacheStorage->read($cachePath)) : ''; |
84
|
1 |
|
} catch (StorageException | PathsException $ex) { |
85
|
1 |
|
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
public function clear(): void |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
2 |
|
$cachePath = Stuff::arrayToPath($this->cachePath); |
93
|
2 |
|
$this->cacheStorage->remove($cachePath); |
94
|
1 |
|
} catch (StorageException | PathsException $ex) { |
95
|
1 |
|
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex); |
96
|
|
|
} |
97
|
1 |
|
} |
98
|
|
|
} |
99
|
|
|
|