PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/cache/storage.php

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