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

/classes/cache/CacheMemcached.php

https://gitlab.com/staging06/myproject
PHP | 242 lines | 117 code | 21 blank | 104 comment | 15 complexity | c910b8aedee90a0951b223632b7dc085 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 Memcached extension
  28. *
  29. */
  30. class CacheMemcachedCore extends Cache
  31. {
  32. /**
  33. * @var Memcached
  34. */
  35. protected $memcached;
  36. /**
  37. * @var bool Connection status
  38. */
  39. protected $is_connected = false;
  40. public function __construct()
  41. {
  42. $this->connect();
  43. if ($this->is_connected)
  44. {
  45. $this->memcached->setOption(Memcached::OPT_PREFIX_KEY, _DB_PREFIX_);
  46. if ($this->memcached->getOption(Memcached::HAVE_IGBINARY))
  47. $this->memcached->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY);
  48. }
  49. }
  50. public function __destruct()
  51. {
  52. $this->close();
  53. }
  54. /**
  55. * Connect to memcached server
  56. */
  57. public function connect()
  58. {
  59. if (class_exists('Memcached') && extension_loaded('memcached'))
  60. $this->memcached = new Memcached();
  61. else
  62. return;
  63. $servers = self::getMemcachedServers();
  64. if (!$servers)
  65. return;
  66. foreach ($servers as $server)
  67. $this->memcached->addServer($server['ip'], $server['port'], (int) $server['weight']);
  68. $this->is_connected = in_array('255.255.255', $this->memcached->getVersion(), true) === false;
  69. }
  70. /**
  71. * @see Cache::_set()
  72. */
  73. protected function _set($key, $value, $ttl = 0)
  74. {
  75. if (!$this->is_connected)
  76. return false;
  77. return $this->memcached->set($key, $value, $ttl);
  78. }
  79. /**
  80. * @see Cache::_get()
  81. */
  82. protected function _get($key)
  83. {
  84. if (!$this->is_connected)
  85. return false;
  86. return $this->memcached->get($key);
  87. }
  88. /**
  89. * @see Cache::_exists()
  90. */
  91. protected function _exists($key)
  92. {
  93. if (!$this->is_connected)
  94. return false;
  95. return ($this->memcached->get($key) !== false);
  96. }
  97. /**
  98. * @see Cache::_delete()
  99. */
  100. protected function _delete($key)
  101. {
  102. if (!$this->is_connected)
  103. return false;
  104. return $this->memcached->delete($key);
  105. }
  106. /**
  107. * @see Cache::_writeKeys()
  108. */
  109. protected function _writeKeys()
  110. {
  111. if (!$this->is_connected)
  112. return false;
  113. return true;
  114. }
  115. /**
  116. * @see Cache::flush()
  117. */
  118. public function flush()
  119. {
  120. if (!$this->is_connected)
  121. return false;
  122. return $this->memcached->flush();
  123. }
  124. /**
  125. * Store a data in cache
  126. *
  127. * @param string $key
  128. * @param mixed $value
  129. * @param int $ttl
  130. * @return bool
  131. */
  132. public function set($key, $value, $ttl = 0)
  133. {
  134. return $this->_set($key, $value, $ttl);
  135. }
  136. /**
  137. * Retrieve a data from cache
  138. *
  139. * @param string $key
  140. * @return mixed
  141. */
  142. public function get($key)
  143. {
  144. return $this->_get($key);
  145. }
  146. /**
  147. * Check if a data is cached
  148. *
  149. * @param string $key
  150. * @return bool
  151. */
  152. public function exists($key)
  153. {
  154. return $this->_exists($key);
  155. }
  156. /**
  157. * Delete one or several data from cache (* joker can be used, but avoid it !)
  158. * E.g.: delete('*'); delete('my_prefix_*'); delete('my_key_name');
  159. *
  160. * @param string $key
  161. * @return bool
  162. */
  163. public function delete($key)
  164. {
  165. if ($key == '*')
  166. $this->flush();
  167. elseif (strpos($key, '*') === false)
  168. $this->_delete($key);
  169. else
  170. {
  171. $pattern = str_replace('\\*', '.*', preg_quote($key));
  172. $keys = $this->memcached->getAllKeys();
  173. foreach ($keys as $key => $data)
  174. {
  175. if (preg_match('#^'.$pattern.'$#', $key))
  176. $this->_delete($key);
  177. }
  178. }
  179. return true;
  180. }
  181. /**
  182. * Close connection to memcache server
  183. *
  184. * @return bool
  185. */
  186. protected function close()
  187. {
  188. if (!$this->is_connected)
  189. return false;
  190. return $this->memcached->quit();
  191. }
  192. /**
  193. * Add a memcached server
  194. *
  195. * @param string $ip
  196. * @param int $port
  197. * @param int $weight
  198. */
  199. public static function addServer($ip, $port, $weight)
  200. {
  201. return Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'memcached_servers (ip, port, weight) VALUES(\''.pSQL($ip).'\', '.(int)$port.', '.(int)$weight.')', false);
  202. }
  203. /**
  204. * Get list of memcached servers
  205. *
  206. * @return array
  207. */
  208. public static function getMemcachedServers()
  209. {
  210. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT * FROM '._DB_PREFIX_.'memcached_servers', true, false);
  211. }
  212. /**
  213. * Delete a memcached server
  214. *
  215. * @param int $id_server
  216. */
  217. public static function deleteServer($id_server)
  218. {
  219. return Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'memcached_servers WHERE id_memcached_server='.(int)$id_server);
  220. }
  221. }