ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
Wincache.php
Go to the documentation of this file.
1 <?php
37 
38  private $_cachePrefix = null;
39 
40  private $_cacheTime = 600;
41 
42 
43  private function _storeData() {
44  $this->_currentObject->detach();
45 
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  }
58 
59  $this->_currentObjectID = $this->_currentObject = null;
60  } // function _storeData()
61 
62 
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;
76 
77  $this->_currentObjectID = $pCoord;
78  $this->_currentObject = $cell;
79 
80  return $cell;
81  } // function addCacheData()
82 
83 
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()
108 
109 
117  public function getCacheData($pCoord) {
118  if ($pCoord === $this->_currentObjectID) {
119  return $this->_currentObject;
120  }
121  $this->_storeData();
122 
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  }
137 
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);
143 
144  // Return requested entry
145  return $this->_currentObject;
146  } // function getCacheData()
147 
148 
155  public function deleteCacheData($pCoord) {
156  // Delete the entry from Wincache
157  wincache_ucache_delete($this->_cachePrefix.$pCoord.'.cache');
158 
159  // Delete the entry from our cell address array
160  parent::deleteCacheData($pCoord);
161  } // function deleteCacheData()
162 
163 
169  public function copyCellCollection(PHPExcel_Worksheet $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()
192 
193 
194  public function unsetWorksheetCells() {
195  if(!is_null($this->_currentObject)) {
196  $this->_currentObject->detach();
197  $this->_currentObject = $this->_currentObjectID = null;
198  }
199 
200  // Flush the WinCache cache
201  $this->__destruct();
202 
203  $this->_cellCache = array();
204 
205  // detach ourself from the worksheet, so that it can then delete this object successfully
206  $this->_parent = null;
207  } // function unsetWorksheetCells()
208 
209 
210  public function __construct(PHPExcel_Worksheet $parent, $arguments) {
211  $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
212 
213  if (is_null($this->_cachePrefix)) {
214  $baseUnique = $this->_getUniqueID();
215  $this->_cachePrefix = substr(md5($baseUnique),0,8).'.';
216  $this->_cacheTime = $cacheTime;
217 
218  parent::__construct($parent);
219  }
220  } // function __construct()
221 
222 
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()
229 
230 }