ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilGlobalCache.php
Go to the documentation of this file.
1<?php
2require_once('./Services/GlobalCache/classes/Memcache/class.ilMemcache.php');
3require_once('./Services/GlobalCache/classes/Xcache/class.ilXcache.php');
4require_once('./Services/GlobalCache/classes/Apc/class.ilApc.php');
5require_once('./Services/GlobalCache/classes/Static/class.ilStaticCache.php');
6require_once('Settings/class.ilGlobalCacheSettings.php');
7
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';
30 const COMP_TPL_BLOCKS = 'tpl_blocks';
31 const COMP_TPL_VARIABLES = 'tpl_variables';
32 const COMP_GLOBAL_SCREEN = 'global_screen';
36 protected static $types = array(
37 self::TYPE_MEMCACHED,
38 self::TYPE_XCACHE,
39 self::TYPE_APC,
40 self::TYPE_STATIC,
41 );
45 protected static $available_types = array(
46 self::TYPE_MEMCACHED,
47 self::TYPE_XCACHE,
48 self::TYPE_APC,
49 self::TYPE_STATIC,
50 );
54 protected static $active_components = array();
58 protected static $available_components = array(
59 self::COMP_CLNG,
60 self::COMP_OBJ_DEF,
61 self::COMP_ILCTRL,
62 self::COMP_COMPONENT,
63 self::COMP_TEMPLATE,
64 self::COMP_TPL_BLOCKS,
65 self::COMP_TPL_VARIABLES,
66 self::COMP_EVENTS,
67 self::COMP_GLOBAL_SCREEN,
68 );
72 protected static $type_per_component = array();
76 protected static $unique_service_id = null;
80 protected static $instances;
84 protected $global_cache;
88 protected $component;
92 protected $active = true;
100 protected static $settings;
101
105 public static function setup(ilGlobalCacheSettings $ilGlobalCacheSettings)
106 {
107 self::setSettings($ilGlobalCacheSettings);
108 self::setActiveComponents($ilGlobalCacheSettings->getActivatedComponents());
109 }
110
115 public static function getInstance($component)
116 {
117 if (!isset(self::$instances[$component])) {
118 $service_type = self::getSettings()->getService();
119 $ilGlobalCache = new self($service_type);
120 $ilGlobalCache->setComponent($component);
121 $ilGlobalCache->initCachingService();
122
123 self::$instances[$component] = $ilGlobalCache;
124 }
125
126 return self::$instances[$component];
127 }
128
132 protected function __construct($service_type)
133 {
134 $this->checkSettings();
137 }
138
139 protected function initCachingService()
140 {
144 if (!$this->getComponent()) {
145 $this->setComponent('default');
146 }
147 $serviceName = self::lookupServiceClassName($this->getServiceType());
148 $ilGlobalCacheService = new $serviceName(self::$unique_service_id, $this->getComponent());
149 $ilGlobalCacheService->setServiceType($this->getServiceType());
150 $this->global_cache = $ilGlobalCacheService;
151 $this->setActive(in_array($this->getComponent(), self::getActiveComponents()));
152 }
153
154 protected function checkSettings()
155 {
156 if (!$this->getSettings() instanceof ilGlobalCacheSettings) {
157 $ilGlobalCacheSettings = new ilGlobalCacheSettings();
158 $this->setSettings($ilGlobalCacheSettings);
159 }
160 }
161
165 public static function log($message, $log_level)
166 {
167 if ($log_level <= self::getSettings()->getLogLevel()) {
168 global $DIC;
169 $ilLog = $DIC['ilLog'];
170 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
171 $function = $backtrace[1]['function'];
172 $class = $backtrace[1]['class'];
173 if ($ilLog instanceof ilComponentLogger) {
174 $ilLog->alert($class . '::' . $function . '(): ' . $message);
175 }
176 }
177 }
178
182 protected static function generateServiceId()
183 {
184 if (!isset(self::$unique_service_id)) {
185 $rawServiceId = '_';
186 if (defined('CLIENT_ID')) {
187 $rawServiceId .= 'il_' . CLIENT_ID;
188 }
189 self::$unique_service_id = substr(md5($rawServiceId), 0, 6);
190 }
191 }
192
193 public static function flushAll()
194 {
199 foreach (self::$types as $type) {
200 $serviceName = self::lookupServiceClassName($type);
201 $service = new $serviceName(self::generateServiceId(), 'flush');
202 if ($service->isActive()) {
203 self::log('Told ' . $serviceName . ' to flush', ilGlobalCacheSettings::LOG_LEVEL_NORMAL);
204 $returned = $service->flush();
205 self::log($serviceName . ' returned status ' . ($returned ? 'ok' : 'failure'), ilGlobalCacheSettings::LOG_LEVEL_NORMAL);
206 }
207 }
208 }
209
213 public static function getAllInstallableTypes()
214 {
215 $types = array();
216 foreach (self::getAllTypes() as $type) {
217 if ($type->isCacheServiceInstallable()) {
218 $types[] = $type;
219 }
220 }
221
222 return $types;
223 }
224
229 public static function getAllTypes($only_available = true)
230 {
231 $types = array();
232 foreach (self::$types as $type) {
233 if ($only_available && !in_array($type, self::$available_types)) {
234 continue;
235 }
236 $obj = new self($type);
237 $obj->initCachingService();
238 $types[$type] = $obj;
239 }
240
241 return $types;
242 }
243
248 public static function lookupServiceClassName($service_type)
249 {
250 switch ($service_type) {
251 case self::TYPE_APC:
252 return 'ilApc';
253 break;
255 return 'ilMemcache';
256 break;
258 return 'ilXcache';
259 break;
260 default:
261 return 'ilStaticCache';
262 break;
263 }
264 }
265
269 protected static $active_cache = array();
270
274 public function isActive()
275 {
276 $c = $this->getComponent();
277 if (isset(self::$active_cache[$c]) && self::$active_cache[$c] !== null) {
278 return self::$active_cache[$c];
279 }
280 if (!self::ACTIVE) {
281 self::$active_cache[$c] = false;
282
283 return false;
284 }
285 if (!$this->getActive()) {
286 self::log($c . '-wrapper is inactive...', ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
287 self::$active_cache[$c] = false;
288
289 return false;
290 }
291
292 $isActive = $this->global_cache->isActive();
293 self::log('component ' . $c . ', service is active: '
294 . ($isActive ? 'yes' : 'no'), ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
295 self::$active_cache[$c] = $isActive;
296
297 return $isActive;
298 }
299
304 public function isValid($key)
305 {
306 return $this->global_cache->isValid($key);
307 }
308
312 public function isInstallable()
313 {
314 return count(self::getAllInstallableTypes()) > 0;
315 }
316
321 {
322 return $this->global_cache->isInstallable();
323 }
324
329 {
330 return $this->global_cache->getInstallationFailureReason();
331 }
332
338 public function exists($key)
339 {
340 if (!$this->global_cache->isActive()) {
341 return false;
342 }
343
344 return $this->global_cache->exists($key);
345 }
346
354 public function set($key, $value, $ttl = null)
355 {
356 if (!$this->isActive()) {
357 return false;
358 }
359 self::log($key . ' set in component ' . $this->getComponent(), ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
360 $this->global_cache->setValid($key);
361
362 return $this->global_cache->set($key, $this->global_cache->serialize($value), $ttl);
363 }
364
370 public function get($key)
371 {
372 if (!$this->isActive()) {
373 return false;
374 }
375 $unserialized_return = $this->global_cache->unserialize($this->global_cache->get($key));
376 if ($unserialized_return) {
377 $service_name = ' [' . self::lookupServiceClassName($this->getServiceType()) . ']';
378 if ($this->global_cache->isValid($key)) {
379 self::log($key . ' from component ' . $this->getComponent() . $service_name, ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
380
381 return $unserialized_return;
382 } else {
383 self::log($key . ' from component ' . $this->getComponent() . ' is invalid' . $service_name, ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
384 }
385 }
386
387 return null;
388 }
389
394 public function delete($key)
395 {
396 if (!$this->isActive()) {
397 return false;
398 }
399
400 return $this->global_cache->delete($key);
401 }
402
408 public function flush($complete = false)
409 {
410 if ($this->global_cache->isActive()) {
411 return $this->global_cache->flush($complete);
412 }
413
414 return false;
415 }
416
417 public function getInfo()
418 {
419 return $this->global_cache->getInfo();
420 }
421
425 public function setComponent($component)
426 {
427 $this->component = $component;
428 }
429
433 public function getComponent()
434 {
435 return $this->component;
436 }
437
441 public function setActive($active)
442 {
443 $this->active = $active;
444 }
445
449 public function getActive()
450 {
451 return $this->active;
452 }
453
458 {
459 if ($this->global_cache instanceof ilGlobalCacheService) {
460 $this->global_cache->setServiceType($service_type);
461 }
462 $this->service_type = $service_type;
463 }
464
468 public function getServiceType()
469 {
470 if ($this->global_cache instanceof ilGlobalCacheService) {
471 return $this->global_cache->getServiceType();
472 }
473
474 return $this->service_type;
475 }
476
480 public static function getSettings()
481 {
482 return (self::$settings instanceof ilGlobalCacheSettings ? self::$settings : new ilGlobalCacheSettings());
483 }
484
488 public static function setSettings($settings)
489 {
490 self::$settings = $settings;
491 }
492
496 public static function getActiveComponents()
497 {
499 }
500
505 {
506 self::$active_components = $active_components;
507 }
508
512 public static function getAvailableComponents()
513 {
515 }
516
521 {
522 self::$available_components = $available_components;
523 }
524}
An exception for terminatinating execution or to throw for unit testing.
Component logger with individual log levels by component id.
Class ilGlobalCacheService.
Class ilGlobalCacheSettings.
Class ilGlobalCache.
setServiceType($service_type)
static setSettings($settings)
flush($complete=false)
static setAvailableComponents($available_components)
static setup(ilGlobalCacheSettings $ilGlobalCacheSettings)
static lookupServiceClassName($service_type)
static getAllInstallableTypes()
static getAllTypes($only_available=true)
static setActiveComponents($active_components)
static getActiveComponents()
static log($message, $log_level)
__construct($service_type)
setComponent($component)
static getAvailableComponents()
static getInstance($component)
$key
Definition: croninfo.php:18
catch(Exception $e) $message
$type
global $DIC
Definition: saml.php:7