1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Nip\Cache\Cacheable; |
5
|
|
|
|
6
|
|
|
use DateInterval; |
7
|
|
|
use Nip\Utility\Str; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait CanCache |
11
|
|
|
* @package Nip\Cache\Cacheable |
12
|
|
|
*/ |
13
|
|
|
trait CanCache |
14
|
|
|
{ |
15
|
|
|
use HasCacheStore; |
16
|
|
|
|
17
|
|
|
protected ?CacheData $data = null; |
18
|
|
|
protected $needsCaching = false; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param null $needCaching |
|
|
|
|
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
|
public function needsCaching($needCaching = null): bool |
25
|
|
|
{ |
26
|
|
|
if (is_bool($needCaching)) { |
|
|
|
|
27
|
|
|
$this->needsCaching = $needCaching; |
28
|
|
|
} |
29
|
|
|
return $this->needsCaching; |
30
|
|
|
} |
31
|
|
|
public function getData(): ?CacheData |
32
|
|
|
{ |
33
|
|
|
$this->checkInitData(); |
34
|
|
|
return $this->data; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function checkInitData() |
38
|
|
|
{ |
39
|
|
|
if ($this->data !== null) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
$cacheStore = $this->cacheStore(); |
43
|
|
|
if ($cacheStore->has($this->cacheName())) { |
44
|
|
|
$this->data = $cacheStore->get($this->cacheName()); |
45
|
|
|
} else { |
46
|
|
|
$this->data = new CacheData(); |
47
|
|
|
if (method_exists($this, 'generateCacheData')) { |
48
|
|
|
$data = $this->generateCacheData(); |
49
|
|
|
$this->setDataToCache($data); |
50
|
|
|
} |
51
|
|
|
$this->saveDataToCache(); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function checkSaveCache() |
56
|
|
|
{ |
57
|
|
|
if ($this->needsCaching() !== true) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
$data = $this->getData(); |
61
|
|
|
$this->saveDataToCache($data); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param null $key |
|
|
|
|
66
|
|
|
* @return mixed|null |
67
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
68
|
|
|
*/ |
69
|
|
|
protected function getDataFromCache($key = null, $default = null) |
70
|
|
|
{ |
71
|
|
|
$key = $this->dataCacheKey($key); |
72
|
|
|
if ($this->getData()->offsetExists($key)) { |
73
|
|
|
return $this->getData()->get($key); |
74
|
|
|
} |
75
|
|
|
$value = $this->generateCacheDataKey($key, $default); |
76
|
|
|
$this->setDataToCache($value, $key); |
77
|
|
|
return $value; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $data |
82
|
|
|
* @param $key |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
protected function setDataToCache($data, $key = null) |
86
|
|
|
{ |
87
|
|
|
$key = $this->dataCacheKey($key); |
88
|
|
|
$this->getData()->set($data, $key); |
89
|
|
|
$this->needsCaching(true); |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $data |
95
|
|
|
* @param null $key |
|
|
|
|
96
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
97
|
|
|
*/ |
98
|
|
|
protected function saveDataToCache($data = null, $key = null) |
99
|
|
|
{ |
100
|
|
|
if ($data) { |
101
|
|
|
$this->setDataToCache($data, $key); |
102
|
|
|
} |
103
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
104
|
|
|
$this->cacheStore()->set($this->cacheName(), $this->getData(), $this->dataCacheTtl($key)); |
105
|
|
|
$this->needsCaching(false); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $key |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
protected function dataCacheKey($key = null): string |
113
|
|
|
{ |
114
|
|
|
if ($key !== null) { |
115
|
|
|
return $key; |
116
|
|
|
} |
117
|
|
|
return CacheData::DEFAULT_KEY; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function cacheName(): string |
121
|
|
|
{ |
122
|
|
|
return Str::slug(__CLASS__); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param null $key |
|
|
|
|
127
|
|
|
* @return DateInterval |
128
|
|
|
*/ |
129
|
|
|
protected function dataCacheTtl($key = null) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
return DateInterval::createFromDateString('10 years'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $key |
136
|
|
|
* @param mixed|null $default |
137
|
|
|
* @return mixed|null |
138
|
|
|
*/ |
139
|
|
|
protected function generateCacheDataKey(string $key, mixed $default = null) |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
return $default; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function __destruct() |
145
|
|
|
{ |
146
|
|
|
$this->checkSaveCache(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|