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 - 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_Memcache
  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.     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 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 apc
  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 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 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.     public function unsetWorksheetCells({
  158.         if(!is_null($this->_currentObject)) {
  159.             $this->_currentObject->detach();
  160.             $this->_currentObject = $this->_currentObjectID = null;
  161.         }
  162.  
  163.         //    Flush the Memcache cache
  164.         $this->__destruct();
  165.  
  166.         $this->_cellCache = array();
  167.  
  168.         //    detach ourself from the worksheet, so that it can then delete this object successfully
  169.         $this->_parent = null;
  170.     }    //    function unsetWorksheetCells()
  171.  
  172.  
  173.     public function __construct(PHPExcel_Worksheet $parent$arguments{
  174.         $memcacheServer    (isset($arguments['memcacheServer']))    $arguments['memcacheServer']    'localhost';
  175.         $memcachePort    (isset($arguments['memcachePort']))    $arguments['memcachePort']    11211;
  176.         $cacheTime        (isset($arguments['cacheTime']))        $arguments['cacheTime']        600;
  177.  
  178.         if (is_null($this->_cachePrefix)) {
  179.             if (function_exists('posix_getpid')) {
  180.                 $baseUnique posix_getpid();
  181.             else {
  182.                 $baseUnique mt_rand();
  183.             }
  184.             $this->_cachePrefix substr(md5(uniqid($baseUnique,true)),0,8).'.';
  185.  
  186.             //    Set a new Memcache object and connect to the Memcache server
  187.             $this->_memcache new Memcache();
  188.             if (!$this->_memcache->addServer($memcacheServer$memcachePortfalse5055truearray($this'failureCallback')) {
  189.                 throw new Exception('Could not connect to Memcache server at '.$memcacheServer.':'.$memcachePort);
  190.             }
  191.             $this->_cacheTime $cacheTime;
  192.  
  193.             parent::__construct($parent);
  194.         }
  195.     }    //    function __construct()
  196.  
  197.  
  198.     public function failureCallback($host$port{
  199.         throw new Exception('memcache '.$host.':'.$port' failed');
  200.     }
  201.  
  202.  
  203.     public function __destruct({
  204.         $cacheList $this->getCellList();
  205.         foreach($cacheList as $cellID{
  206.             $this->_memcache->delete($this->_cachePrefix.$cellID.'.cache');
  207.         }
  208.     }    //    function __destruct()
  209.  
  210. }
  211.  
  212.  
  213. ?>

Documentation generated on Tue, 01 Jun 2010 17:05:13 +0200 by phpDocumentor 1.4.3