Memcache.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_Memcache
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2014 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 PHPExcel_Exception
  59. */
  60. protected function _storeData() {
  61. if ($this->_currentCellIsDirty && !empty($this->_currentObjectID)) {
  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 PHPExcel_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 PHPExcel_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 PHPExcel_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 PHPExcel_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 PHPExcel_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 this as the cell's parent
  144. $this->_currentObject->attach($this);
  145. // Return requested entry
  146. return $this->_currentObject;
  147. } // function getCacheData()
  148. /**
  149. * Get a list of all cell addresses currently held in cache
  150. *
  151. * @return array of string
  152. */
  153. public function getCellList() {
  154. if ($this->_currentObjectID !== null) {
  155. $this->_storeData();
  156. }
  157. return parent::getCellList();
  158. }
  159. /**
  160. * Delete a cell in cache identified by coordinate address
  161. *
  162. * @param string $pCoord Coordinate address of the cell to delete
  163. * @throws PHPExcel_Exception
  164. */
  165. public function deleteCacheData($pCoord) {
  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. } // function deleteCacheData()
  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. parent::copyCellCollection($parent);
  179. // Get a new id for the new file name
  180. $baseUnique = $this->_getUniqueID();
  181. $newCachePrefix = substr(md5($baseUnique),0,8).'.';
  182. $cacheList = $this->getCellList();
  183. foreach($cacheList as $cellID) {
  184. if ($cellID != $this->_currentObjectID) {
  185. $obj = $this->_memcache->get($this->_cachePrefix.$cellID.'.cache');
  186. if ($obj === false) {
  187. // Entry no longer exists in Memcache, so clear it from the cache array
  188. parent::deleteCacheData($cellID);
  189. throw new PHPExcel_Exception('Cell entry '.$cellID.' no longer exists in MemCache');
  190. }
  191. if (!$this->_memcache->add($newCachePrefix.$cellID.'.cache',$obj,NULL,$this->_cacheTime)) {
  192. $this->__destruct();
  193. throw new PHPExcel_Exception('Failed to store cell '.$cellID.' in MemCache');
  194. }
  195. }
  196. }
  197. $this->_cachePrefix = $newCachePrefix;
  198. } // function copyCellCollection()
  199. /**
  200. * Clear the cell collection and disconnect from our parent
  201. *
  202. * @return void
  203. */
  204. public function unsetWorksheetCells() {
  205. if(!is_null($this->_currentObject)) {
  206. $this->_currentObject->detach();
  207. $this->_currentObject = $this->_currentObjectID = null;
  208. }
  209. // Flush the Memcache cache
  210. $this->__destruct();
  211. $this->_cellCache = array();
  212. // detach ourself from the worksheet, so that it can then delete this object successfully
  213. $this->_parent = null;
  214. } // function unsetWorksheetCells()
  215. /**
  216. * Initialise this new cell collection
  217. *
  218. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  219. * @param array of mixed $arguments Additional initialisation arguments
  220. */
  221. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
  222. $memcacheServer = (isset($arguments['memcacheServer'])) ? $arguments['memcacheServer'] : 'localhost';
  223. $memcachePort = (isset($arguments['memcachePort'])) ? $arguments['memcachePort'] : 11211;
  224. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
  225. if (is_null($this->_cachePrefix)) {
  226. $baseUnique = $this->_getUniqueID();
  227. $this->_cachePrefix = substr(md5($baseUnique),0,8).'.';
  228. // Set a new Memcache object and connect to the Memcache server
  229. $this->_memcache = new Memcache();
  230. if (!$this->_memcache->addServer($memcacheServer, $memcachePort, false, 50, 5, 5, true, array($this, 'failureCallback'))) {
  231. throw new PHPExcel_Exception('Could not connect to MemCache server at '.$memcacheServer.':'.$memcachePort);
  232. }
  233. $this->_cacheTime = $cacheTime;
  234. parent::__construct($parent);
  235. }
  236. } // function __construct()
  237. /**
  238. * Memcache error handler
  239. *
  240. * @param string $host Memcache server
  241. * @param integer $port Memcache port
  242. * @throws PHPExcel_Exception
  243. */
  244. public function failureCallback($host, $port) {
  245. throw new PHPExcel_Exception('memcache '.$host.':'.$port.' failed');
  246. }
  247. /**
  248. * Destroy this cell collection
  249. */
  250. public function __destruct() {
  251. $cacheList = $this->getCellList();
  252. foreach($cacheList as $cellID) {
  253. $this->_memcache->delete($this->_cachePrefix.$cellID.'.cache');
  254. }
  255. } // function __destruct()
  256. /**
  257. * Identify whether the caching method is currently available
  258. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  259. *
  260. * @return boolean
  261. */
  262. public static function cacheMethodIsAvailable() {
  263. if (!function_exists('memcache_add')) {
  264. return false;
  265. }
  266. return true;
  267. }
  268. }