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 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.3c, 2010-06-01
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_CachedObjectStorage_APC
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_CachedObjectStorage
  34.  * @copyright  Copyright (c) 2006 - 2010 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 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 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 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.     public function unsetWorksheetCells({
  153.         if(!is_null($this->_currentObject)) {
  154.             $this->_currentObject->detach();
  155.             $this->_currentObject = $this->_currentObjectID = null;
  156.         }
  157.  
  158.         //    Flush the APC cache
  159.         $this->__destruct();
  160.  
  161.         $this->_cellCache = array();
  162.  
  163.         //    detach ourself from the worksheet, so that it can then delete this object successfully
  164.         $this->_parent = null;
  165.     }    //    function unsetWorksheetCells()
  166.  
  167.  
  168.     public function __construct(PHPExcel_Worksheet $parent$arguments{
  169.         $cacheTime    (isset($arguments['cacheTime']))    $arguments['cacheTime']    600;
  170.  
  171.         if (is_null($this->_cachePrefix)) {
  172.             if (function_exists('posix_getpid')) {
  173.                 $baseUnique posix_getpid();
  174.             else {
  175.                 $baseUnique mt_rand();
  176.             }
  177.             $this->_cachePrefix substr(md5(uniqid($baseUnique,true)),0,8).'.';
  178.             $this->_cacheTime $cacheTime;
  179.  
  180.             parent::__construct($parent);
  181.         }
  182.     }    //    function __construct()
  183.  
  184.  
  185.     public function __destruct({
  186.         $cacheList $this->getCellList();
  187.         foreach($cacheList as $cellID{
  188.             apc_delete($this->_cachePrefix.$cellID.'.cache');
  189.         }
  190.     }    //    function __destruct()
  191.  
  192. }

Documentation generated on Tue, 01 Jun 2010 17:00:42 +0200 by phpDocumentor 1.4.3