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

Source for file Memcache.php

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

Documentation generated on Sun, 27 Feb 2011 16:32:46 -0800 by phpDocumentor 1.4.3