PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPExcel_1.7.8-with_documentation-msoffice_format/PHPExcel_1.7.8-with_documentation-msoffice_format/Classes/PHPExcel/CachedObjectStorage/Memcache.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 298 lines | 126 code | 41 blank | 131 comment | 21 complexity | 72fe3911a3d5c83cc2c9fcb2a6157029 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.8, 2012-10-12
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_Memcache
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_CachedObjectStorage_Memcache extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
  35. /**
  36. * Prefix used to uniquely identify cache data for this worksheet
  37. *
  38. * @var string
  39. */
  40. private $_cachePrefix = null;
  41. /**
  42. * Cache timeout
  43. *
  44. * @var integer
  45. */
  46. private $_cacheTime = 600;
  47. /**
  48. * Memcache interface
  49. *
  50. * @var resource
  51. */
  52. private $_memcache = null;
  53. /**
  54. * Store cell data in cache for the current cell object if it's "dirty",
  55. * and the 'nullify' the current cell object
  56. *
  57. * @return void
  58. * @throws Exception
  59. */
  60. private function _storeData() {
  61. if ($this->_currentCellIsDirty) {
  62. $this->_currentObject->detach();
  63. $obj = serialize($this->_currentObject);
  64. if (!$this->_memcache->replace($this->_cachePrefix.$this->_currentObjectID.'.cache',$obj,NULL,$this->_cacheTime)) {
  65. if (!$this->_memcache->add($this->_cachePrefix.$this->_currentObjectID.'.cache',$obj,NULL,$this->_cacheTime)) {
  66. $this->__destruct();
  67. throw new Exception('Failed to store cell '.$this->_currentObjectID.' in MemCache');
  68. }
  69. }
  70. $this->_currentCellIsDirty = false;
  71. }
  72. $this->_currentObjectID = $this->_currentObject = null;
  73. } // function _storeData()
  74. /**
  75. * Add or Update a cell in cache identified by coordinate address
  76. *
  77. * @param string $pCoord Coordinate address of the cell to update
  78. * @param PHPExcel_Cell $cell Cell to update
  79. * @return void
  80. * @throws Exception
  81. */
  82. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  83. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  84. $this->_storeData();
  85. }
  86. $this->_cellCache[$pCoord] = true;
  87. $this->_currentObjectID = $pCoord;
  88. $this->_currentObject = $cell;
  89. $this->_currentCellIsDirty = true;
  90. return $cell;
  91. } // function addCacheData()
  92. /**
  93. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  94. *
  95. * @param string $pCoord Coordinate address of the cell to check
  96. * @return void
  97. * @return boolean
  98. */
  99. public function isDataSet($pCoord) {
  100. // Check if the requested entry is the current object, or exists in the cache
  101. if (parent::isDataSet($pCoord)) {
  102. if ($this->_currentObjectID == $pCoord) {
  103. return true;
  104. }
  105. // Check if the requested entry still exists in Memcache
  106. $success = $this->_memcache->get($this->_cachePrefix.$pCoord.'.cache');
  107. if ($success === false) {
  108. // Entry no longer exists in Memcache, so clear it from the cache array
  109. parent::deleteCacheData($pCoord);
  110. throw new Exception('Cell entry '.$pCoord.' no longer exists in MemCache');
  111. }
  112. return true;
  113. }
  114. return false;
  115. } // function isDataSet()
  116. /**
  117. * Get cell at a specific coordinate
  118. *
  119. * @param string $pCoord Coordinate of the cell
  120. * @throws Exception
  121. * @return PHPExcel_Cell Cell that was found, or null if not found
  122. */
  123. public function getCacheData($pCoord) {
  124. if ($pCoord === $this->_currentObjectID) {
  125. return $this->_currentObject;
  126. }
  127. $this->_storeData();
  128. // Check if the entry that has been requested actually exists
  129. if (parent::isDataSet($pCoord)) {
  130. $obj = $this->_memcache->get($this->_cachePrefix.$pCoord.'.cache');
  131. if ($obj === false) {
  132. // Entry no longer exists in Memcache, so clear it from the cache array
  133. parent::deleteCacheData($pCoord);
  134. throw new Exception('Cell entry '.$pCoord.' no longer exists in MemCache');
  135. }
  136. } else {
  137. // Return null if requested entry doesn't exist in cache
  138. return null;
  139. }
  140. // Set current entry to the requested entry
  141. $this->_currentObjectID = $pCoord;
  142. $this->_currentObject = unserialize($obj);
  143. // Re-attach the parent worksheet
  144. $this->_currentObject->attach($this->_parent);
  145. // Return requested entry
  146. return $this->_currentObject;
  147. } // function getCacheData()
  148. /**
  149. * Delete a cell in cache identified by coordinate address
  150. *
  151. * @param string $pCoord Coordinate address of the cell to delete
  152. * @throws Exception
  153. */
  154. public function deleteCacheData($pCoord) {
  155. // Delete the entry from Memcache
  156. $this->_memcache->delete($this->_cachePrefix.$pCoord.'.cache');
  157. // Delete the entry from our cell address array
  158. parent::deleteCacheData($pCoord);
  159. } // function deleteCacheData()
  160. /**
  161. * Clone the cell collection
  162. *
  163. * @param PHPExcel_Worksheet $parent The new worksheet
  164. * @return void
  165. */
  166. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  167. parent::copyCellCollection($parent);
  168. // Get a new id for the new file name
  169. $baseUnique = $this->_getUniqueID();
  170. $newCachePrefix = substr(md5($baseUnique),0,8).'.';
  171. $cacheList = $this->getCellList();
  172. foreach($cacheList as $cellID) {
  173. if ($cellID != $this->_currentObjectID) {
  174. $obj = $this->_memcache->get($this->_cachePrefix.$cellID.'.cache');
  175. if ($obj === false) {
  176. // Entry no longer exists in Memcache, so clear it from the cache array
  177. parent::deleteCacheData($cellID);
  178. throw new Exception('Cell entry '.$cellID.' no longer exists in MemCache');
  179. }
  180. if (!$this->_memcache->add($newCachePrefix.$cellID.'.cache',$obj,NULL,$this->_cacheTime)) {
  181. $this->__destruct();
  182. throw new Exception('Failed to store cell '.$cellID.' in MemCache');
  183. }
  184. }
  185. }
  186. $this->_cachePrefix = $newCachePrefix;
  187. } // function copyCellCollection()
  188. /**
  189. * Clear the cell collection and disconnect from our parent
  190. *
  191. * @return void
  192. */
  193. public function unsetWorksheetCells() {
  194. if(!is_null($this->_currentObject)) {
  195. $this->_currentObject->detach();
  196. $this->_currentObject = $this->_currentObjectID = null;
  197. }
  198. // Flush the Memcache cache
  199. $this->__destruct();
  200. $this->_cellCache = array();
  201. // detach ourself from the worksheet, so that it can then delete this object successfully
  202. $this->_parent = null;
  203. } // function unsetWorksheetCells()
  204. /**
  205. * Initialise this new cell collection
  206. *
  207. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  208. * @param array of mixed $arguments Additional initialisation arguments
  209. */
  210. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
  211. $memcacheServer = (isset($arguments['memcacheServer'])) ? $arguments['memcacheServer'] : 'localhost';
  212. $memcachePort = (isset($arguments['memcachePort'])) ? $arguments['memcachePort'] : 11211;
  213. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
  214. if (is_null($this->_cachePrefix)) {
  215. $baseUnique = $this->_getUniqueID();
  216. $this->_cachePrefix = substr(md5($baseUnique),0,8).'.';
  217. // Set a new Memcache object and connect to the Memcache server
  218. $this->_memcache = new Memcache();
  219. if (!$this->_memcache->addServer($memcacheServer, $memcachePort, false, 50, 5, 5, true, array($this, 'failureCallback'))) {
  220. throw new Exception('Could not connect to MemCache server at '.$memcacheServer.':'.$memcachePort);
  221. }
  222. $this->_cacheTime = $cacheTime;
  223. parent::__construct($parent);
  224. }
  225. } // function __construct()
  226. /**
  227. * Memcache error handler
  228. *
  229. * @param string $host Memcache server
  230. * @param integer $port Memcache port
  231. * @throws Exception
  232. */
  233. public function failureCallback($host, $port) {
  234. throw new Exception('memcache '.$host.':'.$port.' failed');
  235. }
  236. /**
  237. * Destroy this cell collection
  238. */
  239. public function __destruct() {
  240. $cacheList = $this->getCellList();
  241. foreach($cacheList as $cellID) {
  242. $this->_memcache->delete($this->_cachePrefix.$cellID.'.cache');
  243. }
  244. } // function __destruct()
  245. /**
  246. * Identify whether the caching method is currently available
  247. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  248. *
  249. * @return boolean
  250. */
  251. public static function cacheMethodIsAvailable() {
  252. if (!function_exists('memcache_add')) {
  253. return false;
  254. }
  255. return true;
  256. }
  257. }