1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file allows models to be cached to prevent unnecessary queries to the database |
4
|
|
|
* |
5
|
|
|
* @package BZiON\Models |
6
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A database object with the ability to be cached in the memory |
11
|
|
|
* @package BZiON\Models |
12
|
|
|
*/ |
13
|
|
|
abstract class CachedModel extends BaseModel |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
* |
18
|
|
|
* @return static |
19
|
|
|
*/ |
20
|
77 |
|
public static function get($id) |
21
|
|
|
{ |
22
|
77 |
|
if (is_object($id)) { |
23
|
2 |
|
return parent::get($id); |
24
|
|
|
} |
25
|
|
|
|
26
|
76 |
|
$cache = Service::getModelCache(); |
27
|
76 |
|
$id = (int) $id; |
28
|
|
|
|
29
|
76 |
|
if (!$cache) { |
30
|
|
|
// There is no cache, just get the model |
31
|
|
|
return parent::get($id); |
32
|
|
|
} |
33
|
|
|
|
34
|
76 |
|
if ($model = self::getFromCache($id)) { |
35
|
|
|
// The model exists in the cache, return that to the caller |
36
|
76 |
|
return $model; |
37
|
|
|
} else { |
38
|
76 |
|
return parent::get($id)->storeInCache(); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Find out whether a model exists in the cache |
44
|
|
|
* |
45
|
|
|
* @param int $id The ID of the model |
46
|
|
|
* @return bool True if the model exists in the cache |
47
|
|
|
*/ |
48
|
76 |
|
private static function existsInCache($id) |
49
|
|
|
{ |
50
|
76 |
|
$cache = Service::getModelCache(); |
51
|
|
|
|
52
|
76 |
|
if (!$cache) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
76 |
|
return $cache->has(get_called_class(), $id); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get a model from the cache |
61
|
|
|
* |
62
|
|
|
* @param int $id The ID of the model |
63
|
|
|
* @return static|null The model if it's found, null if it doesn't exist |
64
|
|
|
*/ |
65
|
76 |
|
private static function getFromCache($id) |
66
|
|
|
{ |
67
|
76 |
|
if (!self::existsInCache($id)) { |
68
|
76 |
|
return null; |
69
|
|
|
} |
70
|
|
|
|
71
|
76 |
|
return Service::getModelCache()->get(get_called_class(), $id); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Store the model in the cache |
76
|
|
|
* |
77
|
|
|
* @return self |
78
|
|
|
*/ |
79
|
76 |
|
protected function storeInCache() |
80
|
|
|
{ |
81
|
76 |
|
if (!Service::getModelCache()) { |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
76 |
|
Service::getModelCache()->save($this); |
|
|
|
|
86
|
|
|
|
87
|
76 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Fetch a model's data from the database again |
92
|
|
|
* @return static The new model |
93
|
|
|
*/ |
94
|
76 |
|
public function refresh() |
95
|
|
|
{ |
96
|
76 |
|
$this->getFromDatabase(); |
97
|
|
|
|
98
|
76 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get a model's data from the database |
103
|
|
|
*/ |
104
|
76 |
|
private function getFromDatabase() |
105
|
|
|
{ |
106
|
76 |
|
self::__construct($this->id); |
107
|
|
|
|
108
|
76 |
|
if ($this->loaded) { |
109
|
|
|
// Reload the lazy parameters of the model if they're loaded already |
110
|
76 |
|
$this->lazyLoad(true); |
111
|
|
|
} |
112
|
76 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
76 |
|
protected static function create($params, $now = null, $table = '') |
118
|
|
|
{ |
119
|
76 |
|
$model = parent::create($params, $now, $table); |
120
|
76 |
|
$model->storeInCache(); |
121
|
|
|
|
122
|
76 |
|
return $model; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.