PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/phpexcel/Classes/PHPExcel/CachedObjectStorage/Wincache.php

https://github.com/Borluse/ProjetOption
PHP | 201 lines | 92 code | 36 blank | 73 comment | 16 complexity | f7f4151c553b79ad0f98bb6ec1bf6e05 MD5 | raw file
  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. * PHPExcel_CachedObjectStorage_Wincache
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_CachedObjectStorage_Wincache extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
  35. private $_cachePrefix = null;
  36. private $_cacheTime = 600;
  37. private function _storeData() {
  38. $this->_currentObject->detach();
  39. $obj = serialize($this->_currentObject);
  40. if (wincache_ucache_exists($this->_cachePrefix.$this->_currentObjectID.'.cache')) {
  41. wincache_ucache_set($this->_cachePrefix.$this->_currentObjectID.'.cache', $obj, $this->_cacheTime);
  42. } else {
  43. wincache_ucache_add($this->_cachePrefix.$this->_currentObjectID.'.cache', $obj, $this->_cacheTime);
  44. }
  45. $this->_currentObjectID = $this->_currentObject = null;
  46. } // function _storeData()
  47. /**
  48. * Add or Update a cell in cache identified by coordinate address
  49. *
  50. * @param string $pCoord Coordinate address of the cell to update
  51. * @param PHPExcel_Cell $cell Cell to update
  52. * @return void
  53. * @throws Exception
  54. */
  55. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  56. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  57. $this->_storeData();
  58. }
  59. $this->_cellCache[$pCoord] = true;
  60. $this->_currentObjectID = $pCoord;
  61. $this->_currentObject = $cell;
  62. return $cell;
  63. } // function addCacheData()
  64. /**
  65. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  66. *
  67. * @param string $pCoord Coordinate address of the cell to check
  68. * @return void
  69. * @return boolean
  70. */
  71. public function isDataSet($pCoord) {
  72. // Check if the requested entry is the current object, or exists in the cache
  73. if (parent::isDataSet($pCoord)) {
  74. if ($this->_currentObjectID == $pCoord) {
  75. return true;
  76. }
  77. // Check if the requested entry still exists in cache
  78. $success = wincache_ucache_exists($this->_cachePrefix.$pCoord.'.cache');
  79. if ($success === false) {
  80. // Entry no longer exists in Wincache, so clear it from the cache array
  81. parent::deleteCacheData($pCoord);
  82. throw new Exception('Cell entry no longer exists in Wincache');
  83. }
  84. return true;
  85. }
  86. return false;
  87. } // function isDataSet()
  88. /**
  89. * Get cell at a specific coordinate
  90. *
  91. * @param string $pCoord Coordinate of the cell
  92. * @throws Exception
  93. * @return PHPExcel_Cell Cell that was found, or null if not found
  94. */
  95. public function getCacheData($pCoord) {
  96. if ($pCoord === $this->_currentObjectID) {
  97. return $this->_currentObject;
  98. }
  99. $this->_storeData();
  100. // Check if the entry that has been requested actually exists
  101. $obj = null;
  102. if (parent::isDataSet($pCoord)) {
  103. $success = false;
  104. $obj = wincache_ucache_get($this->_cachePrefix.$pCoord.'.cache', $success);
  105. if ($success === false) {
  106. // Entry no longer exists in Wincache, so clear it from the cache array
  107. parent::deleteCacheData($pCoord);
  108. throw new Exception('Cell entry no longer exists in Wincache');
  109. }
  110. } else {
  111. // Return null if requested entry doesn't exist in cache
  112. return null;
  113. }
  114. // Set current entry to the requested entry
  115. $this->_currentObjectID = $pCoord;
  116. $this->_currentObject = unserialize($obj);
  117. // Re-attach the parent worksheet
  118. $this->_currentObject->attach($this->_parent);
  119. // Return requested entry
  120. return $this->_currentObject;
  121. } // function getCacheData()
  122. /**
  123. * Delete a cell in cache identified by coordinate address
  124. *
  125. * @param string $pCoord Coordinate address of the cell to delete
  126. * @throws Exception
  127. */
  128. public function deleteCacheData($pCoord) {
  129. // Delete the entry from Wincache
  130. wincache_ucache_delete($this->_cachePrefix.$pCoord.'.cache');
  131. // Delete the entry from our cell address array
  132. parent::deleteCacheData($pCoord);
  133. } // function deleteCacheData()
  134. public function unsetWorksheetCells() {
  135. if(!is_null($this->_currentObject)) {
  136. $this->_currentObject->detach();
  137. $this->_currentObject = $this->_currentObjectID = null;
  138. }
  139. // Flush the Wincache cache
  140. $this->__destruct();
  141. $this->_cellCache = array();
  142. // detach ourself from the worksheet, so that it can then delete this object successfully
  143. $this->_parent = null;
  144. } // function unsetWorksheetCells()
  145. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
  146. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
  147. if (is_null($this->_cachePrefix)) {
  148. if (function_exists('posix_getpid')) {
  149. $baseUnique = posix_getpid();
  150. } else {
  151. $baseUnique = mt_rand();
  152. }
  153. $this->_cachePrefix = substr(md5(uniqid($baseUnique,true)),0,8).'.';
  154. $this->_cacheTime = $cacheTime;
  155. parent::__construct($parent);
  156. }
  157. } // function __construct()
  158. public function __destruct() {
  159. $cacheList = $this->getCellList();
  160. foreach($cacheList as $cellID) {
  161. wincache_ucache_delete($this->_cachePrefix.$cellID.'.cache');
  162. }
  163. } // function __destruct()
  164. }
  165. ?>