Wincache.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_Wincache
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_CachedObjectStorage_Wincache extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
  35. /**
  36. * Prefix used to uniquely identify cache data for this worksheet
  37. *
  38. * @var string
  39. */
  40. private $_cachePrefix = null;
  41. /**
  42. * Cache timeout
  43. *
  44. * @var integer
  45. */
  46. private $_cacheTime = 600;
  47. /**
  48. * Store cell data in cache for the current cell object if it's "dirty",
  49. * and the 'nullify' the current cell object
  50. *
  51. * @return void
  52. * @throws PHPExcel_Exception
  53. */
  54. protected function _storeData() {
  55. if ($this->_currentCellIsDirty && !empty($this->_currentObjectID)) {
  56. $this->_currentObject->detach();
  57. $obj = serialize($this->_currentObject);
  58. if (wincache_ucache_exists($this->_cachePrefix.$this->_currentObjectID.'.cache')) {
  59. if (!wincache_ucache_set($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. } else {
  64. if (!wincache_ucache_add($this->_cachePrefix.$this->_currentObjectID.'.cache', $obj, $this->_cacheTime)) {
  65. $this->__destruct();
  66. throw new PHPExcel_Exception('Failed to store cell '.$this->_currentObjectID.' in WinCache');
  67. }
  68. }
  69. $this->_currentCellIsDirty = false;
  70. }
  71. $this->_currentObjectID = $this->_currentObject = null;
  72. } // function _storeData()
  73. /**
  74. * Add or Update a cell in cache identified by coordinate address
  75. *
  76. * @param string $pCoord Coordinate address of the cell to update
  77. * @param PHPExcel_Cell $cell Cell to update
  78. * @return void
  79. * @throws PHPExcel_Exception
  80. */
  81. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  82. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  83. $this->_storeData();
  84. }
  85. $this->_cellCache[$pCoord] = true;
  86. $this->_currentObjectID = $pCoord;
  87. $this->_currentObject = $cell;
  88. $this->_currentCellIsDirty = true;
  89. return $cell;
  90. } // function addCacheData()
  91. /**
  92. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  93. *
  94. * @param string $pCoord Coordinate address of the cell to check
  95. * @return boolean
  96. */
  97. public function isDataSet($pCoord) {
  98. // Check if the requested entry is the current object, or exists in the cache
  99. if (parent::isDataSet($pCoord)) {
  100. if ($this->_currentObjectID == $pCoord) {
  101. return true;
  102. }
  103. // Check if the requested entry still exists in cache
  104. $success = wincache_ucache_exists($this->_cachePrefix.$pCoord.'.cache');
  105. if ($success === false) {
  106. // Entry no longer exists in Wincache, so clear it from the cache array
  107. parent::deleteCacheData($pCoord);
  108. throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in WinCache');
  109. }
  110. return true;
  111. }
  112. return false;
  113. } // function isDataSet()
  114. /**
  115. * Get cell at a specific coordinate
  116. *
  117. * @param string $pCoord Coordinate of the cell
  118. * @throws PHPExcel_Exception
  119. * @return PHPExcel_Cell Cell that was found, or null if not found
  120. */
  121. public function getCacheData($pCoord) {
  122. if ($pCoord === $this->_currentObjectID) {
  123. return $this->_currentObject;
  124. }
  125. $this->_storeData();
  126. // Check if the entry that has been requested actually exists
  127. $obj = null;
  128. if (parent::isDataSet($pCoord)) {
  129. $success = false;
  130. $obj = wincache_ucache_get($this->_cachePrefix.$pCoord.'.cache', $success);
  131. if ($success === false) {
  132. // Entry no longer exists in WinCache, so clear it from the cache array
  133. parent::deleteCacheData($pCoord);
  134. throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in WinCache');
  135. }
  136. } else {
  137. // Return null if requested entry doesn't exist in cache
  138. return null;
  139. }
  140. // Set current entry to the requested entry
  141. $this->_currentObjectID = $pCoord;
  142. $this->_currentObject = unserialize($obj);
  143. // Re-attach this as the cell's parent
  144. $this->_currentObject->attach($this);
  145. // Return requested entry
  146. return $this->_currentObject;
  147. } // function getCacheData()
  148. /**
  149. * Get a list of all cell addresses currently held in cache
  150. *
  151. * @return array of string
  152. */
  153. public function getCellList() {
  154. if ($this->_currentObjectID !== null) {
  155. $this->_storeData();
  156. }
  157. return parent::getCellList();
  158. }
  159. /**
  160. * Delete a cell in cache identified by coordinate address
  161. *
  162. * @param string $pCoord Coordinate address of the cell to delete
  163. * @throws PHPExcel_Exception
  164. */
  165. public function deleteCacheData($pCoord) {
  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. } // function deleteCacheData()
  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. parent::copyCellCollection($parent);
  179. // Get a new id for the new file name
  180. $baseUnique = $this->_getUniqueID();
  181. $newCachePrefix = substr(md5($baseUnique),0,8).'.';
  182. $cacheList = $this->getCellList();
  183. foreach($cacheList as $cellID) {
  184. if ($cellID != $this->_currentObjectID) {
  185. $success = false;
  186. $obj = wincache_ucache_get($this->_cachePrefix.$cellID.'.cache', $success);
  187. if ($success === false) {
  188. // Entry no longer exists in WinCache, so clear it from the cache array
  189. parent::deleteCacheData($cellID);
  190. throw new PHPExcel_Exception('Cell entry '.$cellID.' no longer exists in Wincache');
  191. }
  192. if (!wincache_ucache_add($newCachePrefix.$cellID.'.cache', $obj, $this->_cacheTime)) {
  193. $this->__destruct();
  194. throw new PHPExcel_Exception('Failed to store cell '.$cellID.' in Wincache');
  195. }
  196. }
  197. }
  198. $this->_cachePrefix = $newCachePrefix;
  199. } // function copyCellCollection()
  200. /**
  201. * Clear the cell collection and disconnect from our parent
  202. *
  203. * @return void
  204. */
  205. public function unsetWorksheetCells() {
  206. if(!is_null($this->_currentObject)) {
  207. $this->_currentObject->detach();
  208. $this->_currentObject = $this->_currentObjectID = null;
  209. }
  210. // Flush the WinCache cache
  211. $this->__destruct();
  212. $this->_cellCache = array();
  213. // detach ourself from the worksheet, so that it can then delete this object successfully
  214. $this->_parent = null;
  215. } // function unsetWorksheetCells()
  216. /**
  217. * Initialise this new cell collection
  218. *
  219. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  220. * @param array of mixed $arguments Additional initialisation arguments
  221. */
  222. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
  223. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
  224. if (is_null($this->_cachePrefix)) {
  225. $baseUnique = $this->_getUniqueID();
  226. $this->_cachePrefix = substr(md5($baseUnique),0,8).'.';
  227. $this->_cacheTime = $cacheTime;
  228. parent::__construct($parent);
  229. }
  230. } // function __construct()
  231. /**
  232. * Destroy this cell collection
  233. */
  234. public function __destruct() {
  235. $cacheList = $this->getCellList();
  236. foreach($cacheList as $cellID) {
  237. wincache_ucache_delete($this->_cachePrefix.$cellID.'.cache');
  238. }
  239. } // function __destruct()
  240. /**
  241. * Identify whether the caching method is currently available
  242. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  243. *
  244. * @return boolean
  245. */
  246. public static function cacheMethodIsAvailable() {
  247. if (!function_exists('wincache_ucache_add')) {
  248. return false;
  249. }
  250. return true;
  251. }
  252. }