PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/common/libraries/plugin/phpexcel/PHPExcel/CachedObjectStorage/Memcache.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 266 lines | 148 code | 37 blank | 81 comment | 18 complexity | e1422366846a4e3fb43cfeae9cd16f6b MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  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. * PHPExcel_CachedObjectStorage_Memcache
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_CachedObjectStorage_Memcache extends PHPExcel_CachedObjectStorage_CacheBase implements
  35. PHPExcel_CachedObjectStorage_ICache
  36. {
  37. private $_cachePrefix = null;
  38. private $_cacheTime = 600;
  39. private $_memcache = null;
  40. private function _storeData()
  41. {
  42. $this->_currentObject->detach();
  43. $obj = serialize($this->_currentObject);
  44. if (! $this->_memcache->replace($this->_cachePrefix . $this->_currentObjectID . '.cache', $obj, NULL, $this->_cacheTime))
  45. {
  46. if (! $this->_memcache->add($this->_cachePrefix . $this->_currentObjectID . '.cache', $obj, NULL, $this->_cacheTime))
  47. {
  48. $this->__destruct();
  49. throw new Exception('Failed to store cell ' . $cellID . ' in MemCache');
  50. }
  51. }
  52. $this->_currentObjectID = $this->_currentObject = null;
  53. } // function _storeData()
  54. /**
  55. * Add or Update a cell in cache identified by coordinate address
  56. *
  57. * @param string $pCoord Coordinate address of the cell to update
  58. * @param PHPExcel_Cell $cell Cell to update
  59. * @return void
  60. * @throws Exception
  61. */
  62. public function addCacheData($pCoord, PHPExcel_Cell $cell)
  63. {
  64. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null))
  65. {
  66. $this->_storeData();
  67. }
  68. $this->_cellCache[$pCoord] = true;
  69. $this->_currentObjectID = $pCoord;
  70. $this->_currentObject = $cell;
  71. return $cell;
  72. } // function addCacheData()
  73. /**
  74. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  75. *
  76. * @param string $pCoord Coordinate address of the cell to check
  77. * @return void
  78. * @return boolean
  79. */
  80. public function isDataSet($pCoord)
  81. {
  82. // Check if the requested entry is the current object, or exists in the cache
  83. if (parent :: isDataSet($pCoord))
  84. {
  85. if ($this->_currentObjectID == $pCoord)
  86. {
  87. return true;
  88. }
  89. // Check if the requested entry still exists in Memcache
  90. $success = $this->_memcache->get($this->_cachePrefix . $pCoord . '.cache');
  91. if ($success === false)
  92. {
  93. // Entry no longer exists in Memcache, so clear it from the cache array
  94. parent :: deleteCacheData($pCoord);
  95. throw new Exception('Cell entry ' . $cellID . ' no longer exists in MemCache');
  96. }
  97. return true;
  98. }
  99. return false;
  100. } // function isDataSet()
  101. /**
  102. * Get cell at a specific coordinate
  103. *
  104. * @param string $pCoord Coordinate of the cell
  105. * @throws Exception
  106. * @return PHPExcel_Cell Cell that was found, or null if not found
  107. */
  108. public function getCacheData($pCoord)
  109. {
  110. if ($pCoord === $this->_currentObjectID)
  111. {
  112. return $this->_currentObject;
  113. }
  114. $this->_storeData();
  115. // Check if the entry that has been requested actually exists
  116. if (parent :: isDataSet($pCoord))
  117. {
  118. $obj = $this->_memcache->get($this->_cachePrefix . $pCoord . '.cache');
  119. if ($obj === false)
  120. {
  121. // Entry no longer exists in Memcache, so clear it from the cache array
  122. parent :: deleteCacheData($pCoord);
  123. throw new Exception('Cell entry ' . $cellID . ' no longer exists in MemCache');
  124. }
  125. }
  126. else
  127. {
  128. // Return null if requested entry doesn't exist in cache
  129. return null;
  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. // Return requested entry
  137. return $this->_currentObject;
  138. } // function getCacheData()
  139. /**
  140. * Delete a cell in cache identified by coordinate address
  141. *
  142. * @param string $pCoord Coordinate address of the cell to delete
  143. * @throws Exception
  144. */
  145. public function deleteCacheData($pCoord)
  146. {
  147. // Delete the entry from Memcache
  148. $this->_memcache->delete($this->_cachePrefix . $pCoord . '.cache');
  149. // Delete the entry from our cell address array
  150. parent :: deleteCacheData($pCoord);
  151. } // function deleteCacheData()
  152. /**
  153. * Clone the cell collection
  154. *
  155. * @return void
  156. */
  157. public function copyCellCollection(PHPExcel_Worksheet $parent)
  158. {
  159. parent :: copyCellCollection($parent);
  160. // Get a new id for the new file name
  161. $baseUnique = $this->_getUniqueID();
  162. $newCachePrefix = substr(md5($baseUnique), 0, 8) . '.';
  163. $cacheList = $this->getCellList();
  164. foreach ($cacheList as $cellID)
  165. {
  166. if ($cellID != $this->_currentObjectID)
  167. {
  168. $obj = $this->_memcache->get($this->_cachePrefix . $cellID . '.cache');
  169. if ($obj === false)
  170. {
  171. // Entry no longer exists in Memcache, so clear it from the cache array
  172. parent :: deleteCacheData($cellID);
  173. throw new Exception('Cell entry ' . $cellID . ' no longer exists in MemCache');
  174. }
  175. if (! $this->_memcache->add($newCachePrefix . $cellID . '.cache', $obj, NULL, $this->_cacheTime))
  176. {
  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. public function unsetWorksheetCells()
  185. {
  186. if (! is_null($this->_currentObject))
  187. {
  188. $this->_currentObject->detach();
  189. $this->_currentObject = $this->_currentObjectID = null;
  190. }
  191. // Flush the Memcache cache
  192. $this->__destruct();
  193. $this->_cellCache = array();
  194. // detach ourself from the worksheet, so that it can then delete this object successfully
  195. $this->_parent = null;
  196. } // function unsetWorksheetCells()
  197. public function __construct(PHPExcel_Worksheet $parent, $arguments)
  198. {
  199. $memcacheServer = (isset($arguments['memcacheServer'])) ? $arguments['memcacheServer'] : 'localhost';
  200. $memcachePort = (isset($arguments['memcachePort'])) ? $arguments['memcachePort'] : 11211;
  201. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
  202. if (is_null($this->_cachePrefix))
  203. {
  204. $baseUnique = $this->_getUniqueID();
  205. $this->_cachePrefix = substr(md5($baseUnique), 0, 8) . '.';
  206. // Set a new Memcache object and connect to the Memcache server
  207. $this->_memcache = new Memcache();
  208. if (! $this->_memcache->addServer($memcacheServer, $memcachePort, false, 50, 5, 5, true, array($this,
  209. 'failureCallback')))
  210. {
  211. throw new Exception('Could not connect to MemCache server at ' . $memcacheServer . ':' . $memcachePort);
  212. }
  213. $this->_cacheTime = $cacheTime;
  214. parent :: __construct($parent);
  215. }
  216. } // function __construct()
  217. public function failureCallback($host, $port)
  218. {
  219. throw new Exception('memcache ' . $host . ':' . $port . ' failed');
  220. }
  221. public function __destruct()
  222. {
  223. $cacheList = $this->getCellList();
  224. foreach ($cacheList as $cellID)
  225. {
  226. $this->_memcache->delete($this->_cachePrefix . $cellID . '.cache');
  227. }
  228. } // function __destruct()
  229. }