diff --git a/Classes/PHPExcel/CachedObjectStorage/APC.php b/Classes/PHPExcel/CachedObjectStorage/APC.php index f8e64587..f6d974fe 100644 --- a/Classes/PHPExcel/CachedObjectStorage/APC.php +++ b/Classes/PHPExcel/CachedObjectStorage/APC.php @@ -41,11 +41,14 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach private function _storeData() { - $this->_currentObject->detach(); + if ($this->_currentCellIsDirty) { + $this->_currentObject->detach(); - if (!apc_store($this->_cachePrefix.$this->_currentObjectID.'.cache',serialize($this->_currentObject),$this->_cacheTime)) { - $this->__destruct(); - throw new Exception('Failed to store cell '.$cellID.' in APC'); + if (!apc_store($this->_cachePrefix.$this->_currentObjectID.'.cache',serialize($this->_currentObject),$this->_cacheTime)) { + $this->__destruct(); + throw new Exception('Failed to store cell '.$cellID.' in APC'); + } + $this->_currentCellIsDirty = false; } $this->_currentObjectID = $this->_currentObject = null; } // function _storeData() @@ -67,6 +70,7 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach $this->_currentObjectID = $pCoord; $this->_currentObject = $cell; + $this->_currentCellIsDirty = true; return $cell; } // function addCacheData() diff --git a/Classes/PHPExcel/CachedObjectStorage/CacheBase.php b/Classes/PHPExcel/CachedObjectStorage/CacheBase.php index e3d14b5f..6119a348 100644 --- a/Classes/PHPExcel/CachedObjectStorage/CacheBase.php +++ b/Classes/PHPExcel/CachedObjectStorage/CacheBase.php @@ -57,6 +57,13 @@ class PHPExcel_CachedObjectStorage_CacheBase { protected $_currentObjectID = null; + /** + * Flag indicating whether the currently active Cell requires saving + * + * @var boolean + */ + protected $_currentCellIsDirty = true; + /** * An array of cells or cell pointers for the worksheet cells held in this cache, * and indexed by their coordinate address within the worksheet @@ -118,6 +125,7 @@ class PHPExcel_CachedObjectStorage_CacheBase { $this->_cellCache[$pCoord]->detach(); unset($this->_cellCache[$pCoord]); } + $this->_currentCellIsDirty = false; } // function deleteCacheData() diff --git a/Classes/PHPExcel/CachedObjectStorage/DiscISAM.php b/Classes/PHPExcel/CachedObjectStorage/DiscISAM.php index 327f4b4e..84ffcc95 100644 --- a/Classes/PHPExcel/CachedObjectStorage/DiscISAM.php +++ b/Classes/PHPExcel/CachedObjectStorage/DiscISAM.php @@ -40,14 +40,17 @@ class PHPExcel_CachedObjectStorage_DiscISAM extends PHPExcel_CachedObjectStorage private function _storeData() { - $this->_currentObject->detach(); + if ($this->_currentCellIsDirty) { + $this->_currentObject->detach(); - fseek($this->_fileHandle,0,SEEK_END); - $offset = ftell($this->_fileHandle); - fwrite($this->_fileHandle, serialize($this->_currentObject)); - $this->_cellCache[$this->_currentObjectID] = array('ptr' => $offset, - 'sz' => ftell($this->_fileHandle) - $offset - ); + fseek($this->_fileHandle,0,SEEK_END); + $offset = ftell($this->_fileHandle); + fwrite($this->_fileHandle, serialize($this->_currentObject)); + $this->_cellCache[$this->_currentObjectID] = array('ptr' => $offset, + 'sz' => ftell($this->_fileHandle) - $offset + ); + $this->_currentCellIsDirty = false; + } $this->_currentObjectID = $this->_currentObject = null; } // function _storeData() @@ -67,6 +70,7 @@ class PHPExcel_CachedObjectStorage_DiscISAM extends PHPExcel_CachedObjectStorage $this->_currentObjectID = $pCoord; $this->_currentObject = $cell; + $this->_currentCellIsDirty = true; return $cell; } // function addCacheData() diff --git a/Classes/PHPExcel/CachedObjectStorage/Memcache.php b/Classes/PHPExcel/CachedObjectStorage/Memcache.php index e40413ed..28b74e39 100644 --- a/Classes/PHPExcel/CachedObjectStorage/Memcache.php +++ b/Classes/PHPExcel/CachedObjectStorage/Memcache.php @@ -43,14 +43,17 @@ class PHPExcel_CachedObjectStorage_Memcache extends PHPExcel_CachedObjectStorage private function _storeData() { - $this->_currentObject->detach(); + if ($this->_currentCellIsDirty) { + $this->_currentObject->detach(); - $obj = serialize($this->_currentObject); - if (!$this->_memcache->replace($this->_cachePrefix.$this->_currentObjectID.'.cache',$obj,NULL,$this->_cacheTime)) { - if (!$this->_memcache->add($this->_cachePrefix.$this->_currentObjectID.'.cache',$obj,NULL,$this->_cacheTime)) { - $this->__destruct(); - throw new Exception('Failed to store cell '.$cellID.' in MemCache'); + $obj = serialize($this->_currentObject); + if (!$this->_memcache->replace($this->_cachePrefix.$this->_currentObjectID.'.cache',$obj,NULL,$this->_cacheTime)) { + if (!$this->_memcache->add($this->_cachePrefix.$this->_currentObjectID.'.cache',$obj,NULL,$this->_cacheTime)) { + $this->__destruct(); + throw new Exception('Failed to store cell '.$cellID.' in MemCache'); + } } + $this->_currentCellIsDirty = false; } $this->_currentObjectID = $this->_currentObject = null; } // function _storeData() @@ -72,6 +75,7 @@ class PHPExcel_CachedObjectStorage_Memcache extends PHPExcel_CachedObjectStorage $this->_currentObjectID = $pCoord; $this->_currentObject = $cell; + $this->_currentCellIsDirty = true; return $cell; } // function addCacheData() diff --git a/Classes/PHPExcel/CachedObjectStorage/MemoryGZip.php b/Classes/PHPExcel/CachedObjectStorage/MemoryGZip.php index dfe359ef..4fb90adb 100644 --- a/Classes/PHPExcel/CachedObjectStorage/MemoryGZip.php +++ b/Classes/PHPExcel/CachedObjectStorage/MemoryGZip.php @@ -36,9 +36,12 @@ class PHPExcel_CachedObjectStorage_MemoryGZip extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache { private function _storeData() { - $this->_currentObject->detach(); + if ($this->_currentCellIsDirty) { + $this->_currentObject->detach(); - $this->_cellCache[$this->_currentObjectID] = gzdeflate(serialize($this->_currentObject)); + $this->_cellCache[$this->_currentObjectID] = gzdeflate(serialize($this->_currentObject)); + $this->_currentCellIsDirty = false; + } $this->_currentObjectID = $this->_currentObject = null; } // function _storeData() @@ -58,6 +61,7 @@ class PHPExcel_CachedObjectStorage_MemoryGZip extends PHPExcel_CachedObjectStora $this->_currentObjectID = $pCoord; $this->_currentObject = $cell; + $this->_currentCellIsDirty = true; return $cell; } // function addCacheData() diff --git a/Classes/PHPExcel/CachedObjectStorage/MemorySerialized.php b/Classes/PHPExcel/CachedObjectStorage/MemorySerialized.php index ca8f0317..14abd793 100644 --- a/Classes/PHPExcel/CachedObjectStorage/MemorySerialized.php +++ b/Classes/PHPExcel/CachedObjectStorage/MemorySerialized.php @@ -36,9 +36,12 @@ class PHPExcel_CachedObjectStorage_MemorySerialized extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache { private function _storeData() { - $this->_currentObject->detach(); + if ($this->_currentCellIsDirty) { + $this->_currentObject->detach(); - $this->_cellCache[$this->_currentObjectID] = serialize($this->_currentObject); + $this->_cellCache[$this->_currentObjectID] = serialize($this->_currentObject); + $this->_currentCellIsDirty = false; + } $this->_currentObjectID = $this->_currentObject = null; } // function _storeData() @@ -58,6 +61,7 @@ class PHPExcel_CachedObjectStorage_MemorySerialized extends PHPExcel_CachedObjec $this->_currentObjectID = $pCoord; $this->_currentObject = $cell; + $this->_currentCellIsDirty = true; return $cell; } // function addCacheData() diff --git a/Classes/PHPExcel/CachedObjectStorage/PHPTemp.php b/Classes/PHPExcel/CachedObjectStorage/PHPTemp.php index 07caa85c..2919aa08 100644 --- a/Classes/PHPExcel/CachedObjectStorage/PHPTemp.php +++ b/Classes/PHPExcel/CachedObjectStorage/PHPTemp.php @@ -41,14 +41,17 @@ class PHPExcel_CachedObjectStorage_PHPTemp extends PHPExcel_CachedObjectStorage_ private $_memoryCacheSize = null; private function _storeData() { - $this->_currentObject->detach(); + if ($this->_currentCellIsDirty) { + $this->_currentObject->detach(); - fseek($this->_fileHandle,0,SEEK_END); - $offset = ftell($this->_fileHandle); - fwrite($this->_fileHandle, serialize($this->_currentObject)); - $this->_cellCache[$this->_currentObjectID] = array('ptr' => $offset, - 'sz' => ftell($this->_fileHandle) - $offset - ); + fseek($this->_fileHandle,0,SEEK_END); + $offset = ftell($this->_fileHandle); + fwrite($this->_fileHandle, serialize($this->_currentObject)); + $this->_cellCache[$this->_currentObjectID] = array('ptr' => $offset, + 'sz' => ftell($this->_fileHandle) - $offset + ); + $this->_currentCellIsDirty = false; + } $this->_currentObjectID = $this->_currentObject = null; } // function _storeData() @@ -68,6 +71,7 @@ class PHPExcel_CachedObjectStorage_PHPTemp extends PHPExcel_CachedObjectStorage_ $this->_currentObjectID = $pCoord; $this->_currentObject = $cell; + $this->_currentCellIsDirty = true; return $cell; } // function addCacheData() diff --git a/Classes/PHPExcel/CachedObjectStorage/Wincache.php b/Classes/PHPExcel/CachedObjectStorage/Wincache.php index 6c2cfd29..14435b13 100644 --- a/Classes/PHPExcel/CachedObjectStorage/Wincache.php +++ b/Classes/PHPExcel/CachedObjectStorage/Wincache.php @@ -41,19 +41,22 @@ class PHPExcel_CachedObjectStorage_Wincache extends PHPExcel_CachedObjectStorage private function _storeData() { - $this->_currentObject->detach(); + if ($this->_currentCellIsDirty) { + $this->_currentObject->detach(); - $obj = serialize($this->_currentObject); - if (wincache_ucache_exists($this->_cachePrefix.$this->_currentObjectID.'.cache')) { - if (!wincache_ucache_set($this->_cachePrefix.$this->_currentObjectID.'.cache', $obj, $this->_cacheTime)) { - $this->__destruct(); - throw new Exception('Failed to store cell '.$cellID.' in WinCache'); - } - } else { - if (!wincache_ucache_add($this->_cachePrefix.$this->_currentObjectID.'.cache', $obj, $this->_cacheTime)) { - $this->__destruct(); - throw new Exception('Failed to store cell '.$cellID.' in WinCache'); + $obj = serialize($this->_currentObject); + if (wincache_ucache_exists($this->_cachePrefix.$this->_currentObjectID.'.cache')) { + if (!wincache_ucache_set($this->_cachePrefix.$this->_currentObjectID.'.cache', $obj, $this->_cacheTime)) { + $this->__destruct(); + throw new Exception('Failed to store cell '.$cellID.' in WinCache'); + } + } else { + if (!wincache_ucache_add($this->_cachePrefix.$this->_currentObjectID.'.cache', $obj, $this->_cacheTime)) { + $this->__destruct(); + throw new Exception('Failed to store cell '.$cellID.' in WinCache'); + } } + $this->_currentCellIsDirty = false; } $this->_currentObjectID = $this->_currentObject = null; @@ -76,6 +79,7 @@ class PHPExcel_CachedObjectStorage_Wincache extends PHPExcel_CachedObjectStorage $this->_currentObjectID = $pCoord; $this->_currentObject = $cell; + $this->_currentCellIsDirty = true; return $cell; } // function addCacheData() diff --git a/changelog.txt b/changelog.txt index fbca91b4..ad48976f 100644 --- a/changelog.txt +++ b/changelog.txt @@ -25,6 +25,7 @@ Fixed in SVN: - General: (MBaker) Fix to build to ensure that Examples are included with the documentation +- General: (MBaker) Reduce cell caching overhead using dirty flag to ensure that cells are only rewritten to the cache if they have actually been changed - Bugfix: (MBaker) Work item 15459 - Invalid cell coordinate in Autofilter for Excel2007 Writer - Bugfix: (MBaker) Work item 15518 - PCLZip library issue - Bugfix: (MBaker) Work item 15537 - Excel2007 Reader canRead function bug