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

Source for file APC.php

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

Documentation generated on Sun, 27 Feb 2011 16:26:55 -0800 by phpDocumentor 1.4.3