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