PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/cache/CacheMemcache.php

https://github.com/netplayer/PrestaShop
PHP | 212 lines | 87 code | 20 blank | 105 comment | 12 complexity | e58fb182fe62a884e62e1d14ad082fb2 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 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-2014 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. if($this->is_connected)
  44. {
  45. $this->keys = @$this->memcache->get(_COOKIE_IV_);
  46. if (!is_array($this->keys))
  47. $this->keys = array();
  48. }
  49. /*
  50. // Get keys (this code comes from Doctrine 2 project)
  51. if(is_array($servers) && count($servers) > 0 && method_exists('Memcache', 'getStats'))
  52. $all_slabs = $this->memcache->getStats('slabs');
  53. if(isset($all_slabs) && is_array($all_slabs))
  54. foreach ($all_slabs as $server => $slabs)
  55. {
  56. if (is_array($slabs))
  57. {
  58. foreach (array_keys($slabs) as $i => $slab_id) // $slab_id is not an int but a string, using the key instead ?
  59. {
  60. if(is_int($i))
  61. {
  62. $dump = $this->memcache->getStats('cachedump', (int)$i);
  63. if ($dump)
  64. {
  65. foreach ($dump as $entries)
  66. {
  67. if($entries)
  68. foreach ($entries as $key => $data)
  69. $this->keys[$key] = $data[1];
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }*/
  76. }
  77. public function __destruct()
  78. {
  79. $this->close();
  80. }
  81. /**
  82. * Connect to memcache server
  83. */
  84. public function connect()
  85. {
  86. if (class_exists('Memcache') && extension_loaded('memcache'))
  87. $this->memcache = new Memcache();
  88. else
  89. return;
  90. $servers = self::getMemcachedServers();
  91. if (!$servers)
  92. return;
  93. foreach ($servers as $server)
  94. $this->memcache->addServer($server['ip'], $server['port'], true, (int) $server['weight']);
  95. $this->is_connected = true;
  96. }
  97. /**
  98. * @see Cache::_set()
  99. */
  100. protected function _set($key, $value, $ttl = 0)
  101. {
  102. if (!$this->is_connected)
  103. return false;
  104. return $this->memcache->set($key, $value, 0, $ttl);
  105. }
  106. /**
  107. * @see Cache::_get()
  108. */
  109. protected function _get($key)
  110. {
  111. if (!$this->is_connected)
  112. return false;
  113. return $this->memcache->get($key);
  114. }
  115. /**
  116. * @see Cache::_exists()
  117. */
  118. protected function _exists($key)
  119. {
  120. if (!$this->is_connected)
  121. return false;
  122. return isset($this->keys[$key]);
  123. }
  124. /**
  125. * @see Cache::_delete()
  126. */
  127. protected function _delete($key)
  128. {
  129. if (!$this->is_connected)
  130. return false;
  131. return $this->memcache->delete($key);
  132. }
  133. /**
  134. * @see Cache::_writeKeys()
  135. */
  136. protected function _writeKeys()
  137. {
  138. if (!$this->is_connected)
  139. return false;
  140. $this->memcache->set(_COOKIE_IV_, $this->keys);
  141. }
  142. /**
  143. * @see Cache::flush()
  144. */
  145. public function flush()
  146. {
  147. if (!$this->is_connected)
  148. return false;
  149. return $this->memcache->flush();
  150. }
  151. /**
  152. * Close connection to memcache server
  153. *
  154. * @return bool
  155. */
  156. protected function close()
  157. {
  158. if (!$this->is_connected)
  159. return false;
  160. return $this->memcache->close();
  161. }
  162. /**
  163. * Add a memcache server
  164. *
  165. * @param string $ip
  166. * @param int $port
  167. * @param int $weight
  168. */
  169. public static function addServer($ip, $port, $weight)
  170. {
  171. return Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'memcached_servers (ip, port, weight) VALUES(\''.pSQL($ip).'\', '.(int)$port.', '.(int)$weight.')', false);
  172. }
  173. /**
  174. * Get list of memcached servers
  175. *
  176. * @return array
  177. */
  178. public static function getMemcachedServers()
  179. {
  180. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT * FROM '._DB_PREFIX_.'memcached_servers', true, false);
  181. }
  182. /**
  183. * Delete a memcache server
  184. *
  185. * @param int $id_server
  186. */
  187. public static function deleteServer($id_server)
  188. {
  189. return Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'memcached_servers WHERE id_memcached_server='.(int)$id_server);
  190. }
  191. }