ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilGlobalCache.php
Go to the documentation of this file.
1 <?php
2 require_once('./Services/GlobalCache/classes/Memcache/class.ilMemcache.php');
3 require_once('./Services/GlobalCache/classes/Xcache/class.ilXcache.php');
4 require_once('./Services/GlobalCache/classes/Apc/class.ilApc.php');
5 require_once('./Services/GlobalCache/classes/Static/class.ilStaticCache.php');
6 
14 
15  const MSG = 'Global Cache not active, can not access cache';
16  const ACTIVE = true;
17  const TYPE_STATIC = 0;
18  const TYPE_XCACHE = 1;
19  const TYPE_MEMCACHED = 2;
20  const TYPE_APC = 3;
22  const COMP_CLNG = 'clng';
23  const COMP_OBJ_DEF = 'obj_def';
24  const COMP_TEMPLATE = 'tpl';
25  const COMP_ILCTRL = 'ilctrl';
26  const COMP_PLUGINS = 'plugins';
27  const COMP_COMPONENT = 'comp';
28  const COMP_RBAC_UA = 'rbac_ua';
29  const COMP_EVENTS = 'events';
33  protected static $types = array(
34  // self::TYPE_MEMCACHED,
35  // self::TYPE_XCACHE,
36  self::TYPE_APC,
37  self::TYPE_STATIC
38  );
42  protected static $active_components = array(
43  self::COMP_CLNG,
44  self::COMP_OBJ_DEF,
45  self::COMP_ILCTRL,
46  self::COMP_COMPONENT,
47  self::COMP_TEMPLATE,
48  self::COMP_EVENTS,
49  //'ctrl_mm'
50  );
54  protected static $type_per_component = array();
58  protected static $unique_service_id = NULL;
62  protected static $instances;
66  protected $global_cache;
70  protected $service_id = '';
74  protected $component;
78  protected $active = true;
83 
84 
90  protected static function getComponentType($component = NULL) {
91  $component = 0; // In this Version All Components have the same Caching-Type
92  if (!isset(self::$type_per_component[$component])) {
96  global $ilClientIniFile;
97  if ($ilClientIniFile instanceof ilIniFile) {
98  self::$type_per_component[$component] = $ilClientIniFile->readVariable('cache', 'global_cache_service_type');
99  }
100  }
101 
102  if (self::$type_per_component[$component]) {
103  return self::$type_per_component[$component];
104  }
105 
106  return self::TYPE_FALLBACK;
107  }
108 
109 
115  public static function getInstance($component) {
116  if (!isset(self::$instances[$component])) {
117  $service_type = self::getComponentType($component);
118  $ilGlobalCache = new self($service_type, $component);
119 
120  self::$instances[$component] = $ilGlobalCache;
121  }
122 
123  return self::$instances[$component];
124  }
125 
126 
130  protected static function generateServiceId() {
131  if (!isset(self::$unique_service_id)) {
132  self::$unique_service_id = substr(md5('il_' . CLIENT_ID), 0, 6);
133  }
134 
136  }
137 
138 
139  public static function flushAll() {
143  foreach (self::$types as $type) {
144  $serviceName = self::lookupServiceName($type);
145  $service = new $serviceName(self::generateServiceId(), 'flush');
146  if ($service->isActive()) {
147  $service->flush();
148  }
149  }
150  }
151 
152 
156  public static function getAllInstallableTypes() {
157  $types = array();
158  foreach (self::getAllTypes() as $type) {
159  if ($type->isCacheServiceInstallable()) {
160  $types[] = $type;
161  }
162  }
163 
164  return $types;
165  }
166 
167 
171  public static function getAllTypes() {
172  $types = array();
173  foreach (self::$types as $type) {
174  $obj = new self($type);
175  $types[$type] = $obj;
176  }
177 
178  return $types;
179  }
180 
181 
186  protected function __construct($service_type_id, $component = NULL) {
187  $this->setComponent($component);
188  $this->setServiceid(self::generateServiceId());
189  $this->setActive(in_array($component, self::$active_components));
190  $serviceName = self::lookupServiceName($service_type_id);
191  $this->global_cache = new $serviceName($this->getServiceid(), $this->getComponent());
192  $this->global_cache->setServiceType($service_type_id);
193  }
194 
195 
201  protected static function lookupServiceName($type_id) {
202  switch ($type_id) {
203  case self::TYPE_APC:
204  return 'ilApc';
205  break;
206  case self::TYPE_MEMCACHED:
207  return 'ilMemcache';
208  break;
209  case self::TYPE_XCACHE:
210  return 'ilXcache';
211  break;
212  default:
213  return 'ilStaticCache';
214  break;
215  }
216  }
217 
218 
222  public function isActive() {
223  if (!self::ACTIVE) {
224 
225  return false;
226  }
230  global $ilClientIniFile;
231  if ($ilClientIniFile instanceof ilIniFile) {
232  if ($ilClientIniFile->readVariable('cache', 'activate_global_cache') != '1') {
233  return false;
234  }
235  } else {
236  return false;
237  }
238  if (!$this->getActive()) {
239  return false;
240  }
241 
242  return $this->global_cache->isActive();
243  }
244 
245 
251  public function isValid($key) {
252  return $this->global_cache->isValid($key);
253  }
254 
255 
259  public function isInstallable() {
260  return count(self::getAllInstallableTypes()) > 0;
261  }
262 
263 
267  public function isCacheServiceInstallable() {
268  return $this->global_cache->isInstallable();
269  }
270 
271 
275  public function getInstallationFailureReason() {
276  return $this->global_cache->getInstallationFailureReason();
277  }
278 
279 
286  public function exists($key) {
287  if (!$this->global_cache->isActive()) {
288  return false;
289  }
290 
291  return $this->global_cache->exists($key);
292  }
293 
294 
303  public function set($key, $value, $ttl = NULL) {
304  if (!$this->isActive()) {
305  return false;
306  }
307  $this->global_cache->setValid($key);
308 
309  return $this->global_cache->set($key, $this->global_cache->serialize($value), $ttl);
310  }
311 
312 
319  public function get($key) {
320  if (!$this->isActive()) {
321  return false;
322  }
323  $unserialized_return = $this->global_cache->unserialize($this->global_cache->get($key));
324  if ($unserialized_return) {
325 
326  if ($this->global_cache->isValid($key)) {
327 
328  return $unserialized_return;
329  } else {
330  // var_dump($key); // FSX
331  }
332  }
333 
334  return NULL;
335  }
336 
337 
344  public function delete($key) {
345  if (!$this->isActive()) {
346 
347  return false;
348  throw new RuntimeException(self::MSG);
349  }
350 
351  return $this->global_cache->delete($key);
352  }
353 
354 
361  public function flush($complete = false) {
362  if ($this->global_cache->isActive()) {
363  if ($complete) {
364  return $this->global_cache->flush();
365  } else {
366  $this->global_cache->setInvalid();
367  }
368  }
369 
370  return false;
371  }
372 
373 
374  public function getInfo() {
375  return $this->global_cache->getInfo();
376  }
377 
378 
382  public function setServiceid($service_id) {
383  $this->service_id = $service_id;
384  }
385 
386 
390  public function getServiceid() {
391  return $this->service_id;
392  }
393 
394 
398  public function setComponent($component) {
399  $this->component = $component;
400  }
401 
402 
406  public function getComponent() {
407  return $this->component;
408  }
409 
410 
414  public function setActive($active) {
415  $this->active = $active;
416  }
417 
418 
422  public function getActive() {
423  return $this->active;
424  }
425 
426 
430  public function setServiceType($service_type) {
431  $this->global_cache->setServiceType($service_type);
432  }
433 
434 
438  public function getServiceType() {
439  return $this->global_cache->getServiceType();
440  }
441 }
442 
443 ?>