Wincache.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * PHPExcel_CachedObjectStorage_Wincache
  4. *
  5. * Copyright (c) 2006 - 2015 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_CachedObjectStorage
  23. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_CachedObjectStorage_Wincache extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
  28. {
  29. /**
  30. * Prefix used to uniquely identify cache data for this worksheet
  31. *
  32. * @var string
  33. */
  34. private $cachePrefix = null;
  35. /**
  36. * Cache timeout
  37. *
  38. * @var integer
  39. */
  40. private $cacheTime = 600;
  41. /**
  42. * Store cell data in cache for the current cell object if it's "dirty",
  43. * and the 'nullify' the current cell object
  44. *
  45. * @return void
  46. * @throws PHPExcel_Exception
  47. */
  48. protected function storeData()
  49. {
  50. if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
  51. $this->currentObject->detach();
  52. $obj = serialize($this->currentObject);
  53. if (wincache_ucache_exists($this->cachePrefix.$this->currentObjectID.'.cache')) {
  54. if (!wincache_ucache_set($this->cachePrefix.$this->currentObjectID.'.cache', $obj, $this->cacheTime)) {
  55. $this->__destruct();
  56. throw new PHPExcel_Exception('Failed to store cell '.$this->currentObjectID.' in WinCache');
  57. }
  58. } else {
  59. if (!wincache_ucache_add($this->cachePrefix.$this->currentObjectID.'.cache', $obj, $this->cacheTime)) {
  60. $this->__destruct();
  61. throw new PHPExcel_Exception('Failed to store cell '.$this->currentObjectID.' in WinCache');
  62. }
  63. }
  64. $this->currentCellIsDirty = false;
  65. }
  66. $this->currentObjectID = $this->currentObject = null;
  67. }
  68. /**
  69. * Add or Update a cell in cache identified by coordinate address
  70. *
  71. * @param string $pCoord Coordinate address of the cell to update
  72. * @param PHPExcel_Cell $cell Cell to update
  73. * @return PHPExcel_Cell
  74. * @throws PHPExcel_Exception
  75. */
  76. public function addCacheData($pCoord, PHPExcel_Cell $cell)
  77. {
  78. if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
  79. $this->storeData();
  80. }
  81. $this->cellCache[$pCoord] = true;
  82. $this->currentObjectID = $pCoord;
  83. $this->currentObject = $cell;
  84. $this->currentCellIsDirty = true;
  85. return $cell;
  86. }
  87. /**
  88. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  89. *
  90. * @param string $pCoord Coordinate address of the cell to check
  91. * @return boolean
  92. */
  93. public function isDataSet($pCoord)
  94. {
  95. // Check if the requested entry is the current object, or exists in the cache
  96. if (parent::isDataSet($pCoord)) {
  97. if ($this->currentObjectID == $pCoord) {
  98. return true;
  99. }
  100. // Check if the requested entry still exists in cache
  101. $success = wincache_ucache_exists($this->cachePrefix.$pCoord.'.cache');
  102. if ($success === false) {
  103. // Entry no longer exists in Wincache, so clear it from the cache array
  104. parent::deleteCacheData($pCoord);
  105. throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in WinCache');
  106. }
  107. return true;
  108. }
  109. return false;
  110. }
  111. /**
  112. * Get cell at a specific coordinate
  113. *
  114. * @param string $pCoord Coordinate of the cell
  115. * @throws PHPExcel_Exception
  116. * @return PHPExcel_Cell Cell that was found, or null if not found
  117. */
  118. public function getCacheData($pCoord)
  119. {
  120. if ($pCoord === $this->currentObjectID) {
  121. return $this->currentObject;
  122. }
  123. $this->storeData();
  124. // Check if the entry that has been requested actually exists
  125. $obj = null;
  126. if (parent::isDataSet($pCoord)) {
  127. $success = false;
  128. $obj = wincache_ucache_get($this->cachePrefix.$pCoord.'.cache', $success);
  129. if ($success === false) {
  130. // Entry no longer exists in WinCache, so clear it from the cache array
  131. parent::deleteCacheData($pCoord);
  132. throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in WinCache');
  133. }
  134. } else {
  135. // Return null if requested entry doesn't exist in cache
  136. return null;
  137. }
  138. // Set current entry to the requested entry
  139. $this->currentObjectID = $pCoord;
  140. $this->currentObject = unserialize($obj);
  141. // Re-attach this as the cell's parent
  142. $this->currentObject->attach($this);
  143. // Return requested entry
  144. return $this->currentObject;
  145. }
  146. /**
  147. * Get a list of all cell addresses currently held in cache
  148. *
  149. * @return string[]
  150. */
  151. public function getCellList()
  152. {
  153. if ($this->currentObjectID !== null) {
  154. $this->storeData();
  155. }
  156. return parent::getCellList();
  157. }
  158. /**
  159. * Delete a cell in cache identified by coordinate address
  160. *
  161. * @param string $pCoord Coordinate address of the cell to delete
  162. * @throws PHPExcel_Exception
  163. */
  164. public function deleteCacheData($pCoord)
  165. {
  166. // Delete the entry from Wincache
  167. wincache_ucache_delete($this->cachePrefix.$pCoord.'.cache');
  168. // Delete the entry from our cell address array
  169. parent::deleteCacheData($pCoord);
  170. }
  171. /**
  172. * Clone the cell collection
  173. *
  174. * @param PHPExcel_Worksheet $parent The new worksheet
  175. * @return void
  176. */
  177. public function copyCellCollection(PHPExcel_Worksheet $parent)
  178. {
  179. parent::copyCellCollection($parent);
  180. // Get a new id for the new file name
  181. $baseUnique = $this->getUniqueID();
  182. $newCachePrefix = substr(md5($baseUnique), 0, 8) . '.';
  183. $cacheList = $this->getCellList();
  184. foreach ($cacheList as $cellID) {
  185. if ($cellID != $this->currentObjectID) {
  186. $success = false;
  187. $obj = wincache_ucache_get($this->cachePrefix.$cellID.'.cache', $success);
  188. if ($success === false) {
  189. // Entry no longer exists in WinCache, so clear it from the cache array
  190. parent::deleteCacheData($cellID);
  191. throw new PHPExcel_Exception('Cell entry '.$cellID.' no longer exists in Wincache');
  192. }
  193. if (!wincache_ucache_add($newCachePrefix.$cellID.'.cache', $obj, $this->cacheTime)) {
  194. $this->__destruct();
  195. throw new PHPExcel_Exception('Failed to store cell '.$cellID.' in Wincache');
  196. }
  197. }
  198. }
  199. $this->cachePrefix = $newCachePrefix;
  200. }
  201. /**
  202. * Clear the cell collection and disconnect from our parent
  203. *
  204. * @return void
  205. */
  206. public function unsetWorksheetCells()
  207. {
  208. if (!is_null($this->currentObject)) {
  209. $this->currentObject->detach();
  210. $this->currentObject = $this->currentObjectID = null;
  211. }
  212. // Flush the WinCache cache
  213. $this->__destruct();
  214. $this->cellCache = array();
  215. // detach ourself from the worksheet, so that it can then delete this object successfully
  216. $this->parent = null;
  217. }
  218. /**
  219. * Initialise this new cell collection
  220. *
  221. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  222. * @param array of mixed $arguments Additional initialisation arguments
  223. */
  224. public function __construct(PHPExcel_Worksheet $parent, $arguments)
  225. {
  226. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
  227. if (is_null($this->cachePrefix)) {
  228. $baseUnique = $this->getUniqueID();
  229. $this->cachePrefix = substr(md5($baseUnique), 0, 8).'.';
  230. $this->cacheTime = $cacheTime;
  231. parent::__construct($parent);
  232. }
  233. }
  234. /**
  235. * Destroy this cell collection
  236. */
  237. public function __destruct()
  238. {
  239. $cacheList = $this->getCellList();
  240. foreach ($cacheList as $cellID) {
  241. wincache_ucache_delete($this->cachePrefix.$cellID.'.cache');
  242. }
  243. }
  244. /**
  245. * Identify whether the caching method is currently available
  246. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  247. *
  248. * @return boolean
  249. */
  250. public static function cacheMethodIsAvailable()
  251. {
  252. if (!function_exists('wincache_ucache_add')) {
  253. return false;
  254. }
  255. return true;
  256. }
  257. }