Memcache.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_CachedObjectStorage_Memcache 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. * Memcache interface
  23. *
  24. * @var resource
  25. */
  26. private $memcache = null;
  27. /**
  28. * Store cell data in cache for the current cell object if it's "dirty",
  29. * and the 'nullify' the current cell object
  30. *
  31. * @return void
  32. * @throws PHPExcel_Exception
  33. */
  34. protected function storeData()
  35. {
  36. if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
  37. $this->currentObject->detach();
  38. $obj = serialize($this->currentObject);
  39. if (!$this->memcache->replace($this->cachePrefix . $this->currentObjectID . '.cache', $obj, null, $this->cacheTime)) {
  40. if (!$this->memcache->add($this->cachePrefix . $this->currentObjectID . '.cache', $obj, null, $this->cacheTime)) {
  41. $this->__destruct();
  42. throw new PHPExcel_Exception("Failed to store cell {$this->currentObjectID} in MemCache");
  43. }
  44. }
  45. $this->currentCellIsDirty = false;
  46. }
  47. $this->currentObjectID = $this->currentObject = null;
  48. } // function _storeData()
  49. /**
  50. * Add or Update a cell in cache identified by coordinate address
  51. *
  52. * @param string $pCoord Coordinate address of the cell to update
  53. * @param PHPExcel_Cell $cell Cell to update
  54. * @return PHPExcel_Cell
  55. * @throws PHPExcel_Exception
  56. */
  57. public function addCacheData($pCoord, PHPExcel_Cell $cell)
  58. {
  59. if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
  60. $this->storeData();
  61. }
  62. $this->cellCache[$pCoord] = true;
  63. $this->currentObjectID = $pCoord;
  64. $this->currentObject = $cell;
  65. $this->currentCellIsDirty = true;
  66. return $cell;
  67. } // function addCacheData()
  68. /**
  69. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  70. *
  71. * @param string $pCoord Coordinate address of the cell to check
  72. * @return boolean
  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 Memcache
  83. $success = $this->memcache->get($this->cachePrefix.$pCoord.'.cache');
  84. if ($success === false) {
  85. // Entry no longer exists in Memcache, so clear it from the cache array
  86. parent::deleteCacheData($pCoord);
  87. throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in MemCache');
  88. }
  89. return true;
  90. }
  91. return false;
  92. }
  93. /**
  94. * Get cell at a specific coordinate
  95. *
  96. * @param string $pCoord Coordinate of the cell
  97. * @throws PHPExcel_Exception
  98. * @return PHPExcel_Cell Cell that was found, or null if not found
  99. */
  100. public function getCacheData($pCoord)
  101. {
  102. if ($pCoord === $this->currentObjectID) {
  103. return $this->currentObject;
  104. }
  105. $this->storeData();
  106. // Check if the entry that has been requested actually exists
  107. if (parent::isDataSet($pCoord)) {
  108. $obj = $this->memcache->get($this->cachePrefix . $pCoord . '.cache');
  109. if ($obj === false) {
  110. // Entry no longer exists in Memcache, so clear it from the cache array
  111. parent::deleteCacheData($pCoord);
  112. throw new PHPExcel_Exception("Cell entry {$pCoord} no longer exists in MemCache");
  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 Memcache
  147. $this->memcache->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. $obj = $this->memcache->get($this->cachePrefix.$cellID.'.cache');
  167. if ($obj === false) {
  168. // Entry no longer exists in Memcache, so clear it from the cache array
  169. parent::deleteCacheData($cellID);
  170. throw new PHPExcel_Exception("Cell entry {$cellID} no longer exists in MemCache");
  171. }
  172. if (!$this->memcache->add($newCachePrefix . $cellID . '.cache', $obj, null, $this->cacheTime)) {
  173. $this->__destruct();
  174. throw new PHPExcel_Exception("Failed to store cell {$cellID} in MemCache");
  175. }
  176. }
  177. }
  178. $this->cachePrefix = $newCachePrefix;
  179. }
  180. /**
  181. * Clear the cell collection and disconnect from our parent
  182. *
  183. * @return void
  184. */
  185. public function unsetWorksheetCells()
  186. {
  187. if (!is_null($this->currentObject)) {
  188. $this->currentObject->detach();
  189. $this->currentObject = $this->currentObjectID = null;
  190. }
  191. // Flush the Memcache cache
  192. $this->__destruct();
  193. $this->cellCache = array();
  194. // detach ourself from the worksheet, so that it can then delete this object successfully
  195. $this->parent = null;
  196. }
  197. /**
  198. * Initialise this new cell collection
  199. *
  200. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  201. * @param array of mixed $arguments Additional initialisation arguments
  202. */
  203. public function __construct(PHPExcel_Worksheet $parent, $arguments)
  204. {
  205. $memcacheServer = (isset($arguments['memcacheServer'])) ? $arguments['memcacheServer'] : 'localhost';
  206. $memcachePort = (isset($arguments['memcachePort'])) ? $arguments['memcachePort'] : 11211;
  207. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
  208. if (is_null($this->cachePrefix)) {
  209. $baseUnique = $this->getUniqueID();
  210. $this->cachePrefix = substr(md5($baseUnique), 0, 8) . '.';
  211. // Set a new Memcache object and connect to the Memcache server
  212. $this->memcache = new Memcache();
  213. if (!$this->memcache->addServer($memcacheServer, $memcachePort, false, 50, 5, 5, true, array($this, 'failureCallback'))) {
  214. throw new PHPExcel_Exception("Could not connect to MemCache server at {$memcacheServer}:{$memcachePort}");
  215. }
  216. $this->cacheTime = $cacheTime;
  217. parent::__construct($parent);
  218. }
  219. }
  220. /**
  221. * Memcache error handler
  222. *
  223. * @param string $host Memcache server
  224. * @param integer $port Memcache port
  225. * @throws PHPExcel_Exception
  226. */
  227. public function failureCallback($host, $port)
  228. {
  229. throw new PHPExcel_Exception("memcache {$host}:{$port} failed");
  230. }
  231. /**
  232. * Destroy this cell collection
  233. */
  234. public function __destruct()
  235. {
  236. $cacheList = $this->getCellList();
  237. foreach ($cacheList as $cellID) {
  238. $this->memcache->delete($this->cachePrefix.$cellID . '.cache');
  239. }
  240. }
  241. /**
  242. * Identify whether the caching method is currently available
  243. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  244. *
  245. * @return boolean
  246. */
  247. public static function cacheMethodIsAvailable()
  248. {
  249. if (!function_exists('memcache_add')) {
  250. return false;
  251. }
  252. return true;
  253. }
  254. }