1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model; |
13
|
|
|
|
14
|
|
|
use Phalcon\Mvc\ModelInterface; |
15
|
|
|
use Zemit\Config\ConfigInterface; |
16
|
|
|
use Zemit\Models\Audit; |
17
|
|
|
use Zemit\Models\AuditDetail; |
18
|
|
|
use Zemit\Models\Session; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Flush Cache on changes |
22
|
|
|
* |
23
|
|
|
* @todo set cache keys |
24
|
|
|
* @todo improve to delete only necessary keys |
25
|
|
|
* @todo improve whiteList system |
26
|
|
|
* @todo precache system |
27
|
|
|
*/ |
28
|
|
|
trait Cache |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Set true to avoid flushing cache for the current instance |
32
|
|
|
*/ |
33
|
|
|
public bool $preventFlushCache = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Whitelisted classes to not force global cache flush on change |
37
|
|
|
*/ |
38
|
|
|
public array $flushModelsCacheBlackList = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get modelsCache service from default DI |
42
|
|
|
*/ |
43
|
|
|
public function getModelsCache(): \Phalcon\Cache |
44
|
|
|
{ |
45
|
|
|
return $this->getDI()->get('modelsCache'); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Initializing Cache |
50
|
|
|
*/ |
51
|
|
|
public function initializeCache(): void |
52
|
|
|
{ |
53
|
|
|
$config = $this->getDI()->get('config'); |
54
|
|
|
assert($config instanceof ConfigInterface); |
55
|
|
|
|
56
|
|
|
$this->flushModelsCacheBlackList [] = $config->getModelClass(Session::class); |
57
|
|
|
$this->flushModelsCacheBlackList [] = $config->getModelClass(Audit::class); |
58
|
|
|
$this->flushModelsCacheBlackList [] = $config->getModelClass(AuditDetail::class); |
59
|
|
|
|
60
|
|
|
$this->addFlushCacheBehavior($this->flushModelsCacheBlackList); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Adding Cache Behavior |
65
|
|
|
*/ |
66
|
|
|
public function addFlushCacheBehavior(?array $flushModelsCacheBlackList = null): void |
67
|
|
|
{ |
68
|
|
|
$flushModelsCacheBlackList ??= $this->flushModelsCacheBlackList; |
69
|
|
|
|
70
|
|
|
// flush cache prevented by current instance |
71
|
|
|
if ($this->preventFlushCache) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// flush cache prevented if current instance class is blacklisted |
76
|
|
|
if ($this->isThisInstanceOf($flushModelsCacheBlackList)) { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$modelsCache = $this->getModelsCache(); |
81
|
|
|
$flushAction = function (ModelInterface $model) use ($modelsCache) { |
82
|
|
|
// Do not flush cache if nothing has changed |
83
|
|
|
return ($model->hasSnapshotData() && !($model->hasUpdated() || $model->hasChanged())) |
|
|
|
|
84
|
|
|
&& $modelsCache->clear(); |
85
|
|
|
}; |
86
|
|
|
|
87
|
|
|
$actions = ['flush' => $flushAction]; |
88
|
|
|
$this->addBehavior(new Behavior\Action([ |
|
|
|
|
89
|
|
|
'afterSave' => $actions, |
90
|
|
|
'afterCreate' => $actions, |
91
|
|
|
'afterUpdate' => $actions, |
92
|
|
|
'afterDelete' => $actions, |
93
|
|
|
'afterRestore' => $actions, |
94
|
|
|
'afterReorder' => $actions, |
95
|
|
|
])); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check whether the current instance is any of the classes |
100
|
|
|
*/ |
101
|
|
|
public function isThisInstanceOf(array $classes = []): bool |
102
|
|
|
{ |
103
|
|
|
// Prevent adding behavior to whiteListed models |
104
|
|
|
foreach ($classes as $class) { |
105
|
|
|
if ($this instanceof $class) { |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|