PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/cache/CacheApc.php

https://gitlab.com/staging06/myproject
PHP | 165 lines | 79 code | 12 blank | 74 comment | 7 complexity | 22847cffe27c42a57c3b70eb51e02cc1 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 APC extension
  28. *
  29. * @since 1.5.0
  30. */
  31. class CacheApcCore extends Cache
  32. {
  33. public function __construct()
  34. {
  35. if (!function_exists('apc_exists'))
  36. {
  37. $this->keys = array();
  38. $cache_info = apc_cache_info((extension_loaded('apcu') === true) ? '' : 'user');
  39. foreach ($cache_info['cache_list'] as $entry)
  40. {
  41. if (isset($entry['key']))
  42. $this->keys[$entry['key']] = $entry['ttl'];
  43. else
  44. $this->keys[$entry['info']] = $entry['ttl'];
  45. }
  46. }
  47. }
  48. /**
  49. * Delete one or several data from cache (* joker can be used, but avoid it !)
  50. * E.g.: delete('*'); delete('my_prefix_*'); delete('my_key_name');
  51. *
  52. * @param string $key
  53. * @return bool
  54. */
  55. public function delete($key)
  56. {
  57. if ($key == '*')
  58. $this->flush();
  59. elseif (strpos($key, '*') === false)
  60. $this->_delete($key);
  61. else
  62. {
  63. $pattern = str_replace('\\*', '.*', preg_quote($key));
  64. $cache_info = apc_cache_info((extension_loaded('apcu') === true) ? '' : 'user');
  65. foreach ($cache_info['cache_list'] as $entry)
  66. {
  67. if (isset($entry['key']))
  68. $key = $entry['key'];
  69. else
  70. $key = $entry['info'];
  71. if (preg_match('#^'.$pattern.'$#', $key))
  72. $this->_delete($key);
  73. }
  74. }
  75. return true;
  76. }
  77. /**
  78. * @see Cache::_set()
  79. */
  80. protected function _set($key, $value, $ttl = 0)
  81. {
  82. return apc_store($key, $value, $ttl);
  83. }
  84. /**
  85. * @see Cache::_get()
  86. */
  87. protected function _get($key)
  88. {
  89. return apc_fetch($key);
  90. }
  91. /**
  92. * @see Cache::_exists()
  93. */
  94. protected function _exists($key)
  95. {
  96. if (!function_exists('apc_exists'))
  97. return isset($this->keys[$key]);
  98. else
  99. return apc_exists($key);
  100. }
  101. /**
  102. * @see Cache::_delete()
  103. */
  104. protected function _delete($key)
  105. {
  106. return apc_delete($key);
  107. }
  108. /**
  109. * @see Cache::_writeKeys()
  110. */
  111. protected function _writeKeys()
  112. {
  113. }
  114. /**
  115. * @see Cache::flush()
  116. */
  117. public function flush()
  118. {
  119. return apc_clear_cache();
  120. }
  121. /**
  122. * Store a data in cache
  123. *
  124. * @param string $key
  125. * @param mixed $value
  126. * @param int $ttl
  127. * @return bool
  128. */
  129. public function set($key, $value, $ttl = 0)
  130. {
  131. return $this->_set($key, $value, $ttl);
  132. }
  133. /**
  134. * Retrieve a data from cache
  135. *
  136. * @param string $key
  137. * @return mixed
  138. */
  139. public function get($key)
  140. {
  141. return $this->_get($key);
  142. }
  143. /**
  144. * Check if a data is cached
  145. *
  146. * @param string $key
  147. * @return bool
  148. */
  149. public function exists($key)
  150. {
  151. return $this->_exists($key);
  152. }
  153. }