File Source for Wincache.php

PHPExcel_CachedObjectStorage

# Source for file Wincache.php

Documentation is available at Wincache.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_Wincache
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_Wincache extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
38. private $_cachePrefix = null;
40. private $_cacheTime = 600;
43. private function _storeData() {
44. $this->_currentObject->detach();
46. $obj = serialize($this->_currentObject);
47. if (wincache_ucache_exists($this->_cachePrefix.$this->_currentObjectID.'.cache')) {
48. if (! wincache_ucache_set($this->_cachePrefix.$this->_currentObjectID.'.cache', $obj, $this->_cacheTime)) {
49. $this->__destruct();
50. throw new Exception('Failed to store cell '.$cellID.' in WinCache');
51. }
52. } else {
53. if (! wincache_ucache_add($this->_cachePrefix.$this->_currentObjectID.'.cache', $obj, $this->_cacheTime)) {
54. $this->__destruct();
55. throw new Exception('Failed to store cell '.$cellID.' in WinCache');
56. }
57. }
59. $this->_currentObjectID = $this->_currentObject = null;
60. } // function _storeData()
63. /**
64. * Add or Update a cell in cache identified by coordinate address
65. *
66. * @param string $pCoord Coordinate address of the cell to update
67. * @param PHPExcel_Cell $cell Cell to update
68. * @return void
69. * @throws Exception
70. */
71. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
72. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
73. $this->_storeData();
74. }
75. $this->_cellCache[$pCoord] = true;
77. $this->_currentObjectID = $pCoord;
78. $this->_currentObject = $cell;
80. return $cell;
81. } // function addCacheData()
84. /**
85. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
86. *
87. * @param string $pCoord Coordinate address of the cell to check
88. * @return void
89. * @return boolean
90. */
91. public function isDataSet($pCoord) {
92. // Check if the requested entry is the current object, or exists in the cache
93. if (parent:: isDataSet($pCoord)) {
94. if ($this->_currentObjectID == $pCoord) {
95. return true;
96. }
97. // Check if the requested entry still exists in cache
98. $success = wincache_ucache_exists($this->_cachePrefix.$pCoord.'.cache');
99. if ($success === false) {
100. // Entry no longer exists in Wincache, so clear it from the cache array
101. parent:: deleteCacheData($pCoord);
102. throw new Exception('Cell entry '.$cellID.' no longer exists in WinCache');
103. }
104. return true;
105. }
106. return false;
107. } // function isDataSet()
110. /**
111. * Get cell at a specific coordinate
112. *
113. * @param string $pCoord Coordinate of the cell
114. * @throws Exception
115. * @return PHPExcel_Cell Cell that was found, or null if not found
116. */
117. public function getCacheData($pCoord) {
118. if ($pCoord === $this->_currentObjectID) {
119. return $this->_currentObject;
120. }
121. $this->_storeData();
123. // Check if the entry that has been requested actually exists
124. $obj = null;
125. if (parent:: isDataSet($pCoord)) {
126. $success = false;
127. $obj = wincache_ucache_get($this->_cachePrefix.$pCoord.'.cache', $success);
128. if ($success === false) {
129. // Entry no longer exists in WinCache, so clear it from the cache array
130. parent:: deleteCacheData($pCoord);
131. throw new Exception('Cell entry '.$cellID.' no longer exists in WinCache');
132. }
133. } else {
134. // Return null if requested entry doesn't exist in cache
135. return null;
136. }
138. // Set current entry to the requested entry
139. $this->_currentObjectID = $pCoord;
140. $this->_currentObject = unserialize($obj);
141. // Re-attach the parent worksheet
142. $this->_currentObject->attach($this->_parent);
144. // Return requested entry
145. return $this->_currentObject;
146. } // function getCacheData()
149. /**
150. * Delete a cell in cache identified by coordinate address
151. *
152. * @param string $pCoord Coordinate address of the cell to delete
153. * @throws Exception
154. */
155. public function deleteCacheData($pCoord) {
156. // Delete the entry from Wincache
157. wincache_ucache_delete($this->_cachePrefix.$pCoord.'.cache');
159. // Delete the entry from our cell address array
160. parent:: deleteCacheData($pCoord);
161. } // function deleteCacheData()
164. /**
165. * Clone the cell collection
166. *
167. * @return void
168. */
169. public function copyCellCollection(PHPExcel_Worksheet $parent) {
170. parent:: copyCellCollection($parent);
171. // Get a new id for the new file name
172. $baseUnique = $this->_getUniqueID();
173. $newCachePrefix = substr( md5($baseUnique),0,8).'.';
174. $cacheList = $this-> getCellList();
175. foreach($cacheList as $cellID) {
176. if ($cellID != $this->_currentObjectID) {
177. $success = false;
178. $obj = wincache_ucache_get($this->_cachePrefix.$cellID.'.cache', $success);
179. if ($success === false) {
180. // Entry no longer exists in WinCache, so clear it from the cache array
181. parent:: deleteCacheData($cellID);
182. throw new Exception('Cell entry '.$cellID.' no longer exists in Wincache');
183. }
184. if (! wincache_ucache_add($newCachePrefix.$cellID.'.cache', $obj, $this->_cacheTime)) {
185. $this->__destruct();
186. throw new Exception('Failed to store cell '.$cellID.' in Wincache');
187. }
188. }
189. }
190. $this->_cachePrefix = $newCachePrefix;
191. } // function copyCellCollection()
194. public function unsetWorksheetCells() {
195. if(! is_null($this->_currentObject)) {
196. $this->_currentObject->detach();
197. $this->_currentObject = $this->_currentObjectID = null;
198. }
200. // Flush the WinCache cache
201. $this->__destruct();
203. $this->_cellCache = array();
205. // detach ourself from the worksheet, so that it can then delete this object successfully
206. $this->_parent = null;
207. } // function unsetWorksheetCells()
210. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
211. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
213. if ( is_null($this->_cachePrefix)) {
214. $baseUnique = $this->_getUniqueID();
215. $this->_cachePrefix = substr( md5($baseUnique),0,8).'.';
216. $this->_cacheTime = $cacheTime;
218. parent::__construct($parent);
219. }
220. } // function __construct()
223. public function __destruct() {
224. $cacheList = $this-> getCellList();
225. foreach($cacheList as $cellID) {
226. wincache_ucache_delete($this->_cachePrefix.$cellID.'.cache');
227. }
228. } // function __destruct()
230. }

Documentation generated on Sun, 27 Feb 2011 16:37:01 -0800 by phpDocumentor 1.4.3