Memcache.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 PHPExcel_CachedObjectStorage_ICache {
  35. private $_cachePrefix = null;
  36. private $_cacheTime = 600;
  37. private $_memcache = null;
  38. private function _storeData() {
  39. $this->_currentObject->detach();
  40. $obj = serialize($this->_currentObject);
  41. if (!$this->_memcache->replace($this->_cachePrefix.$this->_currentObjectID.'.cache',$obj,NULL,$this->_cacheTime)) {
  42. if (!$this->_memcache->add($this->_cachePrefix.$this->_currentObjectID.'.cache',$obj,NULL,$this->_cacheTime)) {
  43. $this->__destruct();
  44. throw new Exception('Failed to store cell '.$cellID.' in MemCache');
  45. }
  46. }
  47. $this->_currentObjectID = $this->_currentObject = null;
  48. } // function _storeData()
  49. /**
  50. * Add or Update a cell in cache identified by coordinate address
  51. *
  52. * @param string $pCoord Coordinate address of the cell to update
  53. * @param PHPExcel_Cell $cell Cell to update
  54. * @return void
  55. * @throws Exception
  56. */
  57. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  58. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  59. $this->_storeData();
  60. }
  61. $this->_cellCache[$pCoord] = true;
  62. $this->_currentObjectID = $pCoord;
  63. $this->_currentObject = $cell;
  64. return $cell;
  65. } // function addCacheData()
  66. /**
  67. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  68. *
  69. * @param string $pCoord Coordinate address of the cell to check
  70. * @return void
  71. * @return boolean
  72. */
  73. public function isDataSet($pCoord) {
  74. // Check if the requested entry is the current object, or exists in the cache
  75. if (parent::isDataSet($pCoord)) {
  76. if ($this->_currentObjectID == $pCoord) {
  77. return true;
  78. }
  79. // Check if the requested entry still exists in Memcache
  80. $success = $this->_memcache->get($this->_cachePrefix.$pCoord.'.cache');
  81. if ($success === false) {
  82. // Entry no longer exists in Memcache, so clear it from the cache array
  83. parent::deleteCacheData($pCoord);
  84. throw new Exception('Cell entry '.$cellID.' no longer exists in MemCache');
  85. }
  86. return true;
  87. }
  88. return false;
  89. } // function isDataSet()
  90. /**
  91. * Get cell at a specific coordinate
  92. *
  93. * @param string $pCoord Coordinate of the cell
  94. * @throws Exception
  95. * @return PHPExcel_Cell Cell that was found, or null if not found
  96. */
  97. public function getCacheData($pCoord) {
  98. if ($pCoord === $this->_currentObjectID) {
  99. return $this->_currentObject;
  100. }
  101. $this->_storeData();
  102. // Check if the entry that has been requested actually exists
  103. if (parent::isDataSet($pCoord)) {
  104. $obj = $this->_memcache->get($this->_cachePrefix.$pCoord.'.cache');
  105. if ($obj === false) {
  106. // Entry no longer exists in Memcache, so clear it from the cache array
  107. parent::deleteCacheData($pCoord);
  108. throw new Exception('Cell entry '.$cellID.' no longer exists in MemCache');
  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 Memcache
  130. $this->_memcache->delete($this->_cachePrefix.$pCoord.'.cache');
  131. // Delete the entry from our cell address array
  132. parent::deleteCacheData($pCoord);
  133. } // function deleteCacheData()
  134. /**
  135. * Clone the cell collection
  136. *
  137. * @return void
  138. */
  139. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  140. parent::copyCellCollection($parent);
  141. // Get a new id for the new file name
  142. $baseUnique = $this->_getUniqueID();
  143. $newCachePrefix = substr(md5($baseUnique),0,8).'.';
  144. $cacheList = $this->getCellList();
  145. foreach($cacheList as $cellID) {
  146. if ($cellID != $this->_currentObjectID) {
  147. $obj = $this->_memcache->get($this->_cachePrefix.$cellID.'.cache');
  148. if ($obj === false) {
  149. // Entry no longer exists in Memcache, so clear it from the cache array
  150. parent::deleteCacheData($cellID);
  151. throw new Exception('Cell entry '.$cellID.' no longer exists in MemCache');
  152. }
  153. if (!$this->_memcache->add($newCachePrefix.$cellID.'.cache',$obj,NULL,$this->_cacheTime)) {
  154. $this->__destruct();
  155. throw new Exception('Failed to store cell '.$cellID.' in MemCache');
  156. }
  157. }
  158. }
  159. $this->_cachePrefix = $newCachePrefix;
  160. } // function copyCellCollection()
  161. public function unsetWorksheetCells() {
  162. if(!is_null($this->_currentObject)) {
  163. $this->_currentObject->detach();
  164. $this->_currentObject = $this->_currentObjectID = null;
  165. }
  166. // Flush the Memcache cache
  167. $this->__destruct();
  168. $this->_cellCache = array();
  169. // detach ourself from the worksheet, so that it can then delete this object successfully
  170. $this->_parent = null;
  171. } // function unsetWorksheetCells()
  172. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
  173. $memcacheServer = (isset($arguments['memcacheServer'])) ? $arguments['memcacheServer'] : 'localhost';
  174. $memcachePort = (isset($arguments['memcachePort'])) ? $arguments['memcachePort'] : 11211;
  175. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
  176. if (is_null($this->_cachePrefix)) {
  177. $baseUnique = $this->_getUniqueID();
  178. $this->_cachePrefix = substr(md5($baseUnique),0,8).'.';
  179. // Set a new Memcache object and connect to the Memcache server
  180. $this->_memcache = new Memcache();
  181. if (!$this->_memcache->addServer($memcacheServer, $memcachePort, false, 50, 5, 5, true, array($this, 'failureCallback'))) {
  182. throw new Exception('Could not connect to MemCache server at '.$memcacheServer.':'.$memcachePort);
  183. }
  184. $this->_cacheTime = $cacheTime;
  185. parent::__construct($parent);
  186. }
  187. } // function __construct()
  188. public function failureCallback($host, $port) {
  189. throw new Exception('memcache '.$host.':'.$port.' failed');
  190. }
  191. public function __destruct() {
  192. $cacheList = $this->getCellList();
  193. foreach($cacheList as $cellID) {
  194. $this->_memcache->delete($this->_cachePrefix.$cellID.'.cache');
  195. }
  196. } // function __destruct()
  197. }