PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/cache/storage/memcached.php

https://github.com/Hackwar/joomla-platform
PHP | 498 lines | 271 code | 81 blank | 146 comment | 45 complexity | 2b252c4a2abf95a3449438610ad1915e MD5 | raw file
  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. * Memcached cache storage handler
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Cache
  15. * @see http://php.net/manual/en/book.memcached.php
  16. * @since 12.1
  17. */
  18. class JCacheStorageMemcached extends JCacheStorage
  19. {
  20. /**
  21. * @var Memcached
  22. * @since 12.1
  23. */
  24. protected static $_db = null;
  25. /**
  26. * @var boolean
  27. * @since 12.1
  28. */
  29. protected $_persistent = false;
  30. /**
  31. * @var
  32. * @since 12.1
  33. */
  34. protected $_compress = 0;
  35. /**
  36. * Constructor
  37. *
  38. * @param array $options Optional parameters.
  39. *
  40. * @since 12.1
  41. */
  42. public function __construct($options = array())
  43. {
  44. parent::__construct($options);
  45. if (self::$_db === null)
  46. {
  47. $this->getConnection();
  48. }
  49. }
  50. /**
  51. * Return memcached connection object
  52. *
  53. * @return object memcached connection object
  54. *
  55. * @since 12.1
  56. * @throws RuntimeException
  57. */
  58. protected function getConnection()
  59. {
  60. if ((extension_loaded('memcached') && class_exists('Memcached')) != true)
  61. {
  62. return false;
  63. }
  64. $config = JFactory::getConfig();
  65. $this->_persistent = $config->get('memcache_persist', true);
  66. $this->_compress = $config->get('memcache_compress', false) == false ? 0 : Memcached::OPT_COMPRESSION;
  67. /*
  68. * This will be an array of loveliness
  69. * @todo: multiple servers
  70. * $servers = (isset($params['servers'])) ? $params['servers'] : array();
  71. */
  72. $server = array();
  73. $server['host'] = $config->get('memcache_server_host', 'localhost');
  74. $server['port'] = $config->get('memcache_server_port', 11211);
  75. // Create the memcache connection
  76. if ($this->_persistent)
  77. {
  78. $session = JFactory::getSession();
  79. self::$_db = new Memcached($session->getId());
  80. }
  81. else
  82. {
  83. self::$_db = new Memcached;
  84. }
  85. $memcachedtest = self::$_db->addServer($server['host'], $server['port']);
  86. if ($memcachedtest == false)
  87. {
  88. throw new RuntimeException('Could not connect to memcached server', 404);
  89. }
  90. self::$_db->setOption(Memcached::OPT_COMPRESSION, $this->_compress);
  91. // Memcached has no list keys, we do our own accounting, initialise key index
  92. if (self::$_db->get($this->_hash . '-index') === false)
  93. {
  94. $empty = array();
  95. self::$_db->set($this->_hash . '-index', $empty, 0);
  96. }
  97. return;
  98. }
  99. /**
  100. * Get cached data from memcached by id and group
  101. *
  102. * @param string $id The cache data id
  103. * @param string $group The cache data group
  104. * @param boolean $checkTime True to verify cache time expiration threshold
  105. *
  106. * @return mixed Boolean false on failure or a cached data string
  107. *
  108. * @since 12.1
  109. */
  110. public function get($id, $group, $checkTime = true)
  111. {
  112. $cache_id = $this->_getCacheId($id, $group);
  113. $back = self::$_db->get($cache_id);
  114. return $back;
  115. }
  116. /**
  117. * Get all cached data
  118. *
  119. * @return array data
  120. *
  121. * @since 12.1
  122. */
  123. public function getAll()
  124. {
  125. parent::getAll();
  126. $keys = self::$_db->get($this->_hash . '-index');
  127. $secret = $this->_hash;
  128. $data = array();
  129. if (!empty($keys) && is_array($keys))
  130. {
  131. foreach ($keys as $key)
  132. {
  133. if (empty($key))
  134. {
  135. continue;
  136. }
  137. $namearr = explode('-', $key->name);
  138. if ($namearr !== false && $namearr[0] == $secret && $namearr[1] == 'cache')
  139. {
  140. $group = $namearr[2];
  141. if (!isset($data[$group]))
  142. {
  143. $item = new JCacheStorageHelper($group);
  144. }
  145. else
  146. {
  147. $item = $data[$group];
  148. }
  149. $item->updateSize($key->size / 1024);
  150. $data[$group] = $item;
  151. }
  152. }
  153. }
  154. return $data;
  155. }
  156. /**
  157. * Store the data to memcached by id and group
  158. *
  159. * @param string $id The cache data id
  160. * @param string $group The cache data group
  161. * @param string $data The data to store in cache
  162. *
  163. * @return boolean True on success, false otherwise
  164. *
  165. * @since 12.1
  166. */
  167. public function store($id, $group, $data)
  168. {
  169. $cache_id = $this->_getCacheId($id, $group);
  170. if (!$this->lockindex())
  171. {
  172. return false;
  173. }
  174. $index = self::$_db->get($this->_hash . '-index');
  175. if ($index === false)
  176. {
  177. $index = array();
  178. }
  179. $tmparr = new stdClass;
  180. $tmparr->name = $cache_id;
  181. $tmparr->size = strlen($data);
  182. $index[] = $tmparr;
  183. self::$_db->replace($this->_hash . '-index', $index, 0);
  184. $this->unlockindex();
  185. // Prevent double writes, write only if it doesn't exist else replace
  186. if (!self::$_db->replace($cache_id, $data, $this->_lifetime))
  187. {
  188. self::$_db->set($cache_id, $data, $this->_lifetime);
  189. }
  190. return true;
  191. }
  192. /**
  193. * Remove a cached data entry by id and group
  194. *
  195. * @param string $id The cache data id
  196. * @param string $group The cache data group
  197. *
  198. * @return boolean True on success, false otherwise
  199. *
  200. * @since 12.1
  201. */
  202. public function remove($id, $group)
  203. {
  204. $cache_id = $this->_getCacheId($id, $group);
  205. if (!$this->lockindex())
  206. {
  207. return false;
  208. }
  209. $index = self::$_db->get($this->_hash . '-index');
  210. if ($index === false)
  211. {
  212. $index = array();
  213. }
  214. foreach ($index as $key => $value)
  215. {
  216. if ($value->name == $cache_id)
  217. {
  218. unset($index[$key]);
  219. }
  220. break;
  221. }
  222. self::$_db->replace($this->_hash . '-index', $index, 0);
  223. $this->unlockindex();
  224. return self::$_db->delete($cache_id);
  225. }
  226. /**
  227. * Clean cache for a group given a mode.
  228. *
  229. * @param string $group The cache data group
  230. * @param string $mode The mode for cleaning cache [group|notgroup]
  231. * group mode : cleans all cache in the group
  232. * notgroup mode : cleans all cache not in the group
  233. *
  234. * @return boolean True on success, false otherwise
  235. *
  236. * @since 12.1
  237. */
  238. public function clean($group, $mode = null)
  239. {
  240. if (!$this->lockindex())
  241. {
  242. return false;
  243. }
  244. $index = self::$_db->get($this->_hash . '-index');
  245. if ($index === false)
  246. {
  247. $index = array();
  248. }
  249. $secret = $this->_hash;
  250. foreach ($index as $key => $value)
  251. {
  252. if (strpos($value->name, $secret . '-cache-' . $group . '-') === 0 xor $mode != 'group')
  253. {
  254. self::$_db->delete($value->name, 0);
  255. unset($index[$key]);
  256. }
  257. }
  258. self::$_db->replace($this->_hash . '-index', $index, 0);
  259. $this->unlockindex();
  260. return true;
  261. }
  262. /**
  263. * Test to see if the cache storage is available.
  264. *
  265. * @return boolean True on success, false otherwise.
  266. *
  267. * @since 12.1
  268. */
  269. public static function isSupported()
  270. {
  271. if ((extension_loaded('memcached') && class_exists('Memcached')) != true)
  272. {
  273. return false;
  274. }
  275. $config = JFactory::getConfig();
  276. $host = $config->get('memcache_server_host', 'localhost');
  277. $port = $config->get('memcache_server_port', 11211);
  278. $memcached = new Memcached;
  279. $memcachedtest = @$memcached->addServer($host, $port);
  280. if (!$memcachedtest)
  281. {
  282. return false;
  283. }
  284. else
  285. {
  286. return true;
  287. }
  288. }
  289. /**
  290. * Lock cached item - override parent as this is more efficient
  291. *
  292. * @param string $id The cache data id
  293. * @param string $group The cache data group
  294. * @param integer $locktime Cached item max lock time
  295. *
  296. * @return boolean True on success, false otherwise.
  297. *
  298. * @since 12.1
  299. */
  300. public function lock($id, $group, $locktime)
  301. {
  302. $returning = new stdClass;
  303. $returning->locklooped = false;
  304. $looptime = $locktime * 10;
  305. $cache_id = $this->_getCacheId($id, $group);
  306. if (!$this->lockindex())
  307. {
  308. return false;
  309. }
  310. $index = self::$_db->get($this->_hash . '-index');
  311. if ($index === false)
  312. {
  313. $index = array();
  314. }
  315. $tmparr = new stdClass;
  316. $tmparr->name = $cache_id;
  317. $tmparr->size = 1;
  318. $index[] = $tmparr;
  319. self::$_db->replace($this->_hash . '-index', $index, 0);
  320. $this->unlockindex();
  321. $data_lock = self::$_db->add($cache_id . '_lock', 1, $locktime);
  322. if ($data_lock === false)
  323. {
  324. $lock_counter = 0;
  325. // Loop until you find that the lock has been released.
  326. // That implies that data get from other thread has finished
  327. while ($data_lock === false)
  328. {
  329. if ($lock_counter > $looptime)
  330. {
  331. $returning->locked = false;
  332. $returning->locklooped = true;
  333. break;
  334. }
  335. usleep(100);
  336. $data_lock = self::$_db->add($cache_id . '_lock', 1, $locktime);
  337. $lock_counter++;
  338. }
  339. }
  340. $returning->locked = $data_lock;
  341. return $returning;
  342. }
  343. /**
  344. * Unlock cached item - override parent for cacheid compatibility with lock
  345. *
  346. * @param string $id The cache data id
  347. * @param string $group The cache data group
  348. *
  349. * @return boolean True on success, false otherwise.
  350. *
  351. * @since 12.1
  352. */
  353. public function unlock($id, $group = null)
  354. {
  355. $cache_id = $this->_getCacheId($id, $group) . '_lock';
  356. if (!$this->lockindex())
  357. {
  358. return false;
  359. }
  360. $index = self::$_db->get($this->_hash . '-index');
  361. if ($index === false)
  362. {
  363. $index = array();
  364. }
  365. foreach ($index as $key => $value)
  366. {
  367. if ($value->name == $cache_id)
  368. {
  369. unset($index[$key]);
  370. }
  371. break;
  372. }
  373. self::$_db->replace($this->_hash . '-index', $index, 0);
  374. $this->unlockindex();
  375. return self::$_db->delete($cache_id);
  376. }
  377. /**
  378. * Lock cache index
  379. *
  380. * @return boolean True on success, false otherwise.
  381. *
  382. * @since 12.1
  383. */
  384. protected function lockindex()
  385. {
  386. $looptime = 300;
  387. $data_lock = self::$_db->add($this->_hash . '-index_lock', 1, 30);
  388. if ($data_lock === false)
  389. {
  390. $lock_counter = 0;
  391. // Loop until you find that the lock has been released. that implies that data get from other thread has finished
  392. while ($data_lock === false)
  393. {
  394. if ($lock_counter > $looptime)
  395. {
  396. return false;
  397. break;
  398. }
  399. usleep(100);
  400. $data_lock = self::$_db->add($this->_hash . '-index_lock', 1, 30);
  401. $lock_counter++;
  402. }
  403. }
  404. return true;
  405. }
  406. /**
  407. * Unlock cache index
  408. *
  409. * @return boolean True on success, false otherwise.
  410. *
  411. * @since 12.1
  412. */
  413. protected function unlockindex()
  414. {
  415. return self::$_db->delete($this->_hash . '-index_lock');
  416. }
  417. }