PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/cache/storage.php

https://github.com/rietn/minima
PHP | 287 lines | 112 code | 33 blank | 142 comment | 12 complexity | fd5869c82285c186311fc14ae7c33807 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id:storage.php 6961 2007-03-15 16:06:53Z tcp $
  4. * @package Joomla.Framework
  5. * @subpackage Cache
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // No direct access
  10. defined('JPATH_BASE') or die;
  11. /**
  12. * Abstract cache storage handler
  13. *
  14. * @abstract
  15. * @package Joomla.Framework
  16. * @subpackage Cache
  17. * @since 1.5
  18. */
  19. class JCacheStorage
  20. {
  21. /**
  22. * @since 1.6
  23. */
  24. protected $rawname;
  25. /**
  26. * @since 1.6
  27. */
  28. public $_now;
  29. /**
  30. * @since 1.6
  31. */
  32. public $_lifetime;
  33. /**
  34. * @since 1.6
  35. */
  36. public $_locking;
  37. /**
  38. * @since 1.6
  39. */
  40. public $_language;
  41. /**
  42. * @since 1.6
  43. */
  44. public $_application;
  45. /**
  46. * @since 1.6
  47. */
  48. public $_hash;
  49. /**
  50. * Constructor
  51. *
  52. * @param array $options optional parameters
  53. * @since 1.5
  54. */
  55. public function __construct($options = array())
  56. {
  57. $config = JFactory::getConfig();
  58. $this->_hash = md5($config->get('secret'));
  59. $this->_application = (isset($options['application'])) ? $options['application'] : null;
  60. $this->_language = (isset($options['language'])) ? $options['language'] : 'en-GB';
  61. $this->_locking = (isset($options['locking'])) ? $options['locking'] : true;
  62. $this->_lifetime = (isset($options['lifetime'])) ? $options['lifetime']*60 : $config->get('cachetime')*60;
  63. $this->_now = (isset($options['now'])) ? $options['now'] : time();
  64. // Set time threshold value. If the lifetime is not set, default to 60 (0 is BAD)
  65. // _threshold is now available ONLY as a legacy (it's deprecated). It's no longer used in the core.
  66. if (empty($this->_lifetime)) {
  67. $this->_threshold = $this->_now - 60;
  68. $this->_lifetime = 60;
  69. } else {
  70. $this->_threshold = $this->_now - $this->_lifetime;
  71. }
  72. }
  73. /**
  74. * Returns a cache storage handler object, only creating it
  75. * if it doesn't already exist.
  76. *
  77. * @static
  78. * @param string $handler The cache storage handler to instantiate
  79. * @return object A JCacheStorageHandler object
  80. * @since 1.5
  81. */
  82. public static function getInstance($handler=null, $options = array())
  83. {
  84. static $now = null;
  85. JCacheStorage::addIncludePath(JPATH_LIBRARIES.DS.'joomla'.DS.'cache'.DS.'storage');
  86. if (!isset($handler)) {
  87. $conf = JFactory::getConfig();
  88. $handler = $conf->get('cache_handler');
  89. if (empty($handler)) {
  90. return JError::raiseWarning(500, JText::_('JLIB_CACHE_ERROR_CACHE_HANDLER_NOT_SET'));
  91. }
  92. }
  93. if (is_null($now)) {
  94. $now = time();
  95. }
  96. $options['now'] = $now;
  97. //We can't cache this since options may change...
  98. $handler = strtolower(preg_replace('/[^A-Z0-9_\.-]/i', '', $handler));
  99. $class = 'JCacheStorage'.ucfirst($handler);
  100. if (!class_exists($class)) {
  101. // Search for the class file in the JCacheStorage include paths.
  102. jimport('joomla.filesystem.path');
  103. if ($path = JPath::find(JCacheStorage::addIncludePath(), strtolower($handler).'.php')) {
  104. require_once $path;
  105. } else {
  106. return JError::raiseWarning(500, JText::sprintf('JLIB_CACHE_ERROR_CACHE_STORAGE_LOAD', $handler));
  107. }
  108. }
  109. return new $class($options);
  110. }
  111. /**
  112. * Get cached data by id and group
  113. *
  114. * @param string $id The cache data id
  115. * @param string $group The cache data group
  116. * @param boolean $checkTime True to verify cache time expiration threshold
  117. * @return mixed Boolean false on failure or a cached data object
  118. * @since 1.5
  119. */
  120. public function get($id, $group, $checkTime = true)
  121. {
  122. return false;
  123. }
  124. /**
  125. * Get all cached data
  126. *
  127. * @return mixed Boolean false on failure or a cached data object
  128. * @since 1.6
  129. */
  130. public function getAll()
  131. {
  132. if (!class_exists('JCacheStorageHelper', false)) {
  133. require_once JPATH_LIBRARIES.'/joomla/cache/storage/helpers/helper.php';
  134. }
  135. return;
  136. }
  137. /**
  138. * Store the data to cache by id and group
  139. *
  140. * @param string $id The cache data id
  141. * @param string $group The cache data group
  142. * @param string $data The data to store in cache
  143. * @return boolean True on success, false otherwise
  144. * @since 1.5
  145. */
  146. public function store($id, $group, $data)
  147. {
  148. return true;
  149. }
  150. /**
  151. * Remove a cached data entry by id and group
  152. *
  153. * @param string $id The cache data id
  154. * @param string $group The cache data group
  155. * @return boolean True on success, false otherwise
  156. * @since 1.5
  157. */
  158. public function remove($id, $group)
  159. {
  160. return true;
  161. }
  162. /**
  163. * Clean cache for a group given a mode.
  164. *
  165. * group mode : cleans all cache in the group
  166. * notgroup mode : cleans all cache not in the group
  167. *
  168. * @param string $group The cache data group
  169. * @param string $mode The mode for cleaning cache [group|notgroup]
  170. * @return boolean True on success, false otherwise
  171. * @since 1.5
  172. */
  173. public function clean($group, $mode = null)
  174. {
  175. return true;
  176. }
  177. /**
  178. * Garbage collect expired cache data
  179. *
  180. * @return boolean True on success, false otherwise.
  181. */
  182. public function gc()
  183. {
  184. return true;
  185. }
  186. /**
  187. * Test to see if the storage handler is available.
  188. *
  189. * @return boolean True on success, false otherwise.
  190. */
  191. public static function test()
  192. {
  193. return true;
  194. }
  195. /**
  196. * Lock cached item
  197. *
  198. * @param string $id The cache data id
  199. * @param string $group The cache data group
  200. * @param integer $locktime Cached item max lock time
  201. * @return boolean True on success, false otherwise.
  202. * @since 1.6
  203. */
  204. public function lock($id,$group,$locktime)
  205. {
  206. return false;
  207. }
  208. /**
  209. * Unlock cached item
  210. *
  211. * @param string $id The cache data id
  212. * @param string $group The cache data group
  213. * @return boolean True on success, false otherwise.
  214. * @since 1.6
  215. */
  216. public function unlock($id, $group = null)
  217. {
  218. return false;
  219. }
  220. /**
  221. * Get a cache_id string from an id/group pair
  222. *
  223. * @param string $id The cache data id
  224. * @param string $group The cache data group
  225. * @return string The cache_id string
  226. * @since 1.6
  227. */
  228. protected function _getCacheId($id, $group)
  229. {
  230. $name = md5($this->_application.'-'.$id.'-'.$this->_language);
  231. $this->rawname = $this->_hash.'-'.$name;
  232. return $this->_hash.'-cache-'.$group.'-'.$name;
  233. }
  234. /**
  235. * Add a directory where JCacheStorage should search for handlers. You may
  236. * either pass a string or an array of directories.
  237. *
  238. * @param string A path to search.
  239. * @return array An array with directory elements
  240. * @since 1.6
  241. */
  242. public static function addIncludePath($path='')
  243. {
  244. static $paths;
  245. if (!isset($paths)) {
  246. $paths = array();
  247. }
  248. if (!empty($path) && !in_array($path, $paths)) {
  249. jimport('joomla.filesystem.path');
  250. array_unshift($paths, JPath::clean($path));
  251. }
  252. return $paths;
  253. }
  254. }