1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits |
4
|
|
|
of supporting developers from this source code or any supporting source code |
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Cache engine For XOOPS |
14
|
|
|
* |
15
|
|
|
* You may not change or alter any portion of this comment or credits |
16
|
|
|
* of supporting developers from this source code or any supporting source code |
17
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
18
|
|
|
* This program is distributed in the hope that it will be useful, |
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
21
|
|
|
* |
22
|
|
|
* @copyright (c) 2000-2025 XOOPS Project (https://xoops.org) |
23
|
|
|
* @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html) |
24
|
|
|
* @package class |
25
|
|
|
* @subpackage cache |
26
|
|
|
* @since 2.3.0 |
27
|
|
|
* @author Taiwen Jiang <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
defined('XOOPS_ROOT_PATH') || exit('Restricted access'); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Database Storage engine for cache |
33
|
|
|
* |
34
|
|
|
* |
35
|
|
|
* PHP versions 4 and 5 |
36
|
|
|
* |
37
|
|
|
* CakePHP(tm) : Rapid Development Framework <https://www.cakephp.org/> |
38
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc. |
39
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204 |
40
|
|
|
* Las Vegas, Nevada 89104 |
41
|
|
|
* |
42
|
|
|
* Licensed under The MIT License |
43
|
|
|
* Redistributions of files must retain the above copyright notice. |
44
|
|
|
* |
45
|
|
|
* @filesource |
46
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. |
47
|
|
|
* @link https://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project |
48
|
|
|
* @package cake |
49
|
|
|
* @subpackage cake.cake.libs.cache |
50
|
|
|
* @since CakePHP(tm) v 1.2.0.4933 |
51
|
|
|
* @license https://www.opensource.org/licenses/mit-license.php The MIT License |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Database Storage engine for cache |
56
|
|
|
* |
57
|
|
|
* @package cake |
58
|
|
|
* @subpackage cake.cake.libs.cache |
59
|
|
|
*/ |
60
|
|
|
class XoopsCacheModel extends XoopsCacheEngine |
61
|
|
|
{ |
62
|
|
|
/** |
63
|
|
|
* settings |
64
|
|
|
* className = name of the model to use, default => Cache |
65
|
|
|
* fields = database fields that hold data and ttl, default => data, expires |
66
|
|
|
* |
67
|
|
|
* @var array |
68
|
|
|
* @access public |
69
|
|
|
*/ |
70
|
|
|
public $settings = []; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Model instance. |
74
|
|
|
* |
75
|
|
|
* @var object |
76
|
|
|
* @access private |
77
|
|
|
*/ |
78
|
|
|
public $model; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Model instance. |
82
|
|
|
* |
83
|
|
|
* @var object |
84
|
|
|
* @access private |
85
|
|
|
*/ |
86
|
|
|
public $fields = []; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Initialize the Cache Engine |
90
|
|
|
* |
91
|
|
|
* Called automatically by the cache frontend |
92
|
|
|
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array()); |
93
|
|
|
* |
94
|
|
|
* @param array $settings array of setting for the engine |
95
|
|
|
* @return boolean True if the engine has been successfully initialized, false if not |
96
|
|
|
* @access public |
97
|
|
|
*/ |
98
|
|
|
public function init($settings = []) |
99
|
|
|
{ |
100
|
|
|
$xoopsDB = XoopsDatabaseFactory::getDatabaseConnection(); |
101
|
|
|
|
102
|
|
|
parent::init($settings); |
103
|
|
|
$defaults = ['fields' => ['data', 'expires']]; |
104
|
|
|
$this->settings = array_merge($defaults, $this->settings); |
105
|
|
|
$this->fields = $this->settings['fields']; |
106
|
|
|
$this->model = new XoopsCacheModelHandler($xoopsDB); |
107
|
|
|
|
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Garbage collection. Permanently remove all expired and deleted data |
113
|
|
|
* |
114
|
|
|
* @access public |
115
|
|
|
*/ |
116
|
|
|
public function gc() |
117
|
|
|
{ |
118
|
|
|
return $this->model->deleteAll(new Criteria($this->fields[1], time(), '<= ')); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Write data for key into cache |
123
|
|
|
* |
124
|
|
|
* @param string $key Identifier for the data |
125
|
|
|
* @param mixed $value Data to be cached |
126
|
|
|
* @param integer $duration How long to cache the data, in seconds |
127
|
|
|
* @return boolean True if the data was successfully cached, false on failure |
128
|
|
|
* @access public |
129
|
|
|
*/ |
130
|
|
|
public function write($key, $value, $duration = null) |
131
|
|
|
{ |
132
|
|
|
// if (isset($this->settings['serialize'])) { |
133
|
|
|
$value = serialize($value); |
134
|
|
|
// } |
135
|
|
|
if (!$value) { |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
$cache_obj = $this->model->create(); |
139
|
|
|
$cache_obj->setVar($this->model->keyname, $key); |
140
|
|
|
$cache_obj->setVar($this->fields[0], $value); |
141
|
|
|
$cache_obj->setVar($this->fields[1], time() + $duration); |
142
|
|
|
|
143
|
|
|
return $this->model->insert($cache_obj); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Read a key from the cache |
148
|
|
|
* |
149
|
|
|
* @param string $key Identifier for the data |
150
|
|
|
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it |
151
|
|
|
* @access public |
152
|
|
|
*/ |
153
|
|
|
public function read($key) |
154
|
|
|
{ |
155
|
|
|
$criteria = new CriteriaCompo(new Criteria($this->model->keyname, $key)); |
156
|
|
|
$criteria->add(new Criteria($this->fields[1], time(), '>')); |
157
|
|
|
$criteria->setLimit(1); |
158
|
|
|
$data = $this->model->getAll($criteria); |
159
|
|
|
if (!$data) { |
160
|
|
|
return null; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return unserialize($data[0]); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Delete a key from the cache |
168
|
|
|
* |
169
|
|
|
* @param string $key Identifier for the data |
170
|
|
|
* @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed |
171
|
|
|
* @access public |
172
|
|
|
*/ |
173
|
|
|
public function delete($key) |
174
|
|
|
{ |
175
|
|
|
return $this->model->delete($key); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Delete all keys from the cache |
180
|
|
|
* |
181
|
|
|
* @return boolean True if the cache was successfully cleared, false otherwise |
182
|
|
|
* @access public |
183
|
|
|
*/ |
184
|
|
|
public function clear($check = null) |
185
|
|
|
{ |
186
|
|
|
return $this->model->deleteAll(); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* XoopsCacheModelObject |
192
|
|
|
* |
193
|
|
|
* @package |
194
|
|
|
* @author John |
195
|
|
|
* @copyright (c) 2000-2025 XOOPS Project (https://xoops.org) |
196
|
|
|
* @access public |
197
|
|
|
*/ |
198
|
|
|
class XoopsCacheModelObject extends XoopsObject |
199
|
|
|
{ |
200
|
|
|
//PHP 8.2 Dynamic properties deprecated |
201
|
|
|
public $key; |
202
|
|
|
public $data; |
203
|
|
|
public $expires; |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Constructor |
207
|
|
|
*/ |
208
|
|
|
public function __construct() |
209
|
|
|
{ |
210
|
|
|
parent::__construct(); |
211
|
|
|
$this->initVar('key', XOBJ_DTYPE_TXTBOX); |
212
|
|
|
$this->initVar('data', XOBJ_DTYPE_SOURCE); |
213
|
|
|
$this->initVar('expires', XOBJ_DTYPE_INT); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* XoopsCacheModelHandler |
219
|
|
|
* |
220
|
|
|
* @package |
221
|
|
|
* @author John |
222
|
|
|
* @copyright (c) 2000-2025 XOOPS Project (https://xoops.org) |
223
|
|
|
* @access public |
224
|
|
|
*/ |
225
|
|
|
class XoopsCacheModelHandler extends XoopsPersistableObjectHandler |
226
|
|
|
{ |
227
|
|
|
public const TABLE = 'cache_model'; |
228
|
|
|
public const CLASSNAME = 'XoopsCacheModelObject'; |
229
|
|
|
public const KEYNAME = 'key'; |
230
|
|
|
} |
231
|
|
|
|