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