3 * @version $Id: cache.php 1956 2009-06-03 06:24:07Z fishbone $
4 * @package Joomla.Framework
6 * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
7 * @license GNU/GPL, see LICENSE.php
8 * Joomla! is free software. This version may have been modified pursuant to the
9 * GNU General Public License, and as distributed it includes or is derivative
10 * of works licensed under the GNU General Public License or other free or open
11 * source software licenses. See COPYRIGHT.php for copyright notices and
15 // Check to ensure this file is within the rest of the framework
16 //defined('JPATH_BASE') or die();
18 //Register the session storage class with the loader
19 //JLoader::register('JCacheStorage', dirname(__FILE__).DS.'storage.php');
22 * Joomla! Cache base object
25 * @package Joomla.Framework
29 class JCache extends JObject
49 * @param array $options options
51 function __construct($options)
53 $this->_options =& $options;
55 // Get the default group and caching
56 if(isset($options['language'])) {
57 $this->_options['language'] = $options['language'];
59 $options['language'] = 'en-GB';
62 if(isset($options['cachebase'])) {
63 $this->_options['cachebase'] = $options['cachebase'];
65 $this->_options['cachebase'] = JPATH_ROOT.DS.'cache';
68 if(isset($options['defaultgroup'])) {
69 $this->_options['defaultgroup'] = $options['defaultgroup'];
71 $this->_options['defaultgroup'] = 'default';
74 if(isset($options['caching'])) {
75 $this->_options['caching'] = $options['caching'];
77 $this->_options['caching'] = true;
80 if( isset($options['storage'])) {
81 $this->_options['storage'] = $options['storage'];
83 $this->_options['storage'] = 'file';
86 //Fix to detect if template positions are enabled...
87 if(JRequest::getCMD('tpl',0)) {
88 $this->_options['caching'] = false;
93 * Returns a reference to a cache adapter object, always creating it
96 * @param string $type The cache object type to instantiate
97 * @return object A JCache object
100 function &getInstance($type = 'output', $options = array())
104 $type = strtolower(preg_replace('/[^A-Z0-9_\.-]/i', '', $type));
106 $class = 'JCache'.ucfirst($type);
107 if(!class_exists($class))
109 //$path = dirname(__FILE__).DS.'handler'.DS.$type.'.php';
110 $path = $gEnvManager->getJoomlaRootPath() . '/class/cache/callback.php';
112 if (file_exists($path)) {
115 JError::raiseError(500, 'Unable to load Cache Handler: '.$type);
119 $instance = new $class($options);
125 * Get the storage handlers
128 * @return array An array of available storage handlers
132 jimport('joomla.filesystem.folder');
133 $handlers = JFolder::files(dirname(__FILE__).DS.'storage', '.php$');
136 foreach($handlers as $handler)
138 $name = substr($handler, 0, strrpos($handler, '.'));
139 $class = 'JCacheStorage'.$name;
141 if(!class_exists($class)) {
142 require_once(dirname(__FILE__).DS.'storage'.DS.$name.'.php');
145 if(call_user_func_array( array( trim($class), 'test' ), null)) {
154 * Set caching enabled state
157 * @param boolean $enabled True to enable caching
161 function setCaching($enabled)
163 $this->_options['caching'] = $enabled;
170 * @param int $lt Cache lifetime
174 function setLifeTime($lt)
176 $this->_options['lifetime'] = $lt;
180 * Set cache validation
186 function setCacheValidation()
192 * Get cached data by id and group
196 * @param string $id The cache data id
197 * @param string $group The cache data group
198 * @return mixed Boolean false on failure or a cached data string
201 function get($id, $group=null)
203 // Get the default group
204 $group = ($group) ? $group : $this->_options['defaultgroup'];
206 // Get the storage handler
207 /* $handler =& $this->_getStorage();
208 if (!JError::isError($handler) && $this->_options['caching']) {
209 return $handler->get($id, $group, (isset($this->_options['checkTime']))? $this->_options['checkTime'] : true);
215 * Store the cached data by id and group
218 * @param string $id The cache data id
219 * @param string $group The cache data group
220 * @param mixed $data The data to store
221 * @return boolean True if cache stored
224 function store($data, $id, $group=null)
226 // Get the default group
227 $group = ($group) ? $group : $this->_options['defaultgroup'];
229 // Get the storage handler and store the cached data
230 /* $handler =& $this->_getStorage();
231 if (!JError::isError($handler) && $this->_options['caching']) {
232 return $handler->store($id, $group, $data);
238 * Remove a cached data entry by id and group
242 * @param string $id The cache data id
243 * @param string $group The cache data group
244 * @return boolean True on success, false otherwise
247 function remove($id, $group=null)
249 // Get the default group
250 $group = ($group) ? $group : $this->_options['defaultgroup'];
252 // Get the storage handler
253 $handler =& $this->_getStorage();
254 if (!JError::isError($handler)) {
255 return $handler->remove($id, $group);
261 * Clean cache for a group given a mode.
263 * group mode : cleans all cache in the group
264 * notgroup mode : cleans all cache not in the group
267 * @param string $group The cache data group
268 * @param string $mode The mode for cleaning cache [group|notgroup]
269 * @return boolean True on success, false otherwise
272 function clean($group=null, $mode='group')
274 // Get the default group
275 $group = ($group) ? $group : $this->_options['defaultgroup'];
277 // Get the storage handler
278 $handler =& $this->_getStorage();
279 if (!JError::isError($handler)) {
280 return $handler->clean($group, $mode);
286 * Garbage collect expired cache data
289 * @return boolean True on success, false otherwise.
294 // Get the storage handler
295 $handler =& $this->_getStorage();
296 if (!JError::isError($handler)) {
297 return $handler->gc();
303 * Get the cache storage handler
306 * @return object A JCacheStorage object
309 function &_getStorage()
311 if (is_a($this->_handler, 'JCacheStorage')) {
312 return $this->_handler;
315 // $this->_handler =& JCacheStorage::getInstance($this->_options['storage'], $this->_options);
316 return $this->_handler;