[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/CachedObjectStorage/ -> Wincache.php (source)

   1  <?php
   2  
   3  /**
   4   * PHPExcel_CachedObjectStorage_Wincache
   5   *
   6   * Copyright (c) 2006 - 2015 PHPExcel
   7   *
   8   * This library is free software; you can redistribute it and/or
   9   * modify it under the terms of the GNU Lesser General Public
  10   * License as published by the Free Software Foundation; either
  11   * version 2.1 of the License, or (at your option) any later version.
  12   *
  13   * This library is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16   * Lesser General Public License for more details.
  17   *
  18   * You should have received a copy of the GNU Lesser General Public
  19   * License along with this library; if not, write to the Free Software
  20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  21   *
  22   * @category   PHPExcel
  23   * @package    PHPExcel_CachedObjectStorage
  24   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  25   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26   * @version    ##VERSION##, ##DATE##
  27   */
  28  class PHPExcel_CachedObjectStorage_Wincache extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
  29  {
  30      /**
  31       * Prefix used to uniquely identify cache data for this worksheet
  32       *
  33       * @var string
  34       */
  35      private $cachePrefix = null;
  36  
  37      /**
  38       * Cache timeout
  39       *
  40       * @var integer
  41       */
  42      private $cacheTime = 600;
  43  
  44  
  45      /**
  46       * Store cell data in cache for the current cell object if it's "dirty",
  47       *     and the 'nullify' the current cell object
  48       *
  49       * @return    void
  50       * @throws    PHPExcel_Exception
  51       */
  52      protected function storeData()
  53      {
  54          if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
  55              $this->currentObject->detach();
  56  
  57              $obj = serialize($this->currentObject);
  58              if (wincache_ucache_exists($this->cachePrefix.$this->currentObjectID.'.cache')) {
  59                  if (!wincache_ucache_set($this->cachePrefix.$this->currentObjectID.'.cache', $obj, $this->cacheTime)) {
  60                      $this->__destruct();
  61                      throw new PHPExcel_Exception('Failed to store cell '.$this->currentObjectID.' in WinCache');
  62                  }
  63              } else {
  64                  if (!wincache_ucache_add($this->cachePrefix.$this->currentObjectID.'.cache', $obj, $this->cacheTime)) {
  65                      $this->__destruct();
  66                      throw new PHPExcel_Exception('Failed to store cell '.$this->currentObjectID.' in WinCache');
  67                  }
  68              }
  69              $this->currentCellIsDirty = false;
  70          }
  71  
  72          $this->currentObjectID = $this->currentObject = null;
  73      }
  74  
  75      /**
  76       * Add or Update a cell in cache identified by coordinate address
  77       *
  78       * @param    string            $pCoord        Coordinate address of the cell to update
  79       * @param    PHPExcel_Cell    $cell        Cell to update
  80       * @return    PHPExcel_Cell
  81       * @throws    PHPExcel_Exception
  82       */
  83      public function addCacheData($pCoord, PHPExcel_Cell $cell)
  84      {
  85          if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
  86              $this->storeData();
  87          }
  88          $this->cellCache[$pCoord] = true;
  89  
  90          $this->currentObjectID = $pCoord;
  91          $this->currentObject = $cell;
  92          $this->currentCellIsDirty = true;
  93  
  94          return $cell;
  95      }
  96  
  97      /**
  98       * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  99       *
 100       * @param    string        $pCoord        Coordinate address of the cell to check
 101       * @return    boolean
 102       */
 103      public function isDataSet($pCoord)
 104      {
 105          //    Check if the requested entry is the current object, or exists in the cache
 106          if (parent::isDataSet($pCoord)) {
 107              if ($this->currentObjectID == $pCoord) {
 108                  return true;
 109              }
 110              //    Check if the requested entry still exists in cache
 111              $success = wincache_ucache_exists($this->cachePrefix.$pCoord.'.cache');
 112              if ($success === false) {
 113                  //    Entry no longer exists in Wincache, so clear it from the cache array
 114                  parent::deleteCacheData($pCoord);
 115                  throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in WinCache');
 116              }
 117              return true;
 118          }
 119          return false;
 120      }
 121  
 122  
 123      /**
 124       * Get cell at a specific coordinate
 125       *
 126       * @param    string            $pCoord        Coordinate of the cell
 127       * @throws    PHPExcel_Exception
 128       * @return    PHPExcel_Cell    Cell that was found, or null if not found
 129       */
 130      public function getCacheData($pCoord)
 131      {
 132          if ($pCoord === $this->currentObjectID) {
 133              return $this->currentObject;
 134          }
 135          $this->storeData();
 136  
 137          //    Check if the entry that has been requested actually exists
 138          $obj = null;
 139          if (parent::isDataSet($pCoord)) {
 140              $success = false;
 141              $obj = wincache_ucache_get($this->cachePrefix.$pCoord.'.cache', $success);
 142              if ($success === false) {
 143                  //    Entry no longer exists in WinCache, so clear it from the cache array
 144                  parent::deleteCacheData($pCoord);
 145                  throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in WinCache');
 146              }
 147          } else {
 148              //    Return null if requested entry doesn't exist in cache
 149              return null;
 150          }
 151  
 152          //    Set current entry to the requested entry
 153          $this->currentObjectID = $pCoord;
 154          $this->currentObject = unserialize($obj);
 155          //    Re-attach this as the cell's parent
 156          $this->currentObject->attach($this);
 157  
 158          //    Return requested entry
 159          return $this->currentObject;
 160      }
 161  
 162  
 163      /**
 164       * Get a list of all cell addresses currently held in cache
 165       *
 166       * @return  string[]
 167       */
 168      public function getCellList()
 169      {
 170          if ($this->currentObjectID !== null) {
 171              $this->storeData();
 172          }
 173  
 174          return parent::getCellList();
 175      }
 176  
 177      /**
 178       * Delete a cell in cache identified by coordinate address
 179       *
 180       * @param    string            $pCoord        Coordinate address of the cell to delete
 181       * @throws    PHPExcel_Exception
 182       */
 183      public function deleteCacheData($pCoord)
 184      {
 185          //    Delete the entry from Wincache
 186          wincache_ucache_delete($this->cachePrefix.$pCoord.'.cache');
 187  
 188          //    Delete the entry from our cell address array
 189          parent::deleteCacheData($pCoord);
 190      }
 191  
 192      /**
 193       * Clone the cell collection
 194       *
 195       * @param    PHPExcel_Worksheet    $parent        The new worksheet
 196       * @return    void
 197       */
 198      public function copyCellCollection(PHPExcel_Worksheet $parent)
 199      {
 200          parent::copyCellCollection($parent);
 201          //    Get a new id for the new file name
 202          $baseUnique = $this->getUniqueID();
 203          $newCachePrefix = substr(md5($baseUnique), 0, 8) . '.';
 204          $cacheList = $this->getCellList();
 205          foreach ($cacheList as $cellID) {
 206              if ($cellID != $this->currentObjectID) {
 207                  $success = false;
 208                  $obj = wincache_ucache_get($this->cachePrefix.$cellID.'.cache', $success);
 209                  if ($success === false) {
 210                      //    Entry no longer exists in WinCache, so clear it from the cache array
 211                      parent::deleteCacheData($cellID);
 212                      throw new PHPExcel_Exception('Cell entry '.$cellID.' no longer exists in Wincache');
 213                  }
 214                  if (!wincache_ucache_add($newCachePrefix.$cellID.'.cache', $obj, $this->cacheTime)) {
 215                      $this->__destruct();
 216                      throw new PHPExcel_Exception('Failed to store cell '.$cellID.' in Wincache');
 217                  }
 218              }
 219          }
 220          $this->cachePrefix = $newCachePrefix;
 221      }
 222  
 223  
 224      /**
 225       * Clear the cell collection and disconnect from our parent
 226       *
 227       * @return    void
 228       */
 229      public function unsetWorksheetCells()
 230      {
 231          if (!is_null($this->currentObject)) {
 232              $this->currentObject->detach();
 233              $this->currentObject = $this->currentObjectID = null;
 234          }
 235  
 236          //    Flush the WinCache cache
 237          $this->__destruct();
 238  
 239          $this->cellCache = array();
 240  
 241          //    detach ourself from the worksheet, so that it can then delete this object successfully
 242          $this->parent = null;
 243      }
 244  
 245      /**
 246       * Initialise this new cell collection
 247       *
 248       * @param    PHPExcel_Worksheet    $parent        The worksheet for this cell collection
 249       * @param    array of mixed        $arguments    Additional initialisation arguments
 250       */
 251      public function __construct(PHPExcel_Worksheet $parent, $arguments)
 252      {
 253          $cacheTime    = (isset($arguments['cacheTime']))    ? $arguments['cacheTime']    : 600;
 254  
 255          if (is_null($this->cachePrefix)) {
 256              $baseUnique = $this->getUniqueID();
 257              $this->cachePrefix = substr(md5($baseUnique), 0, 8).'.';
 258              $this->cacheTime = $cacheTime;
 259  
 260              parent::__construct($parent);
 261          }
 262      }
 263  
 264      /**
 265       * Destroy this cell collection
 266       */
 267      public function __destruct()
 268      {
 269          $cacheList = $this->getCellList();
 270          foreach ($cacheList as $cellID) {
 271              wincache_ucache_delete($this->cachePrefix.$cellID.'.cache');
 272          }
 273      }
 274  
 275      /**
 276       * Identify whether the caching method is currently available
 277       * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
 278       *
 279       * @return    boolean
 280       */
 281      public static function cacheMethodIsAvailable()
 282      {
 283          if (!function_exists('wincache_ucache_add')) {
 284              return false;
 285          }
 286  
 287          return true;
 288      }
 289  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1