ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
Memcache.php
Go to the documentation of this file.
1 <?php
37 
38  private $_cachePrefix = null;
39 
40  private $_cacheTime = 600;
41 
42  private $_memcache = null;
43 
44 
45  private function _storeData() {
46  $this->_currentObject->detach();
47 
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()
57 
58 
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;
72 
73  $this->_currentObjectID = $pCoord;
74  $this->_currentObject = $cell;
75 
76  return $cell;
77  } // function addCacheData()
78 
79 
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()
104 
105 
113  public function getCacheData($pCoord) {
114  if ($pCoord === $this->_currentObjectID) {
115  return $this->_currentObject;
116  }
117  $this->_storeData();
118 
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  }
131 
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);
137 
138  // Return requested entry
139  return $this->_currentObject;
140  } // function getCacheData()
141 
142 
149  public function deleteCacheData($pCoord) {
150  // Delete the entry from Memcache
151  $this->_memcache->delete($this->_cachePrefix.$pCoord.'.cache');
152 
153  // Delete the entry from our cell address array
154  parent::deleteCacheData($pCoord);
155  } // function deleteCacheData()
156 
157 
163  public function copyCellCollection(PHPExcel_Worksheet $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()
185 
186 
187  public function unsetWorksheetCells() {
188  if(!is_null($this->_currentObject)) {
189  $this->_currentObject->detach();
190  $this->_currentObject = $this->_currentObjectID = null;
191  }
192 
193  // Flush the Memcache cache
194  $this->__destruct();
195 
196  $this->_cellCache = array();
197 
198  // detach ourself from the worksheet, so that it can then delete this object successfully
199  $this->_parent = null;
200  } // function unsetWorksheetCells()
201 
202 
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;
207 
208  if (is_null($this->_cachePrefix)) {
209  $baseUnique = $this->_getUniqueID();
210  $this->_cachePrefix = substr(md5($baseUnique),0,8).'.';
211 
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;
218 
219  parent::__construct($parent);
220  }
221  } // function __construct()
222 
223 
224  public function failureCallback($host, $port) {
225  throw new Exception('memcache '.$host.':'.$port.' failed');
226  }
227 
228 
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()
235 
236 }