ILIAS  release_7 Revision v7.30-3-g800a261c036
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
244 public static function lookupServiceClassName($service_type)
245 {
246 switch ($service_type) {
247 case self::TYPE_APC:
248 return ilApc::class;
250 return ilMemcache::class;
252 return ilXcache::class;
253 default:
254 return ilStaticCache::class;
255 }
256 }
257
258 public static function lookupServiceConfigName(int $service_type): string
259 {
260 switch ($service_type) {
261 case self::TYPE_APC:
262 return 'apc';
264 return 'memcached';
266 return 'xcache';
267 default:
268 return 'static';
269 }
270 }
271
275 protected static $active_cache = array();
276
280 public function isActive()
281 {
282 $c = $this->getComponent();
283 if (isset(self::$active_cache[$c]) && self::$active_cache[$c] !== null) {
284 return self::$active_cache[$c];
285 }
286 if (!self::ACTIVE) {
287 self::$active_cache[$c] = false;
288
289 return false;
290 }
291 if (!$this->getActive()) {
292 self::log($c . '-wrapper is inactive...', ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
293 self::$active_cache[$c] = false;
294
295 return false;
296 }
297
298 $isActive = $this->global_cache->isActive();
299 self::log('component ' . $c . ', service is active: '
300 . ($isActive ? 'yes' : 'no'), ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
301 self::$active_cache[$c] = $isActive;
302
303 return $isActive;
304 }
305
310 public function isValid($key)
311 {
312 return $this->global_cache->isValid($key);
313 }
314
318 public function isInstallable()
319 {
320 return count(self::getAllInstallableTypes()) > 0;
321 }
322
327 {
328 return $this->global_cache->isInstallable();
329 }
330
335 {
336 return $this->global_cache->getInstallationFailureReason();
337 }
338
344 public function exists($key)
345 {
346 if (!$this->global_cache->isActive()) {
347 return false;
348 }
349
350 return $this->global_cache->exists($key);
351 }
352
360 public function set($key, $value, $ttl = null)
361 {
362 if (!$this->isActive()) {
363 return false;
364 }
365 self::log($key . ' set in component ' . $this->getComponent(), ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
366 $this->global_cache->setValid($key);
367
368 return $this->global_cache->set($key, $this->global_cache->serialize($value), $ttl);
369 }
370
376 public function get($key)
377 {
378 if (!$this->isActive()) {
379 return false;
380 }
381 $unserialized_return = $this->global_cache->unserialize($this->global_cache->get($key));
382 if ($unserialized_return) {
383 $service_name = ' [' . self::lookupServiceClassName($this->getServiceType()) . ']';
384 if ($this->global_cache->isValid($key)) {
385 self::log($key . ' from component ' . $this->getComponent() . $service_name, ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
386
387 return $unserialized_return;
388 } else {
389 self::log($key . ' from component ' . $this->getComponent() . ' is invalid' . $service_name, ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
390 }
391 }
392
393 return null;
394 }
395
400 public function delete($key)
401 {
402 if (!$this->isActive()) {
403 return false;
404 }
405
406 return $this->global_cache->delete($key);
407 }
408
414 public function flush($complete = false)
415 {
416 if ($this->global_cache->isActive()) {
417 return $this->global_cache->flush($complete);
418 }
419
420 return false;
421 }
422
423 public function getInfo()
424 {
425 return $this->global_cache->getInfo();
426 }
427
431 public function setComponent($component)
432 {
433 $this->component = $component;
434 }
435
439 public function getComponent()
440 {
441 return $this->component;
442 }
443
447 public function setActive($active)
448 {
449 $this->active = $active;
450 }
451
455 public function getActive()
456 {
457 return $this->active;
458 }
459
464 {
465 if ($this->global_cache instanceof ilGlobalCacheService) {
466 $this->global_cache->setServiceType($service_type);
467 }
468 $this->service_type = $service_type;
469 }
470
474 public function getServiceType()
475 {
476 if ($this->global_cache instanceof ilGlobalCacheService) {
477 return $this->global_cache->getServiceType();
478 }
479
480 return $this->service_type;
481 }
482
486 public static function getSettings()
487 {
488 return (self::$settings instanceof ilGlobalCacheSettings ? self::$settings : new ilGlobalCacheSettings());
489 }
490
494 public static function setSettings($settings)
495 {
496 self::$settings = $settings;
497 }
498
502 public static function getActiveComponents()
503 {
505 }
506
511 {
512 self::$active_components = $active_components;
513 }
514
518 public static function getAvailableComponents()
519 {
521 }
522
527 {
528 self::$available_components = $available_components;
529 }
530}
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 lookupServiceConfigName(int $service_type)
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)
$c
Definition: cli.php:37
const CLIENT_ID
Definition: constants.php:39
global $DIC
Definition: goto.php:24
$type
$service
Definition: result.php:17
$message
Definition: xapiexit.php:14