APC.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_APC
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
  35. private $_cachePrefix = null;
  36. private $_cacheTime = 600;
  37. private function _storeData() {
  38. $this->_currentObject->detach();
  39. if (!apc_store($this->_cachePrefix.$this->_currentObjectID.'.cache',serialize($this->_currentObject),$this->_cacheTime)) {
  40. $this->__destruct();
  41. throw new Exception('Failed to store cell '.$cellID.' in APC');
  42. }
  43. $this->_currentObjectID = $this->_currentObject = null;
  44. } // function _storeData()
  45. /**
  46. * Add or Update a cell in cache identified by coordinate address
  47. *
  48. * @param string $pCoord Coordinate address of the cell to update
  49. * @param PHPExcel_Cell $cell Cell to update
  50. * @return void
  51. * @throws Exception
  52. */
  53. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  54. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  55. $this->_storeData();
  56. }
  57. $this->_cellCache[$pCoord] = true;
  58. $this->_currentObjectID = $pCoord;
  59. $this->_currentObject = $cell;
  60. return $cell;
  61. } // function addCacheData()
  62. /**
  63. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  64. *
  65. * @param string $pCoord Coordinate address of the cell to check
  66. * @return void
  67. * @return boolean
  68. */
  69. public function isDataSet($pCoord) {
  70. // Check if the requested entry is the current object, or exists in the cache
  71. if (parent::isDataSet($pCoord)) {
  72. if ($this->_currentObjectID == $pCoord) {
  73. return true;
  74. }
  75. // Check if the requested entry still exists in apc
  76. $success = apc_fetch($this->_cachePrefix.$pCoord.'.cache');
  77. if ($success === false) {
  78. // Entry no longer exists in APC, so clear it from the cache array
  79. parent::deleteCacheData($pCoord);
  80. throw new Exception('Cell entry '.$cellID.' no longer exists in APC');
  81. }
  82. return true;
  83. }
  84. return false;
  85. } // function isDataSet()
  86. /**
  87. * Get cell at a specific coordinate
  88. *
  89. * @param string $pCoord Coordinate of the cell
  90. * @throws Exception
  91. * @return PHPExcel_Cell Cell that was found, or null if not found
  92. */
  93. public function getCacheData($pCoord) {
  94. if ($pCoord === $this->_currentObjectID) {
  95. return $this->_currentObject;
  96. }
  97. $this->_storeData();
  98. // Check if the entry that has been requested actually exists
  99. if (parent::isDataSet($pCoord)) {
  100. $obj = apc_fetch($this->_cachePrefix.$pCoord.'.cache');
  101. if ($obj === false) {
  102. // Entry no longer exists in APC, so clear it from the cache array
  103. parent::deleteCacheData($pCoord);
  104. throw new Exception('Cell entry '.$cellID.' no longer exists in APC');
  105. }
  106. } else {
  107. // Return null if requested entry doesn't exist in cache
  108. return null;
  109. }
  110. // Set current entry to the requested entry
  111. $this->_currentObjectID = $pCoord;
  112. $this->_currentObject = unserialize($obj);
  113. // Re-attach the parent worksheet
  114. $this->_currentObject->attach($this->_parent);
  115. // Return requested entry
  116. return $this->_currentObject;
  117. } // function getCacheData()
  118. /**
  119. * Delete a cell in cache identified by coordinate address
  120. *
  121. * @param string $pCoord Coordinate address of the cell to delete
  122. * @throws Exception
  123. */
  124. public function deleteCacheData($pCoord) {
  125. // Delete the entry from APC
  126. apc_delete($this->_cachePrefix.$pCoord.'.cache');
  127. // Delete the entry from our cell address array
  128. parent::deleteCacheData($pCoord);
  129. } // function deleteCacheData()
  130. /**
  131. * Clone the cell collection
  132. *
  133. * @return void
  134. */
  135. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  136. parent::copyCellCollection($parent);
  137. // Get a new id for the new file name
  138. $baseUnique = $this->_getUniqueID();
  139. $newCachePrefix = substr(md5($baseUnique),0,8).'.';
  140. $cacheList = $this->getCellList();
  141. foreach($cacheList as $cellID) {
  142. if ($cellID != $this->_currentObjectID) {
  143. $obj = apc_fetch($this->_cachePrefix.$cellID.'.cache');
  144. if ($obj === false) {
  145. // Entry no longer exists in APC, so clear it from the cache array
  146. parent::deleteCacheData($cellID);
  147. throw new Exception('Cell entry '.$cellID.' no longer exists in APC');
  148. }
  149. if (!apc_store($newCachePrefix.$cellID.'.cache',$obj,$this->_cacheTime)) {
  150. $this->__destruct();
  151. throw new Exception('Failed to store cell '.$cellID.' in APC');
  152. }
  153. }
  154. }
  155. $this->_cachePrefix = $newCachePrefix;
  156. } // function copyCellCollection()
  157. public function unsetWorksheetCells() {
  158. if(!is_null($this->_currentObject)) {
  159. $this->_currentObject->detach();
  160. $this->_currentObject = $this->_currentObjectID = null;
  161. }
  162. // Flush the APC cache
  163. $this->__destruct();
  164. $this->_cellCache = array();
  165. // detach ourself from the worksheet, so that it can then delete this object successfully
  166. $this->_parent = null;
  167. } // function unsetWorksheetCells()
  168. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
  169. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
  170. if (is_null($this->_cachePrefix)) {
  171. $baseUnique = $this->_getUniqueID();
  172. $this->_cachePrefix = substr(md5($baseUnique),0,8).'.';
  173. $this->_cacheTime = $cacheTime;
  174. parent::__construct($parent);
  175. }
  176. } // function __construct()
  177. public function __destruct() {
  178. $cacheList = $this->getCellList();
  179. foreach($cacheList as $cellID) {
  180. apc_delete($this->_cachePrefix.$cellID.'.cache');
  181. }
  182. } // function __destruct()
  183. }