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

/lib/Varien/Cache/Core.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 241 lines | 93 code | 26 blank | 122 comment | 17 complexity | 8299daa89aa8e5d420aca12dc3c90d92 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  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@magento.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 Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Varien
  22. * @package Varien_Cache
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Varien_Cache_Core extends Zend_Cache_Core
  27. {
  28. /**
  29. * Specific slab size = 1Mb minus overhead
  30. *
  31. * @var array $_specificOptions
  32. */
  33. protected $_specificOptions = array('slab_size' => 0);
  34. /**
  35. * Used to tell chunked data from ordinary
  36. */
  37. const CODE_WORD = '{splitted}';
  38. /**
  39. * Constructor
  40. *
  41. * @throws Varien_Exception
  42. * @param array|Zend_Config $options Associative array of options or Zend_Config instance
  43. */
  44. public function __construct($options = array())
  45. {
  46. parent::__construct($options);
  47. if (!is_numeric($this->getOption('slab_size'))) {
  48. throw new Varien_Exception("Invalid value for the node <slab_size>. Expected to be integer.");
  49. }
  50. }
  51. /**
  52. * Returns ID of a specific chunk on the basis of data's ID
  53. *
  54. * @param string $id Main data's ID
  55. * @param int $index Particular chunk number to return ID for
  56. * @return string
  57. */
  58. protected function _getChunkId($id, $index)
  59. {
  60. return "{$id}[{$index}]";
  61. }
  62. /**
  63. * Remove saved chunks in case something gone wrong (e.g. some chunk from the chain can not be found)
  64. *
  65. * @param string $id ID of data's info cell
  66. * @param int $chunks Number of chunks to remove (basically, the number after '{splitted}|')
  67. * @return null
  68. */
  69. protected function _cleanTheMess($id, $chunks)
  70. {
  71. for ($i = 0; $i < $chunks; $i++) {
  72. $this->remove($this->_getChunkId($id, $i));
  73. }
  74. $this->remove($id);
  75. }
  76. /**
  77. * Make and return a cache id
  78. *
  79. * Checks 'cache_id_prefix' and returns new id with prefix or simply the id if null
  80. *
  81. * @param string $id Cache id
  82. * @return string Cache id (with or without prefix)
  83. */
  84. protected function _id($id)
  85. {
  86. if ($id !== null) {
  87. $id = preg_replace('/([^a-zA-Z0-9_]{1,1})/', '_', $id);
  88. if (isset($this->_options['cache_id_prefix'])) {
  89. $id = $this->_options['cache_id_prefix'] . $id;
  90. }
  91. }
  92. return $id;
  93. }
  94. /**
  95. * Prepare tags
  96. *
  97. * @param array $tags
  98. * @return array
  99. */
  100. protected function _tags($tags)
  101. {
  102. foreach ($tags as $key=>$tag) {
  103. $tags[$key] = $this->_id($tag);
  104. }
  105. return $tags;
  106. }
  107. /**
  108. * Save some data in a cache
  109. *
  110. * @param mixed $data Data to put in cache (can be another type than string if automatic_serialization is on)
  111. * @param string $id Cache id (if not set, the last cache id will be used)
  112. * @param array $tags Cache tags
  113. * @param bool|int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
  114. * @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
  115. * @return boolean True if no problem
  116. */
  117. public function save($data, $id = null, $tags = array(), $specificLifetime = false, $priority = 8)
  118. {
  119. $tags = $this->_tags($tags);
  120. if ($this->getOption('slab_size') && is_string($data) && (strlen($data) > $this->getOption('slab_size'))) {
  121. $dataChunks = str_split($data, $this->getOption('slab_size'));
  122. for ($i = 0, $cnt = count($dataChunks); $i < $cnt; $i++) {
  123. $chunkId = $this->_getChunkId($id, $i);
  124. if (!parent::save($dataChunks[$i], $chunkId, $tags, $specificLifetime, $priority)) {
  125. $this->_cleanTheMess($id, $i + 1);
  126. return false;
  127. }
  128. }
  129. $data = self::CODE_WORD . '|' . $i;
  130. }
  131. return parent::save($data, $id, $tags, $specificLifetime, $priority);
  132. }
  133. /**
  134. * Load data from cached, glue from several chunks if it was splitted upon save.
  135. *
  136. * @param string $id Cache id
  137. * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
  138. * @param boolean $doNotUnserialize Do not serialize (even if automatic_serialization is true) => for internal use
  139. * @return mixed|false Cached datas
  140. */
  141. public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false)
  142. {
  143. $data = parent::load($id, $doNotTestCacheValidity, $doNotUnserialize);
  144. if (is_string($data) && (substr($data, 0, strlen(self::CODE_WORD)) == self::CODE_WORD)) {
  145. // Seems we've got chunked data
  146. $arr = explode('|', $data);
  147. $chunks = isset($arr[1]) ? $arr[1] : false;
  148. $chunkData = array();
  149. if ($chunks && is_numeric($chunks)) {
  150. for ($i = 0; $i < $chunks; $i++) {
  151. $chunk = parent::load($this->_getChunkId($id, $i), $doNotTestCacheValidity, $doNotUnserialize);
  152. if (false === $chunk) {
  153. // Some chunk in chain was not found, we can not glue-up the data:
  154. // clean the mess and return nothing
  155. $this->_cleanTheMess($id, $chunks);
  156. return false;
  157. }
  158. $chunkData[] = $chunk;
  159. }
  160. return implode('', $chunkData);
  161. }
  162. }
  163. // Data has not been splitted to chunks on save
  164. return $data;
  165. }
  166. /**
  167. * Clean cache entries
  168. *
  169. * Available modes are :
  170. * 'all' (default) => remove all cache entries ($tags is not used)
  171. * 'old' => remove too old cache entries ($tags is not used)
  172. * 'matchingTag' => remove cache entries matching all given tags
  173. * ($tags can be an array of strings or a single string)
  174. * 'notMatchingTag' => remove cache entries not matching one of the given tags
  175. * ($tags can be an array of strings or a single string)
  176. * 'matchingAnyTag' => remove cache entries matching any given tags
  177. * ($tags can be an array of strings or a single string)
  178. *
  179. * @param string $mode
  180. * @param array|string $tags
  181. * @throws Zend_Cache_Exception
  182. * @return boolean True if ok
  183. */
  184. public function clean($mode = 'all', $tags = array())
  185. {
  186. $tags = $this->_tags($tags);
  187. return parent::clean($mode, $tags);
  188. }
  189. /**
  190. * Return an array of stored cache ids which match given tags
  191. *
  192. * In case of multiple tags, a logical AND is made between tags
  193. *
  194. * @param array $tags array of tags
  195. * @return array array of matching cache ids (string)
  196. */
  197. public function getIdsMatchingTags($tags = array())
  198. {
  199. $tags = $this->_tags($tags);
  200. return parent::getIdsMatchingTags($tags);
  201. }
  202. /**
  203. * Return an array of stored cache ids which don't match given tags
  204. *
  205. * In case of multiple tags, a logical OR is made between tags
  206. *
  207. * @param array $tags array of tags
  208. * @return array array of not matching cache ids (string)
  209. */
  210. public function getIdsNotMatchingTags($tags = array())
  211. {
  212. $tags = $this->_tags($tags);
  213. return parent::getIdsNotMatchingTags($tags);
  214. }
  215. }