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