APC.php 8.6 KB

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