Files
@ a45f0dabc59c
Branch filter:
Location: hot67beta/libraries/joomla/cache/cache.php
a45f0dabc59c
7.1 KiB
text/x-php
mneh
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 | <?php
/**
* @version $Id: cache.php 10707 2008-08-21 09:52:47Z eddieajau $
* @package Joomla.Framework
* @subpackage Cache
* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant to the
* GNU General Public License, and as distributed it includes or is derivative
* of works licensed under the GNU General Public License or other free or open
* source software licenses. See COPYRIGHT.php for copyright notices and
* details.
*/
// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();
//Register the session storage class with the loader
JLoader::register('JCacheStorage', dirname(__FILE__).DS.'storage.php');
/**
* Joomla! Cache base object
*
* @abstract
* @package Joomla.Framework
* @subpackage Cache
* @since 1.5
*/
class JCache extends JObject
{
/**
* Storage Handler
* @access private
* @var object
*/
var $_handler;
/**
* Cache Options
* @access private
* @var array
*/
var $_options;
/**
* Constructor
*
* @access protected
* @param array $options options
*/
function __construct($options)
{
$this->_options =& $options;
// Get the default group and caching
if(isset($options['language'])) {
$this->_options['language'] = $options['language'];
} else {
$options['language'] = 'en-GB';
}
if(isset($options['cachebase'])) {
$this->_options['cachebase'] = $options['cachebase'];
} else {
$this->_options['cachebase'] = JPATH_ROOT.DS.'cache';
}
if(isset($options['defaultgroup'])) {
$this->_options['defaultgroup'] = $options['defaultgroup'];
} else {
$this->_options['defaultgroup'] = 'default';
}
if(isset($options['caching'])) {
$this->_options['caching'] = $options['caching'];
} else {
$this->_options['caching'] = true;
}
if( isset($options['storage'])) {
$this->_options['storage'] = $options['storage'];
} else {
$this->_options['storage'] = 'file';
}
//Fix to detect if template positions are enabled...
if(JRequest::getCMD('tpl',0)) {
$this->_options['caching'] = false;
}
}
/**
* Returns a reference to a cache adapter object, always creating it
*
* @static
* @param string $type The cache object type to instantiate
* @return object A JCache object
* @since 1.5
*/
function &getInstance($type = 'output', $options = array())
{
$type = strtolower(preg_replace('/[^A-Z0-9_\.-]/i', '', $type));
$class = 'JCache'.ucfirst($type);
if(!class_exists($class))
{
$path = dirname(__FILE__).DS.'handler'.DS.$type.'.php';
if (file_exists($path)) {
require_once($path);
} else {
JError::raiseError(500, 'Unable to load Cache Handler: '.$type);
}
}
$instance = new $class($options);
return $instance;
}
/**
* Get the storage handlers
*
* @access public
* @return array An array of available storage handlers
*/
function getStores()
{
jimport('joomla.filesystem.folder');
$handlers = JFolder::files(dirname(__FILE__).DS.'storage', '.php$');
$names = array();
foreach($handlers as $handler)
{
$name = substr($handler, 0, strrpos($handler, '.'));
$class = 'JCacheStorage'.$name;
if(!class_exists($class)) {
require_once(dirname(__FILE__).DS.'storage'.DS.$name.'.php');
}
if(call_user_func_array( array( trim($class), 'test' ), null)) {
$names[] = $name;
}
}
return $names;
}
/**
* Set caching enabled state
*
* @access public
* @param boolean $enabled True to enable caching
* @return void
* @since 1.5
*/
function setCaching($enabled)
{
$this->_options['caching'] = $enabled;
}
/**
* Set cache lifetime
*
* @access public
* @param int $lt Cache lifetime
* @return void
* @since 1.5
*/
function setLifeTime($lt)
{
$this->_options['lifetime'] = $lt;
}
/**
* Set cache validation
*
* @access public
* @return void
* @since 1.5
*/
function setCacheValidation()
{
// Deprecated
}
/**
* Get cached data by id and group
*
* @abstract
* @access public
* @param string $id The cache data id
* @param string $group The cache data group
* @return mixed Boolean false on failure or a cached data string
* @since 1.5
*/
function get($id, $group=null)
{
// Get the default group
$group = ($group) ? $group : $this->_options['defaultgroup'];
// Get the storage handler
$handler =& $this->_getStorage();
if (!JError::isError($handler) && $this->_options['caching']) {
return $handler->get($id, $group, (isset($this->_options['checkTime']))? $this->_options['checkTime'] : true);
}
return false;
}
/**
* Store the cached data by id and group
*
* @access public
* @param string $id The cache data id
* @param string $group The cache data group
* @param mixed $data The data to store
* @return boolean True if cache stored
* @since 1.5
*/
function store($data, $id, $group=null)
{
// Get the default group
$group = ($group) ? $group : $this->_options['defaultgroup'];
// Get the storage handler and store the cached data
$handler =& $this->_getStorage();
if (!JError::isError($handler) && $this->_options['caching']) {
return $handler->store($id, $group, $data);
}
return false;
}
/**
* Remove a cached data entry by id and group
*
* @abstract
* @access public
* @param string $id The cache data id
* @param string $group The cache data group
* @return boolean True on success, false otherwise
* @since 1.5
*/
function remove($id, $group=null)
{
// Get the default group
$group = ($group) ? $group : $this->_options['defaultgroup'];
// Get the storage handler
$handler =& $this->_getStorage();
if (!JError::isError($handler)) {
return $handler->remove($id, $group);
}
return false;
}
/**
* 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
*
* @access public
* @param string $group The cache data group
* @param string $mode The mode for cleaning cache [group|notgroup]
* @return boolean True on success, false otherwise
* @since 1.5
*/
function clean($group=null, $mode='group')
{
// Get the default group
$group = ($group) ? $group : $this->_options['defaultgroup'];
// Get the storage handler
$handler =& $this->_getStorage();
if (!JError::isError($handler)) {
return $handler->clean($group, $mode);
}
return false;
}
/**
* Garbage collect expired cache data
*
* @access public
* @return boolean True on success, false otherwise.
* @since 1.5
*/
function gc()
{
// Get the storage handler
$handler =& $this->_getStorage();
if (!JError::isError($handler)) {
return $handler->gc();
}
return false;
}
/**
* Get the cache storage handler
*
* @access protected
* @return object A JCacheStorage object
* @since 1.5
*/
function &_getStorage()
{
if (is_a($this->_handler, 'JCacheStorage')) {
return $this->_handler;
}
$this->_handler =& JCacheStorage::getInstance($this->_options['storage'], $this->_options);
return $this->_handler;
}
}
|