/classes/cache/CacheMemcache.php

https://gitlab.com/staging06/myproject · PHP · 265 lines · 138 code · 22 blank · 105 comment · 22 complexity · a2dd374f7edbd32528a17a65c000882c MD5 · raw file

  1. <?php
  2. /*
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. /**
  27. * This class require PECL Memcache extension
  28. *
  29. */
  30. class CacheMemcacheCore extends Cache
  31. {
  32. /**
  33. * @var Memcache
  34. */
  35. protected $memcache;
  36. /**
  37. * @var bool Connection status
  38. */
  39. protected $is_connected = false;
  40. public function __construct()
  41. {
  42. $this->connect();
  43. }
  44. public function __destruct()
  45. {
  46. $this->close();
  47. }
  48. /**
  49. * Connect to memcache server
  50. */
  51. public function connect()
  52. {
  53. if (class_exists('Memcache') && extension_loaded('memcache'))
  54. $this->memcache = new Memcache();
  55. else
  56. return;
  57. $servers = self::getMemcachedServers();
  58. if (!$servers)
  59. return;
  60. foreach ($servers as $server)
  61. $this->memcache->addServer($server['ip'], $server['port'], true, (int)$server['weight']);
  62. $this->is_connected = true;
  63. }
  64. /**
  65. * @see Cache::_set()
  66. */
  67. protected function _set($key, $value, $ttl = 0)
  68. {
  69. if (!$this->is_connected)
  70. return false;
  71. return $this->memcache->set($key, $value, 0, $ttl);
  72. }
  73. /**
  74. * @see Cache::_get()
  75. */
  76. protected function _get($key)
  77. {
  78. if (!$this->is_connected)
  79. return false;
  80. return $this->memcache->get($key);
  81. }
  82. /**
  83. * @see Cache::_exists()
  84. */
  85. protected function _exists($key)
  86. {
  87. if (!$this->is_connected)
  88. return false;
  89. return ($this->memcache->get($key) !== false);
  90. }
  91. /**
  92. * @see Cache::_delete()
  93. */
  94. protected function _delete($key)
  95. {
  96. if (!$this->is_connected)
  97. return false;
  98. return $this->memcache->delete($key);
  99. }
  100. /**
  101. * @see Cache::_writeKeys()
  102. */
  103. protected function _writeKeys()
  104. {
  105. if (!$this->is_connected)
  106. return false;
  107. return true;
  108. }
  109. /**
  110. * @see Cache::flush()
  111. */
  112. public function flush()
  113. {
  114. if (!$this->is_connected)
  115. return false;
  116. return $this->memcache->flush();
  117. }
  118. /**
  119. * Store a data in cache
  120. *
  121. * @param string $key
  122. * @param mixed $value
  123. * @param int $ttl
  124. * @return bool
  125. */
  126. public function set($key, $value, $ttl = 0)
  127. {
  128. return $this->_set($key, $value, $ttl);
  129. }
  130. /**
  131. * Retrieve a data from cache
  132. *
  133. * @param string $key
  134. * @return mixed
  135. */
  136. public function get($key)
  137. {
  138. return $this->_get($key);
  139. }
  140. /**
  141. * Check if a data is cached
  142. *
  143. * @param string $key
  144. * @return bool
  145. */
  146. public function exists($key)
  147. {
  148. return $this->_exists($key);
  149. }
  150. /**
  151. * Delete one or several data from cache (* joker can be used, but avoid it !)
  152. * E.g.: delete('*'); delete('my_prefix_*'); delete('my_key_name');
  153. *
  154. * @param string $key
  155. * @return bool
  156. */
  157. public function delete($key)
  158. {
  159. if ($key == '*')
  160. $this->flush();
  161. elseif (strpos($key, '*') === false)
  162. $this->_delete($key);
  163. else
  164. {
  165. // Get keys (this code comes from Doctrine 2 project)
  166. $pattern = str_replace('\\*', '.*', preg_quote($key));
  167. $servers = $this->getMemcachedServers();
  168. if (is_array($servers) && count($servers) > 0 && method_exists('Memcache', 'getStats'))
  169. $all_slabs = $this->memcache->getStats('slabs');
  170. if (isset($all_slabs) && is_array($all_slabs))
  171. {
  172. foreach ($all_slabs as $server => $slabs)
  173. {
  174. if (is_array($slabs))
  175. {
  176. foreach (array_keys($slabs) as $i => $slab_id) // $slab_id is not an int but a string, using the key instead ?
  177. {
  178. if (is_int($i))
  179. {
  180. $dump = $this->memcache->getStats('cachedump', (int)$i);
  181. if ($dump)
  182. {
  183. foreach ($dump as $entries)
  184. {
  185. if ($entries)
  186. {
  187. foreach ($entries as $key => $data)
  188. {
  189. if (preg_match('#^'.$pattern.'$#', $key))
  190. $this->_delete($key);
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. return true;
  202. }
  203. /**
  204. * Close connection to memcache server
  205. *
  206. * @return bool
  207. */
  208. protected function close()
  209. {
  210. if (!$this->is_connected)
  211. return false;
  212. return $this->memcache->close();
  213. }
  214. /**
  215. * Add a memcache server
  216. *
  217. * @param string $ip
  218. * @param int $port
  219. * @param int $weight
  220. */
  221. public static function addServer($ip, $port, $weight)
  222. {
  223. return Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'memcached_servers (ip, port, weight) VALUES(\''.pSQL($ip).'\', '.(int)$port.', '.(int)$weight.')', false);
  224. }
  225. /**
  226. * Get list of memcached servers
  227. *
  228. * @return array
  229. */
  230. public static function getMemcachedServers()
  231. {
  232. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT * FROM '._DB_PREFIX_.'memcached_servers', true, false);
  233. }
  234. /**
  235. * Delete a memcache server
  236. *
  237. * @param int $id_server
  238. */
  239. public static function deleteServer($id_server)
  240. {
  241. return Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'memcached_servers WHERE id_memcached_server='.(int)$id_server);
  242. }
  243. }