PHPExcel_CachedObjectStorage
[ class tree: PHPExcel_CachedObjectStorage ] [ index: PHPExcel_CachedObjectStorage ] [ all elements ]

Source for file Wincache.php

Documentation is available at Wincache.php

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

Documentation generated on Sun, 27 Feb 2011 16:37:01 -0800 by phpDocumentor 1.4.3