Wincache.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_CachedObjectStorage_Wincache extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
  8. {
  9. /**
  10. * Prefix used to uniquely identify cache data for this worksheet
  11. *
  12. * @var string
  13. */
  14. private $cachePrefix = null;
  15. /**
  16. * Cache timeout
  17. *
  18. * @var integer
  19. */
  20. private $cacheTime = 600;
  21. /**
  22. * Store cell data in cache for the current cell object if it's "dirty",
  23. * and the 'nullify' the current cell object
  24. *
  25. * @return void
  26. * @throws PHPExcel_Exception
  27. */
  28. protected function storeData()
  29. {
  30. if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
  31. $this->currentObject->detach();
  32. $obj = serialize($this->currentObject);
  33. if (wincache_ucache_exists($this->cachePrefix.$this->currentObjectID.'.cache')) {
  34. if (!wincache_ucache_set($this->cachePrefix.$this->currentObjectID.'.cache', $obj, $this->cacheTime)) {
  35. $this->__destruct();
  36. throw new PHPExcel_Exception('Failed to store cell '.$this->currentObjectID.' in WinCache');
  37. }
  38. } else {
  39. if (!wincache_ucache_add($this->cachePrefix.$this->currentObjectID.'.cache', $obj, $this->cacheTime)) {
  40. $this->__destruct();
  41. throw new PHPExcel_Exception('Failed to store cell '.$this->currentObjectID.' in WinCache');
  42. }
  43. }
  44. $this->currentCellIsDirty = false;
  45. }
  46. $this->currentObjectID = $this->currentObject = null;
  47. }
  48. /**
  49. * Add or Update a cell in cache identified by coordinate address
  50. *
  51. * @param string $pCoord Coordinate address of the cell to update
  52. * @param PHPExcel_Cell $cell Cell to update
  53. * @return PHPExcel_Cell
  54. * @throws PHPExcel_Exception
  55. */
  56. public function addCacheData($pCoord, PHPExcel_Cell $cell)
  57. {
  58. if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
  59. $this->storeData();
  60. }
  61. $this->cellCache[$pCoord] = true;
  62. $this->currentObjectID = $pCoord;
  63. $this->currentObject = $cell;
  64. $this->currentCellIsDirty = true;
  65. return $cell;
  66. }
  67. /**
  68. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  69. *
  70. * @param string $pCoord Coordinate address of the cell to check
  71. * @return boolean
  72. */
  73. public function isDataSet($pCoord)
  74. {
  75. // Check if the requested entry is the current object, or exists in the cache
  76. if (parent::isDataSet($pCoord)) {
  77. if ($this->currentObjectID == $pCoord) {
  78. return true;
  79. }
  80. // Check if the requested entry still exists in cache
  81. $success = wincache_ucache_exists($this->cachePrefix.$pCoord.'.cache');
  82. if ($success === false) {
  83. // Entry no longer exists in Wincache, so clear it from the cache array
  84. parent::deleteCacheData($pCoord);
  85. throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in WinCache');
  86. }
  87. return true;
  88. }
  89. return false;
  90. }
  91. /**
  92. * Get cell at a specific coordinate
  93. *
  94. * @param string $pCoord Coordinate of the cell
  95. * @throws PHPExcel_Exception
  96. * @return PHPExcel_Cell Cell that was found, or null if not found
  97. */
  98. public function getCacheData($pCoord)
  99. {
  100. if ($pCoord === $this->currentObjectID) {
  101. return $this->currentObject;
  102. }
  103. $this->storeData();
  104. // Check if the entry that has been requested actually exists
  105. $obj = null;
  106. if (parent::isDataSet($pCoord)) {
  107. $success = false;
  108. $obj = wincache_ucache_get($this->cachePrefix.$pCoord.'.cache', $success);
  109. if ($success === false) {
  110. // Entry no longer exists in WinCache, so clear it from the cache array
  111. parent::deleteCacheData($pCoord);
  112. throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in WinCache');
  113. }
  114. } else {
  115. // Return null if requested entry doesn't exist in cache
  116. return null;
  117. }
  118. // Set current entry to the requested entry
  119. $this->currentObjectID = $pCoord;
  120. $this->currentObject = unserialize($obj);
  121. // Re-attach this as the cell's parent
  122. $this->currentObject->attach($this);
  123. // Return requested entry
  124. return $this->currentObject;
  125. }
  126. /**
  127. * Get a list of all cell addresses currently held in cache
  128. *
  129. * @return string[]
  130. */
  131. public function getCellList()
  132. {
  133. if ($this->currentObjectID !== null) {
  134. $this->storeData();
  135. }
  136. return parent::getCellList();
  137. }
  138. /**
  139. * Delete a cell in cache identified by coordinate address
  140. *
  141. * @param string $pCoord Coordinate address of the cell to delete
  142. * @throws PHPExcel_Exception
  143. */
  144. public function deleteCacheData($pCoord)
  145. {
  146. // Delete the entry from Wincache
  147. wincache_ucache_delete($this->cachePrefix.$pCoord.'.cache');
  148. // Delete the entry from our cell address array
  149. parent::deleteCacheData($pCoord);
  150. }
  151. /**
  152. * Clone the cell collection
  153. *
  154. * @param PHPExcel_Worksheet $parent The new worksheet
  155. * @return void
  156. */
  157. public function copyCellCollection(PHPExcel_Worksheet $parent)
  158. {
  159. parent::copyCellCollection($parent);
  160. // Get a new id for the new file name
  161. $baseUnique = $this->getUniqueID();
  162. $newCachePrefix = substr(md5($baseUnique), 0, 8) . '.';
  163. $cacheList = $this->getCellList();
  164. foreach ($cacheList as $cellID) {
  165. if ($cellID != $this->currentObjectID) {
  166. $success = false;
  167. $obj = wincache_ucache_get($this->cachePrefix.$cellID.'.cache', $success);
  168. if ($success === false) {
  169. // Entry no longer exists in WinCache, so clear it from the cache array
  170. parent::deleteCacheData($cellID);
  171. throw new PHPExcel_Exception('Cell entry '.$cellID.' no longer exists in Wincache');
  172. }
  173. if (!wincache_ucache_add($newCachePrefix.$cellID.'.cache', $obj, $this->cacheTime)) {
  174. $this->__destruct();
  175. throw new PHPExcel_Exception('Failed to store cell '.$cellID.' in Wincache');
  176. }
  177. }
  178. }
  179. $this->cachePrefix = $newCachePrefix;
  180. }
  181. /**
  182. * Clear the cell collection and disconnect from our parent
  183. *
  184. * @return void
  185. */
  186. public function unsetWorksheetCells()
  187. {
  188. if (!is_null($this->currentObject)) {
  189. $this->currentObject->detach();
  190. $this->currentObject = $this->currentObjectID = null;
  191. }
  192. // Flush the WinCache cache
  193. $this->__destruct();
  194. $this->cellCache = array();
  195. // detach ourself from the worksheet, so that it can then delete this object successfully
  196. $this->parent = null;
  197. }
  198. /**
  199. * Initialise this new cell collection
  200. *
  201. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  202. * @param array of mixed $arguments Additional initialisation arguments
  203. */
  204. public function __construct(PHPExcel_Worksheet $parent, $arguments)
  205. {
  206. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
  207. if (is_null($this->cachePrefix)) {
  208. $baseUnique = $this->getUniqueID();
  209. $this->cachePrefix = substr(md5($baseUnique), 0, 8).'.';
  210. $this->cacheTime = $cacheTime;
  211. parent::__construct($parent);
  212. }
  213. }
  214. /**
  215. * Destroy this cell collection
  216. */
  217. public function __destruct()
  218. {
  219. $cacheList = $this->getCellList();
  220. foreach ($cacheList as $cellID) {
  221. wincache_ucache_delete($this->cachePrefix.$cellID.'.cache');
  222. }
  223. }
  224. /**
  225. * Identify whether the caching method is currently available
  226. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  227. *
  228. * @return boolean
  229. */
  230. public static function cacheMethodIsAvailable()
  231. {
  232. if (!function_exists('wincache_ucache_add')) {
  233. return false;
  234. }
  235. return true;
  236. }
  237. }