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