5 * Copyright (c) 2006 - 2014 PHPExcel
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.
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.
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
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 ##VERSION##, ##DATE##
30 * PHPExcel_CachedObjectStorage_Wincache
33 * @package PHPExcel_CachedObjectStorage
34 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
36 class PHPExcel_CachedObjectStorage_Wincache
extends PHPExcel_CachedObjectStorage_CacheBase
implements PHPExcel_CachedObjectStorage_ICache
{
39 * Prefix used to uniquely identify cache data for this worksheet
43 private $_cachePrefix = null;
50 private $_cacheTime = 600;
54 * Store cell data in cache for the current cell object if it's "dirty",
55 * and the 'nullify' the current cell object
58 * @throws PHPExcel_Exception
60 protected function _storeData() {
61 if ($this->_currentCellIsDirty
&& !empty($this->_currentObjectID
)) {
62 $this->_currentObject
->detach();
64 $obj = serialize($this->_currentObject
);
65 if (wincache_ucache_exists($this->_cachePrefix
.$this->_currentObjectID
.'.cache')) {
66 if (!wincache_ucache_set($this->_cachePrefix
.$this->_currentObjectID
.'.cache', $obj, $this->_cacheTime
)) {
68 throw new PHPExcel_Exception('Failed to store cell '.$this->_currentObjectID
.' in WinCache');
71 if (!wincache_ucache_add($this->_cachePrefix
.$this->_currentObjectID
.'.cache', $obj, $this->_cacheTime
)) {
73 throw new PHPExcel_Exception('Failed to store cell '.$this->_currentObjectID
.' in WinCache');
76 $this->_currentCellIsDirty
= false;
79 $this->_currentObjectID
= $this->_currentObject
= null;
80 } // function _storeData()
84 * Add or Update a cell in cache identified by coordinate address
86 * @param string $pCoord Coordinate address of the cell to update
87 * @param PHPExcel_Cell $cell Cell to update
88 * @return PHPExcel_Cell
89 * @throws PHPExcel_Exception
91 public function addCacheData($pCoord, PHPExcel_Cell
$cell) {
92 if (($pCoord !== $this->_currentObjectID
) && ($this->_currentObjectID
!== null)) {
95 $this->_cellCache
[$pCoord] = true;
97 $this->_currentObjectID
= $pCoord;
98 $this->_currentObject
= $cell;
99 $this->_currentCellIsDirty
= true;
102 } // function addCacheData()
106 * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
108 * @param string $pCoord Coordinate address of the cell to check
111 public function isDataSet($pCoord) {
112 // Check if the requested entry is the current object, or exists in the cache
113 if (parent
::isDataSet($pCoord)) {
114 if ($this->_currentObjectID
== $pCoord) {
117 // Check if the requested entry still exists in cache
118 $success = wincache_ucache_exists($this->_cachePrefix
.$pCoord.'.cache');
119 if ($success === false) {
120 // Entry no longer exists in Wincache, so clear it from the cache array
121 parent
::deleteCacheData($pCoord);
122 throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in WinCache');
127 } // function isDataSet()
131 * Get cell at a specific coordinate
133 * @param string $pCoord Coordinate of the cell
134 * @throws PHPExcel_Exception
135 * @return PHPExcel_Cell Cell that was found, or null if not found
137 public function getCacheData($pCoord) {
138 if ($pCoord === $this->_currentObjectID
) {
139 return $this->_currentObject
;
143 // Check if the entry that has been requested actually exists
145 if (parent
::isDataSet($pCoord)) {
147 $obj = wincache_ucache_get($this->_cachePrefix
.$pCoord.'.cache', $success);
148 if ($success === false) {
149 // Entry no longer exists in WinCache, so clear it from the cache array
150 parent
::deleteCacheData($pCoord);
151 throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in WinCache');
154 // Return null if requested entry doesn't exist in cache
158 // Set current entry to the requested entry
159 $this->_currentObjectID
= $pCoord;
160 $this->_currentObject
= unserialize($obj);
161 // Re-attach this as the cell's parent
162 $this->_currentObject
->attach($this);
164 // Return requested entry
165 return $this->_currentObject
;
166 } // function getCacheData()
170 * Get a list of all cell addresses currently held in cache
174 public function getCellList() {
175 if ($this->_currentObjectID
!== null) {
179 return parent
::getCellList();
184 * Delete a cell in cache identified by coordinate address
186 * @param string $pCoord Coordinate address of the cell to delete
187 * @throws PHPExcel_Exception
189 public function deleteCacheData($pCoord) {
190 // Delete the entry from Wincache
191 wincache_ucache_delete($this->_cachePrefix
.$pCoord.'.cache');
193 // Delete the entry from our cell address array
194 parent
::deleteCacheData($pCoord);
195 } // function deleteCacheData()
199 * Clone the cell collection
201 * @param PHPExcel_Worksheet $parent The new worksheet
204 public function copyCellCollection(PHPExcel_Worksheet
$parent) {
205 parent
::copyCellCollection($parent);
206 // Get a new id for the new file name
207 $baseUnique = $this->_getUniqueID();
208 $newCachePrefix = substr(md5($baseUnique),0,8).'.';
209 $cacheList = $this->getCellList();
210 foreach($cacheList as $cellID) {
211 if ($cellID != $this->_currentObjectID
) {
213 $obj = wincache_ucache_get($this->_cachePrefix
.$cellID.'.cache', $success);
214 if ($success === false) {
215 // Entry no longer exists in WinCache, so clear it from the cache array
216 parent
::deleteCacheData($cellID);
217 throw new PHPExcel_Exception('Cell entry '.$cellID.' no longer exists in Wincache');
219 if (!wincache_ucache_add($newCachePrefix.$cellID.'.cache', $obj, $this->_cacheTime
)) {
221 throw new PHPExcel_Exception('Failed to store cell '.$cellID.' in Wincache');
225 $this->_cachePrefix
= $newCachePrefix;
226 } // function copyCellCollection()
230 * Clear the cell collection and disconnect from our parent
234 public function unsetWorksheetCells() {
235 if(!is_null($this->_currentObject
)) {
236 $this->_currentObject
->detach();
237 $this->_currentObject
= $this->_currentObjectID
= null;
240 // Flush the WinCache cache
243 $this->_cellCache
= array();
245 // detach ourself from the worksheet, so that it can then delete this object successfully
246 $this->_parent
= null;
247 } // function unsetWorksheetCells()
251 * Initialise this new cell collection
253 * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
254 * @param array of mixed $arguments Additional initialisation arguments
256 public function __construct(PHPExcel_Worksheet
$parent, $arguments) {
257 $cacheTime = (isset($arguments['cacheTime'])) ?
$arguments['cacheTime'] : 600;
259 if (is_null($this->_cachePrefix
)) {
260 $baseUnique = $this->_getUniqueID();
261 $this->_cachePrefix
= substr(md5($baseUnique),0,8).'.';
262 $this->_cacheTime
= $cacheTime;
264 parent
::__construct($parent);
266 } // function __construct()
270 * Destroy this cell collection
272 public function __destruct() {
273 $cacheList = $this->getCellList();
274 foreach($cacheList as $cellID) {
275 wincache_ucache_delete($this->_cachePrefix
.$cellID.'.cache');
277 } // function __destruct()
281 * Identify whether the caching method is currently available
282 * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
286 public static function cacheMethodIsAvailable() {
287 if (!function_exists('wincache_ucache_add')) {