ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
APC.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  if (!apc_store($this->_cachePrefix.$this->_currentObjectID.'.cache',serialize($this->_currentObject),$this->_cacheTime)) {
47  $this->__destruct();
48  throw new Exception('Failed to store cell '.$cellID.' in APC');
49  }
50  $this->_currentObjectID = $this->_currentObject = null;
51  } // function _storeData()
52 
53 
62  public function addCacheData($pCoord, PHPExcel_Cell $cell) {
63  if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
64  $this->_storeData();
65  }
66  $this->_cellCache[$pCoord] = true;
67 
68  $this->_currentObjectID = $pCoord;
69  $this->_currentObject = $cell;
70 
71  return $cell;
72  } // function addCacheData()
73 
74 
82  public function isDataSet($pCoord) {
83  // Check if the requested entry is the current object, or exists in the cache
84  if (parent::isDataSet($pCoord)) {
85  if ($this->_currentObjectID == $pCoord) {
86  return true;
87  }
88  // Check if the requested entry still exists in apc
89  $success = apc_fetch($this->_cachePrefix.$pCoord.'.cache');
90  if ($success === false) {
91  // Entry no longer exists in APC, so clear it from the cache array
92  parent::deleteCacheData($pCoord);
93  throw new Exception('Cell entry '.$cellID.' no longer exists in APC');
94  }
95  return true;
96  }
97  return false;
98  } // function isDataSet()
99 
100 
108  public function getCacheData($pCoord) {
109  if ($pCoord === $this->_currentObjectID) {
110  return $this->_currentObject;
111  }
112  $this->_storeData();
113 
114  // Check if the entry that has been requested actually exists
115  if (parent::isDataSet($pCoord)) {
116  $obj = apc_fetch($this->_cachePrefix.$pCoord.'.cache');
117  if ($obj === false) {
118  // Entry no longer exists in APC, so clear it from the cache array
119  parent::deleteCacheData($pCoord);
120  throw new Exception('Cell entry '.$cellID.' no longer exists in APC');
121  }
122  } else {
123  // Return null if requested entry doesn't exist in cache
124  return null;
125  }
126 
127  // Set current entry to the requested entry
128  $this->_currentObjectID = $pCoord;
129  $this->_currentObject = unserialize($obj);
130  // Re-attach the parent worksheet
131  $this->_currentObject->attach($this->_parent);
132 
133  // Return requested entry
134  return $this->_currentObject;
135  } // function getCacheData()
136 
137 
144  public function deleteCacheData($pCoord) {
145  // Delete the entry from APC
146  apc_delete($this->_cachePrefix.$pCoord.'.cache');
147 
148  // Delete the entry from our cell address array
149  parent::deleteCacheData($pCoord);
150  } // function deleteCacheData()
151 
152 
158  public function copyCellCollection(PHPExcel_Worksheet $parent) {
160  // Get a new id for the new file name
161  $baseUnique = $this->_getUniqueID();
162  $newCachePrefix = substr(md5($baseUnique),0,8).'.';
163  $cacheList = $this->getCellList();
164  foreach($cacheList as $cellID) {
165  if ($cellID != $this->_currentObjectID) {
166  $obj = apc_fetch($this->_cachePrefix.$cellID.'.cache');
167  if ($obj === false) {
168  // Entry no longer exists in APC, so clear it from the cache array
169  parent::deleteCacheData($cellID);
170  throw new Exception('Cell entry '.$cellID.' no longer exists in APC');
171  }
172  if (!apc_store($newCachePrefix.$cellID.'.cache',$obj,$this->_cacheTime)) {
173  $this->__destruct();
174  throw new Exception('Failed to store cell '.$cellID.' in APC');
175  }
176  }
177  }
178  $this->_cachePrefix = $newCachePrefix;
179  } // function copyCellCollection()
180 
181 
182  public function unsetWorksheetCells() {
183  if(!is_null($this->_currentObject)) {
184  $this->_currentObject->detach();
185  $this->_currentObject = $this->_currentObjectID = null;
186  }
187 
188  // Flush the APC cache
189  $this->__destruct();
190 
191  $this->_cellCache = array();
192 
193  // detach ourself from the worksheet, so that it can then delete this object successfully
194  $this->_parent = null;
195  } // function unsetWorksheetCells()
196 
197 
198  public function __construct(PHPExcel_Worksheet $parent, $arguments) {
199  $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
200 
201  if (is_null($this->_cachePrefix)) {
202  $baseUnique = $this->_getUniqueID();
203  $this->_cachePrefix = substr(md5($baseUnique),0,8).'.';
204  $this->_cacheTime = $cacheTime;
205 
206  parent::__construct($parent);
207  }
208  } // function __construct()
209 
210 
211  public function __destruct() {
212  $cacheList = $this->getCellList();
213  foreach($cacheList as $cellID) {
214  apc_delete($this->_cachePrefix.$cellID.'.cache');
215  }
216  } // function __destruct()
217 
218 }