CacheliteStorage.php
7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Cache\Storage;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Cache\CacheStorage;
/**
* Cache lite storage handler
*
* @link http://pear.php.net/package/Cache_Lite/
* @since 1.7.0
* @deprecated 4.0 Deprecated without replacement
*/
class CacheliteStorage extends CacheStorage
{
/**
* Singleton Cache_Lite instance
*
* @var \Cache_Lite
* @since 1.7.0
*/
protected static $CacheLiteInstance = null;
/**
* Root path
*
* @var string
* @since 1.7.0
*/
protected $_root;
/**
* Constructor
*
* @param array $options Optional parameters.
*
* @since 1.7.0
*/
public function __construct($options = array())
{
parent::__construct($options);
$this->_root = $options['cachebase'];
$cloptions = array(
'cacheDir' => $this->_root . '/',
'lifeTime' => $this->_lifetime,
'fileLocking' => $this->_locking,
'automaticCleaningFactor' => isset($options['autoclean']) ? $options['autoclean'] : 200,
'fileNameProtection' => false,
'hashedDirectoryLevel' => 0,
'caching' => $options['caching'],
);
if (static::$CacheLiteInstance === null)
{
$this->initCache($cloptions);
}
}
/**
* Instantiates the Cache_Lite object. Only initializes the engine if it does not already exist.
*
* @param array $cloptions optional parameters
*
* @return \Cache_Lite
*
* @since 1.7.0
*/
protected function initCache($cloptions)
{
if (!class_exists('\\Cache_Lite'))
{
require_once 'Cache/Lite.php';
}
static::$CacheLiteInstance = new \Cache_Lite($cloptions);
return static::$CacheLiteInstance;
}
/**
* Check if the cache contains data stored by ID and group
*
* @param string $id The cache data ID
* @param string $group The cache data group
*
* @return boolean
*
* @since 3.7.0
*/
public function contains($id, $group)
{
return $this->get($id, $group) !== false;
}
/**
* Get cached data by ID and group
*
* @param string $id The cache data ID
* @param string $group The cache data group
* @param boolean $checkTime True to verify cache time expiration threshold
*
* @return mixed Boolean false on failure or a cached data object
*
* @since 1.7.0
*/
public function get($id, $group, $checkTime = true)
{
static::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
// This call is needed to ensure $this->rawname is set
$this->_getCacheId($id, $group);
return static::$CacheLiteInstance->get($this->rawname, $group);
}
/**
* Get all cached data
*
* @return mixed Boolean false on failure or a cached data object
*
* @since 1.7.0
*/
public function getAll()
{
$path = $this->_root;
$folders = new \DirectoryIterator($path);
$data = array();
foreach ($folders as $folder)
{
if (!$folder->isDir() || $folder->isDot())
{
continue;
}
$foldername = $folder->getFilename();
$files = new \DirectoryIterator($path . '/' . $foldername);
$item = new CacheStorageHelper($foldername);
foreach ($files as $file)
{
if (!$file->isFile())
{
continue;
}
$filename = $file->getFilename();
$item->updateSize(filesize($path . '/' . $foldername . '/' . $filename));
}
$data[$foldername] = $item;
}
return $data;
}
/**
* Store the data to cache by ID and group
*
* @param string $id The cache data ID
* @param string $group The cache data group
* @param string $data The data to store in cache
*
* @return boolean
*
* @since 1.7.0
*/
public function store($id, $group, $data)
{
$dir = $this->_root . '/' . $group;
// If the folder doesn't exist try to create it
if (!is_dir($dir))
{
// Make sure the index file is there
$indexFile = $dir . '/index.html';
@mkdir($dir) && file_put_contents($indexFile, '<!DOCTYPE html><title></title>');
}
// Make sure the folder exists
if (!is_dir($dir))
{
return false;
}
static::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
// This call is needed to ensure $this->rawname is set
$this->_getCacheId($id, $group);
return static::$CacheLiteInstance->save($data, $this->rawname, $group);
}
/**
* Remove a cached data entry by ID and group
*
* @param string $id The cache data ID
* @param string $group The cache data group
*
* @return boolean
*
* @since 1.7.0
*/
public function remove($id, $group)
{
static::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
// This call is needed to ensure $this->rawname is set
$this->_getCacheId($id, $group);
return static::$CacheLiteInstance->remove($this->rawname, $group);
}
/**
* Clean cache for a group given a mode.
*
* group mode : cleans all cache in the group
* notgroup mode : cleans all cache not in the group
*
* @param string $group The cache data group
* @param string $mode The mode for cleaning cache [group|notgroup]
*
* @return boolean
*
* @since 1.7.0
*/
public function clean($group, $mode = null)
{
\JLoader::import('joomla.filesystem.folder');
\JLoader::import('joomla.filesystem.file');
switch ($mode)
{
case 'notgroup':
$clmode = 'notingroup';
$success = static::$CacheLiteInstance->clean($group, $clmode);
break;
case 'group':
if (is_dir($this->_root . '/' . $group))
{
$clmode = $group;
static::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
$success = static::$CacheLiteInstance->clean($group, $clmode);
// Remove sub-folders of folder; disable all filtering
$folders = \JFolder::folders($this->_root . '/' . $group, '.', false, true, array(), array());
foreach ($folders as $folder)
{
if (is_link($folder))
{
if (\JFile::delete($folder) !== true)
{
return false;
}
}
elseif (\JFolder::delete($folder) !== true)
{
return false;
}
}
}
else
{
$success = true;
}
break;
default:
if (is_dir($this->_root . '/' . $group))
{
$clmode = $group;
static::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
$success = static::$CacheLiteInstance->clean($group, $clmode);
}
else
{
$success = true;
}
break;
}
return $success;
}
/**
* Garbage collect expired cache data
*
* @return boolean
*
* @since 1.7.0
*/
public function gc()
{
$result = true;
static::$CacheLiteInstance->setOption('automaticCleaningFactor', 1);
static::$CacheLiteInstance->setOption('hashedDirectoryLevel', 1);
$success1 = static::$CacheLiteInstance->_cleanDir($this->_root . '/', false, 'old');
if (!($dh = opendir($this->_root . '/')))
{
return false;
}
while ($file = readdir($dh))
{
if (($file != '.') && ($file != '..') && ($file != '.svn'))
{
$file2 = $this->_root . '/' . $file;
if (is_dir($file2))
{
$result = ($result && (static::$CacheLiteInstance->_cleanDir($file2 . '/', false, 'old')));
}
}
}
$success = ($success1 && $result);
return $success;
}
/**
* Test to see if the storage handler is available.
*
* @return boolean
*
* @since 3.0.0
*/
public static function isSupported()
{
@include_once 'Cache/Lite.php';
return class_exists('\Cache_Lite');
}
}