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