0
0

Memcache.php 10 KB

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