ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
15{
16 const MSG = 'Global Cache not active, can not access cache';
17 const ACTIVE = true;
18 const TYPE_STATIC = 0;
19 const TYPE_XCACHE = 1;
20 const TYPE_MEMCACHED = 2;
21 const TYPE_APC = 3;
23 const COMP_CLNG = 'clng';
24 const COMP_OBJ_DEF = 'obj_def';
25 const COMP_TEMPLATE = 'tpl';
26 const COMP_ILCTRL = 'ilctrl';
27 const COMP_PLUGINS = 'plugins';
28 const COMP_COMPONENT = 'comp';
29 const COMP_RBAC_UA = 'rbac_ua';
30 const COMP_EVENTS = 'events';
31 const COMP_TPL_BLOCKS = 'tpl_blocks';
32 const COMP_TPL_VARIABLES = 'tpl_variables';
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 );
71 protected static $type_per_component = array();
75 protected static $unique_service_id = null;
79 protected static $instances;
83 protected $global_cache;
87 protected $component;
91 protected $active = true;
99 protected static $settings;
100
101
105 public static function setup(ilGlobalCacheSettings $ilGlobalCacheSettings)
106 {
107 self::setSettings($ilGlobalCacheSettings);
108 self::setActiveComponents($ilGlobalCacheSettings->getActivatedComponents());
109 }
110
111
117 public static function getInstance($component)
118 {
119 if (!isset(self::$instances[$component])) {
120 $service_type = self::getSettings()->getService();
121 $ilGlobalCache = new self($service_type);
122 $ilGlobalCache->setComponent($component);
123 $ilGlobalCache->initCachingService();
124
125 self::$instances[$component] = $ilGlobalCache;
126 }
127
128 return self::$instances[$component];
129 }
130
131
135 protected function __construct($service_type)
136 {
137 $this->checkSettings();
140 }
141
142
143 protected function initCachingService()
144 {
148 if (!$this->getComponent()) {
149 $this->setComponent('default');
150 }
151 $serviceName = self::lookupServiceClassName($this->getServiceType());
152 $ilGlobalCacheService = new $serviceName(self::$unique_service_id, $this->getComponent());
153 $ilGlobalCacheService->setServiceType($this->getServiceType());
154 $this->global_cache = $ilGlobalCacheService;
155 $this->setActive(in_array($this->getComponent(), self::getActiveComponents()));
156 }
157
158
159 protected function checkSettings()
160 {
161 if (!$this->getSettings() instanceof ilGlobalCacheSettings) {
162 $ilGlobalCacheSettings = new ilGlobalCacheSettings();
163 $this->setSettings($ilGlobalCacheSettings);
164 }
165 }
166
167
171 public static function log($message, $log_level)
172 {
173 if ($log_level <= self::getSettings()->getLogLevel()) {
174 global $DIC;
175 $ilLog = $DIC['ilLog'];
176 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
177 $function = $backtrace[1]['function'];
178 $class = $backtrace[1]['class'];
179 if ($ilLog instanceof ilComponentLogger) {
180 $ilLog->alert($class . '::' . $function . '(): ' . $message);
181 }
182 }
183 }
184
185
189 protected static function generateServiceId()
190 {
191 if (!isset(self::$unique_service_id)) {
192 $rawServiceId = '_';
193 if (defined('CLIENT_ID')) {
194 $rawServiceId .= 'il_' . CLIENT_ID;
195 }
196 self::$unique_service_id = substr(md5($rawServiceId), 0, 6);
197 }
198 }
199
200
201 public static function flushAll()
202 {
207 foreach (self::$types as $type) {
208 $serviceName = self::lookupServiceClassName($type);
209 $service = new $serviceName(self::generateServiceId(), 'flush');
210 if ($service->isActive()) {
211 self::log('Told ' . $serviceName . ' to flush', ilGlobalCacheSettings::LOG_LEVEL_NORMAL);
212 $returned = $service->flush();
213 self::log($serviceName . ' returned status ' . ($returned ? 'ok' : 'failure'), ilGlobalCacheSettings::LOG_LEVEL_NORMAL);
214 }
215 }
216 }
217
218
222 public static function getAllInstallableTypes()
223 {
224 $types = array();
225 foreach (self::getAllTypes() as $type) {
226 if ($type->isCacheServiceInstallable()) {
227 $types[] = $type;
228 }
229 }
230
231 return $types;
232 }
233
234
239 public static function getAllTypes($only_available = true)
240 {
241 $types = array();
242 foreach (self::$types as $type) {
243 if ($only_available && !in_array($type, self::$available_types)) {
244 continue;
245 }
246 $obj = new self($type);
247 $obj->initCachingService();
248 $types[$type] = $obj;
249 }
250
251 return $types;
252 }
253
254
260 public static function lookupServiceClassName($service_type)
261 {
262 switch ($service_type) {
263 case self::TYPE_APC:
264 return 'ilApc';
265 break;
267 return 'ilMemcache';
268 break;
270 return 'ilXcache';
271 break;
272 default:
273 return 'ilStaticCache';
274 break;
275 }
276 }
277
278
282 protected static $active_cache = array();
283
284
288 public function isActive()
289 {
290 if (self::$active_cache[$this->getComponent()] !== null) {
291 return self::$active_cache[$this->getComponent()];
292 }
293 if (!self::ACTIVE) {
294 self::$active_cache[$this->getComponent()] = false;
295
296 return false;
297 }
298 if (!$this->getActive()) {
299 self::log($this->getComponent() . '-wrapper is inactive...', ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
300 self::$active_cache[$this->getComponent()] = false;
301
302 return false;
303 }
304
305 $isActive = $this->global_cache->isActive();
306 self::log('component ' . $this->getComponent() . ', service is active: '
307 . ($isActive ? 'yes' : 'no'), ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
308 self::$active_cache[$this->getComponent()] = $isActive;
309
310 return $isActive;
311 }
312
313
319 public function isValid($key)
320 {
321 return $this->global_cache->isValid($key);
322 }
323
324
328 public function isInstallable()
329 {
330 return count(self::getAllInstallableTypes()) > 0;
331 }
332
333
338 {
339 return $this->global_cache->isInstallable();
340 }
341
342
347 {
348 return $this->global_cache->getInstallationFailureReason();
349 }
350
351
358 public function exists($key)
359 {
360 if (!$this->global_cache->isActive()) {
361 return false;
362 }
363
364 return $this->global_cache->exists($key);
365 }
366
367
376 public function set($key, $value, $ttl = null)
377 {
378 if (!$this->isActive()) {
379 return false;
380 }
381 self::log($key . ' set in component ' . $this->getComponent(), ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
382 $this->global_cache->setValid($key);
383
384 return $this->global_cache->set($key, $this->global_cache->serialize($value), $ttl);
385 }
386
387
394 public function get($key)
395 {
396 if (!$this->isActive()) {
397 return false;
398 }
399 $unserialized_return = $this->global_cache->unserialize($this->global_cache->get($key));
400 if ($unserialized_return) {
401 $service_name = ' [' . self::lookupServiceClassName($this->getServiceType()) . ']';
402 if ($this->global_cache->isValid($key)) {
403 self::log($key . ' from component ' . $this->getComponent() . $service_name, ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
404
405 return $unserialized_return;
406 } else {
407 self::log($key . ' from component ' . $this->getComponent() . ' is invalid' . $service_name, ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
408 }
409 }
410
411 return null;
412 }
413
414
420 public function delete($key)
421 {
422 if (!$this->isActive()) {
423 return false;
424 }
425
426 return $this->global_cache->delete($key);
427 }
428
429
436 public function flush($complete = false)
437 {
438 if ($this->global_cache->isActive()) {
439 return $this->global_cache->flush();
440 }
441
442 return false;
443 }
444
445
446 public function getInfo()
447 {
448 return $this->global_cache->getInfo();
449 }
450
451
455 public function setComponent($component)
456 {
457 $this->component = $component;
458 }
459
460
464 public function getComponent()
465 {
466 return $this->component;
467 }
468
469
473 public function setActive($active)
474 {
475 $this->active = $active;
476 }
477
478
482 public function getActive()
483 {
484 return $this->active;
485 }
486
487
492 {
493 if ($this->global_cache instanceof ilGlobalCacheService) {
494 $this->global_cache->setServiceType($service_type);
495 }
496 $this->service_type = $service_type;
497 }
498
499
503 public function getServiceType()
504 {
505 if ($this->global_cache instanceof ilGlobalCacheService) {
506 return $this->global_cache->getServiceType();
507 }
508
509 return $this->service_type;
510 }
511
512
516 public static function getSettings()
517 {
518 return (self::$settings instanceof ilGlobalCacheSettings ? self::$settings : new ilGlobalCacheSettings());
519 }
520
521
525 public static function setSettings($settings)
526 {
527 self::$settings = $settings;
528 }
529
530
534 public static function getActiveComponents()
535 {
537 }
538
539
544 {
545 self::$active_components = $active_components;
546 }
547
548
552 public static function getAvailableComponents()
553 {
555 }
556
557
562 {
563 self::$available_components = $available_components;
564 }
565}
$function
Definition: cas.php:28
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
$service
Definition: login.php:15
catch(Exception $e) $message
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27
$type
global $DIC
Definition: saml.php:7