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
|
|
|
/** @var CompositeAdapter */ |
26
|
|
|
protected $cacheLib = null; |
27
|
|
|
/** @var CompositeAdapter */ |
28
|
|
|
protected $reloadLib = null; |
29
|
|
|
/** @var string[] */ |
30
|
|
|
protected $cachePath = []; |
31
|
|
|
/** @var string[] */ |
32
|
|
|
protected $reloadPath = []; |
33
|
|
|
|
34
|
7 |
|
public function __construct(CompositeAdapter $cacheLib, ?CompositeAdapter $reloadLib = null) |
35
|
|
|
{ |
36
|
7 |
|
$this->cacheLib = $cacheLib; |
37
|
7 |
|
$this->reloadLib = $reloadLib ?? $cacheLib; |
38
|
7 |
|
} |
39
|
|
|
|
40
|
6 |
|
public function init(array $what): void |
41
|
|
|
{ |
42
|
6 |
|
$arr = new ArrayPath(); |
43
|
6 |
|
$arr->setArray($what); |
44
|
6 |
|
$this->cachePath = array_merge( |
45
|
6 |
|
$arr->getArrayDirectory(), |
46
|
6 |
|
[$arr->getFileName() . ICache::EXT_CACHE] |
47
|
|
|
);; |
48
|
6 |
|
$this->reloadPath = array_merge( |
49
|
6 |
|
$arr->getArrayDirectory(), |
50
|
6 |
|
[$arr->getFileName() . ICache::EXT_RELOAD] |
51
|
|
|
); |
52
|
6 |
|
} |
53
|
|
|
|
54
|
4 |
|
public function exists(): bool |
55
|
|
|
{ |
56
|
|
|
try { |
57
|
4 |
|
return $this->cacheLib->exists($this->cachePath) && !$this->reloadLib->exists($this->reloadPath); |
58
|
1 |
|
} catch (FilesException | PathsException $ex) { |
59
|
1 |
|
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
public function set(string $content): bool |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
3 |
|
$result = $this->cacheLib->saveFile($this->cachePath, $content); |
67
|
2 |
|
if (false === $result) { |
68
|
1 |
|
return false; |
69
|
|
|
} |
70
|
|
|
// remove signal to save |
71
|
1 |
|
if ($this->reloadLib->exists($this->reloadPath)) { |
72
|
1 |
|
$this->reloadLib->deleteFile($this->reloadPath); |
73
|
|
|
} |
74
|
1 |
|
return true; |
75
|
1 |
|
} catch (FilesException | PathsException $ex) { |
76
|
1 |
|
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
public function get(): string |
81
|
|
|
{ |
82
|
|
|
try { |
83
|
3 |
|
return $this->exists() |
84
|
2 |
|
? $this->toString(Stuff::arrayToPath($this->cachePath), $this->cacheLib->readFile($this->cachePath)) |
85
|
2 |
|
: '' |
86
|
|
|
; |
87
|
1 |
|
} catch (FilesException | PathsException $ex) { |
88
|
1 |
|
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
public function clear(): void |
93
|
|
|
{ |
94
|
|
|
try { |
95
|
2 |
|
$this->cacheLib->deleteFile($this->cachePath); |
96
|
1 |
|
} catch (FilesException | PathsException $ex) { |
97
|
1 |
|
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex); |
98
|
|
|
} |
99
|
1 |
|
} |
100
|
|
|
} |
101
|
|
|
|