PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/cache/storage.php

https://bitbucket.org/eternaware/joomus
PHP | 346 lines | 130 code | 35 blank | 181 comment | 10 complexity | b14f410e7436dcdbec2f6b1cff98da94 MD5 | raw file
Possible License(s): LGPL-2.1
  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 JCacheStorage A JCacheStorage instance
  90. *
  91. * @since 11.1
  92. * @throws UnexpectedValueException
  93. * @throws RuntimeException
  94. */
  95. public static function getInstance($handler = null, $options = array())
  96. {
  97. static $now = null;
  98. self::addIncludePath(JPATH_PLATFORM . '/joomla/cache/storage');
  99. if (!isset($handler))
  100. {
  101. $conf = JFactory::getConfig();
  102. $handler = $conf->get('cache_handler');
  103. if (empty($handler))
  104. {
  105. throw new UnexpectedValueException('Cache Storage Handler not set.');
  106. }
  107. }
  108. if (is_null($now))
  109. {
  110. $now = time();
  111. }
  112. $options['now'] = $now;
  113. // We can't cache this since options may change...
  114. $handler = strtolower(preg_replace('/[^A-Z0-9_\.-]/i', '', $handler));
  115. $class = 'JCacheStorage' . ucfirst($handler);
  116. if (!class_exists($class))
  117. {
  118. // Search for the class file in the JCacheStorage include paths.
  119. jimport('joomla.filesystem.path');
  120. if ($path = JPath::find(self::addIncludePath(), strtolower($handler) . '.php'))
  121. {
  122. include_once $path;
  123. }
  124. else
  125. {
  126. throw new RuntimeException(sprintf('Unable to load Cache Storage: %s', $handler));
  127. }
  128. }
  129. return new $class($options);
  130. }
  131. /**
  132. * Get cached data by id and group
  133. *
  134. * @param string $id The cache data id
  135. * @param string $group The cache data group
  136. * @param boolean $checkTime True to verify cache time expiration threshold
  137. *
  138. * @return mixed Boolean false on failure or a cached data object
  139. *
  140. * @since 11.1
  141. */
  142. public function get($id, $group, $checkTime = true)
  143. {
  144. return false;
  145. }
  146. /**
  147. * Get all cached data
  148. *
  149. * @return mixed Boolean false on failure or a cached data object
  150. *
  151. * @since 11.1
  152. * @todo Review this method. The docblock doesn't fit what it actually does.
  153. */
  154. public function getAll()
  155. {
  156. if (!class_exists('JCacheStorageHelper', false))
  157. {
  158. include_once JPATH_PLATFORM . '/joomla/cache/storage/helper.php';
  159. }
  160. return;
  161. }
  162. /**
  163. * Store the data to cache by id and group
  164. *
  165. * @param string $id The cache data id
  166. * @param string $group The cache data group
  167. * @param string $data The data to store in cache
  168. *
  169. * @return boolean True on success, false otherwise
  170. *
  171. * @since 11.1
  172. */
  173. public function store($id, $group, $data)
  174. {
  175. return true;
  176. }
  177. /**
  178. * Remove a cached data entry by id and group
  179. *
  180. * @param string $id The cache data id
  181. * @param string $group The cache data group
  182. *
  183. * @return boolean True on success, false otherwise
  184. *
  185. * @since 11.1
  186. */
  187. public function remove($id, $group)
  188. {
  189. return true;
  190. }
  191. /**
  192. * Clean cache for a group given a mode.
  193. *
  194. * @param string $group The cache data group
  195. * @param string $mode The mode for cleaning cache [group|notgroup]
  196. * group mode : cleans all cache in the group
  197. * notgroup mode : cleans all cache not in the group
  198. *
  199. * @return boolean True on success, false otherwise
  200. *
  201. * @since 11.1
  202. */
  203. public function clean($group, $mode = null)
  204. {
  205. return true;
  206. }
  207. /**
  208. * Garbage collect expired cache data
  209. *
  210. * @return boolean True on success, false otherwise.
  211. *
  212. * @since 11.1
  213. */
  214. public function gc()
  215. {
  216. return true;
  217. }
  218. /**
  219. * Test to see if the storage handler is available.
  220. *
  221. * @return boolean True on success, false otherwise
  222. *
  223. * @since 12.1.
  224. */
  225. public static function isSupported()
  226. {
  227. return true;
  228. }
  229. /**
  230. * Test to see if the storage handler is available.
  231. *
  232. * @return boolean True on success, false otherwise.
  233. *
  234. * @since 11.1
  235. * @deprecated 12.3
  236. */
  237. public static function test()
  238. {
  239. JLog::add('JCacheStorage::test() is deprecated. Use JCacheStorage::isSupported() instead.', JLog::WARNING, 'deprecated');
  240. return static::isSupported();
  241. }
  242. /**
  243. * Lock cached item
  244. *
  245. * @param string $id The cache data id
  246. * @param string $group The cache data group
  247. * @param integer $locktime Cached item max lock time
  248. *
  249. * @return boolean True on success, false otherwise.
  250. *
  251. * @since 11.1
  252. */
  253. public function lock($id, $group, $locktime)
  254. {
  255. return false;
  256. }
  257. /**
  258. * Unlock cached item
  259. *
  260. * @param string $id The cache data id
  261. * @param string $group The cache data group
  262. *
  263. * @return boolean True on success, false otherwise.
  264. *
  265. * @since 11.1
  266. */
  267. public function unlock($id, $group = null)
  268. {
  269. return false;
  270. }
  271. /**
  272. * Get a cache_id string from an id/group pair
  273. *
  274. * @param string $id The cache data id
  275. * @param string $group The cache data group
  276. *
  277. * @return string The cache_id string
  278. *
  279. * @since 11.1
  280. */
  281. protected function _getCacheId($id, $group)
  282. {
  283. $name = md5($this->_application . '-' . $id . '-' . $this->_language);
  284. $this->rawname = $this->_hash . '-' . $name;
  285. return $this->_hash . '-cache-' . $group . '-' . $name;
  286. }
  287. /**
  288. * Add a directory where JCacheStorage should search for handlers. You may
  289. * either pass a string or an array of directories.
  290. *
  291. * @param string $path A path to search.
  292. *
  293. * @return array An array with directory elements
  294. *
  295. * @since 11.1
  296. */
  297. public static function addIncludePath($path = '')
  298. {
  299. static $paths;
  300. if (!isset($paths))
  301. {
  302. $paths = array();
  303. }
  304. if (!empty($path) && !in_array($path, $paths))
  305. {
  306. jimport('joomla.filesystem.path');
  307. array_unshift($paths, JPath::clean($path));
  308. }
  309. return $paths;
  310. }
  311. }