File Source for Memcache.php

PHPExcel_CachedObjectStorage

# Source for file Memcache.php

Documentation is available at Memcache.php

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

Documentation generated on Sun, 27 Feb 2011 16:32:46 -0800 by phpDocumentor 1.4.3