APC.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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_APC
  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_APC extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
  35. /**
  36. * Prefix used to uniquely identify cache data for this worksheet
  37. *
  38. * @access private
  39. * @var string
  40. */
  41. private $_cachePrefix = null;
  42. /**
  43. * Cache timeout
  44. *
  45. * @access private
  46. * @var integer
  47. */
  48. private $_cacheTime = 600;
  49. /**
  50. * Store cell data in cache for the current cell object if it's "dirty",
  51. * and the 'nullify' the current cell object
  52. *
  53. * @access private
  54. * @return void
  55. * @throws PHPExcel_Exception
  56. */
  57. protected function _storeData() {
  58. if ($this->_currentCellIsDirty && !empty($this->_currentObjectID)) {
  59. $this->_currentObject->detach();
  60. if (!apc_store($this->_cachePrefix.$this->_currentObjectID.'.cache',serialize($this->_currentObject),$this->_cacheTime)) {
  61. $this->__destruct();
  62. throw new PHPExcel_Exception('Failed to store cell '.$this->_currentObjectID.' in APC');
  63. }
  64. $this->_currentCellIsDirty = false;
  65. }
  66. $this->_currentObjectID = $this->_currentObject = null;
  67. } // function _storeData()
  68. /**
  69. * Add or Update a cell in cache identified by coordinate address
  70. *
  71. * @access public
  72. * @param string $pCoord Coordinate address of the cell to update
  73. * @param PHPExcel_Cell $cell Cell to update
  74. * @return void
  75. * @throws PHPExcel_Exception
  76. */
  77. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  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. } // function addCacheData()
  87. /**
  88. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  89. *
  90. * @access public
  91. * @param string $pCoord Coordinate address of the cell to check
  92. * @return void
  93. * @return boolean
  94. */
  95. public function isDataSet($pCoord) {
  96. // Check if the requested entry is the current object, or exists in the cache
  97. if (parent::isDataSet($pCoord)) {
  98. if ($this->_currentObjectID == $pCoord) {
  99. return true;
  100. }
  101. // Check if the requested entry still exists in apc
  102. $success = apc_fetch($this->_cachePrefix.$pCoord.'.cache');
  103. if ($success === FALSE) {
  104. // Entry no longer exists in APC, so clear it from the cache array
  105. parent::deleteCacheData($pCoord);
  106. throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in APC cache');
  107. }
  108. return true;
  109. }
  110. return false;
  111. } // function isDataSet()
  112. /**
  113. * Get cell at a specific coordinate
  114. *
  115. * @access public
  116. * @param string $pCoord Coordinate of the cell
  117. * @throws PHPExcel_Exception
  118. * @return PHPExcel_Cell Cell that was found, or null if not found
  119. */
  120. public function getCacheData($pCoord) {
  121. if ($pCoord === $this->_currentObjectID) {
  122. return $this->_currentObject;
  123. }
  124. $this->_storeData();
  125. // Check if the entry that has been requested actually exists
  126. if (parent::isDataSet($pCoord)) {
  127. $obj = apc_fetch($this->_cachePrefix.$pCoord.'.cache');
  128. if ($obj === FALSE) {
  129. // Entry no longer exists in APC, so clear it from the cache array
  130. parent::deleteCacheData($pCoord);
  131. throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in APC cache');
  132. }
  133. } else {
  134. // Return null if requested entry doesn't exist in cache
  135. return null;
  136. }
  137. // Set current entry to the requested entry
  138. $this->_currentObjectID = $pCoord;
  139. $this->_currentObject = unserialize($obj);
  140. // Re-attach this as the cell's parent
  141. $this->_currentObject->attach($this);
  142. // Return requested entry
  143. return $this->_currentObject;
  144. } // function getCacheData()
  145. /**
  146. * Get a list of all cell addresses currently held in cache
  147. *
  148. * @return array of string
  149. */
  150. public function getCellList() {
  151. if ($this->_currentObjectID !== null) {
  152. $this->_storeData();
  153. }
  154. return parent::getCellList();
  155. }
  156. /**
  157. * Delete a cell in cache identified by coordinate address
  158. *
  159. * @access public
  160. * @param string $pCoord Coordinate address of the cell to delete
  161. * @throws PHPExcel_Exception
  162. */
  163. public function deleteCacheData($pCoord) {
  164. // Delete the entry from APC
  165. apc_delete($this->_cachePrefix.$pCoord.'.cache');
  166. // Delete the entry from our cell address array
  167. parent::deleteCacheData($pCoord);
  168. } // function deleteCacheData()
  169. /**
  170. * Clone the cell collection
  171. *
  172. * @access public
  173. * @param PHPExcel_Worksheet $parent The new worksheet
  174. * @throws PHPExcel_Exception
  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. $obj = apc_fetch($this->_cachePrefix.$cellID.'.cache');
  186. if ($obj === FALSE) {
  187. // Entry no longer exists in APC, so clear it from the cache array
  188. parent::deleteCacheData($cellID);
  189. throw new PHPExcel_Exception('Cell entry '.$cellID.' no longer exists in APC');
  190. }
  191. if (!apc_store($newCachePrefix.$cellID.'.cache',$obj,$this->_cacheTime)) {
  192. $this->__destruct();
  193. throw new PHPExcel_Exception('Failed to store cell '.$cellID.' in APC');
  194. }
  195. }
  196. }
  197. $this->_cachePrefix = $newCachePrefix;
  198. } // function copyCellCollection()
  199. /**
  200. * Clear the cell collection and disconnect from our parent
  201. *
  202. * @return void
  203. */
  204. public function unsetWorksheetCells() {
  205. if ($this->_currentObject !== NULL) {
  206. $this->_currentObject->detach();
  207. $this->_currentObject = $this->_currentObjectID = null;
  208. }
  209. // Flush the APC cache
  210. $this->__destruct();
  211. $this->_cellCache = array();
  212. // detach ourself from the worksheet, so that it can then delete this object successfully
  213. $this->_parent = null;
  214. } // function unsetWorksheetCells()
  215. /**
  216. * Initialise this new cell collection
  217. *
  218. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  219. * @param array of mixed $arguments Additional initialisation arguments
  220. */
  221. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
  222. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
  223. if ($this->_cachePrefix === NULL) {
  224. $baseUnique = $this->_getUniqueID();
  225. $this->_cachePrefix = substr(md5($baseUnique),0,8).'.';
  226. $this->_cacheTime = $cacheTime;
  227. parent::__construct($parent);
  228. }
  229. } // function __construct()
  230. /**
  231. * Destroy this cell collection
  232. */
  233. public function __destruct() {
  234. $cacheList = $this->getCellList();
  235. foreach($cacheList as $cellID) {
  236. apc_delete($this->_cachePrefix.$cellID.'.cache');
  237. }
  238. } // function __destruct()
  239. /**
  240. * Identify whether the caching method is currently available
  241. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  242. *
  243. * @return boolean
  244. */
  245. public static function cacheMethodIsAvailable() {
  246. if (!function_exists('apc_store')) {
  247. return FALSE;
  248. }
  249. if (apc_sma_info() === FALSE) {
  250. return FALSE;
  251. }
  252. return TRUE;
  253. }
  254. }