ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 self::setSettings($ilGlobalCacheSettings);
107 self::setActiveComponents($ilGlobalCacheSettings->getActivatedComponents());
108 }
109
110
116 public static function getInstance($component) {
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
129
133 protected function __construct($service_type) {
134 $this->checkSettings();
137 }
138
139
140 protected function initCachingService() {
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
155 protected function checkSettings() {
156 if (!$this->getSettings() instanceof ilGlobalCacheSettings) {
157
158 $ilGlobalCacheSettings = new ilGlobalCacheSettings();
159 $this->setSettings($ilGlobalCacheSettings);
160 }
161 }
162
163
167 public static function log($message, $log_level) {
168 if ($log_level <= self::getSettings()->getLogLevel()) {
169 global $DIC;
170 $ilLog = $DIC['ilLog'];
171 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
172 $function = $backtrace[1]['function'];
173 $class = $backtrace[1]['class'];
174 if ($ilLog instanceof ilComponentLogger) {
175 $ilLog->alert($class . '::' . $function . '(): ' . $message);
176 }
177 }
178 }
179
180
184 protected static function generateServiceId() {
185 if (!isset(self::$unique_service_id)) {
186 self::$unique_service_id = substr(md5('il_' . CLIENT_ID), 0, 6);
187 }
188 }
189
190
191 public static function flushAll() {
196 foreach (self::$types as $type) {
197 $serviceName = self::lookupServiceClassName($type);
198 $service = new $serviceName(self::generateServiceId(), 'flush');
199 if ($service->isActive()) {
200 self::log('Told ' . $serviceName . ' to flush', ilGlobalCacheSettings::LOG_LEVEL_NORMAL);
201 $returned = $service->flush();
202 self::log($serviceName . ' returned status ' . ($returned ? 'ok' : 'failure'), ilGlobalCacheSettings::LOG_LEVEL_NORMAL);
203 }
204 }
205 }
206
207
211 public static function getAllInstallableTypes() {
212 $types = array();
213 foreach (self::getAllTypes() as $type) {
214 if ($type->isCacheServiceInstallable()) {
215 $types[] = $type;
216 }
217 }
218
219 return $types;
220 }
221
222
227 public static function getAllTypes($only_available = true) {
228 $types = array();
229 foreach (self::$types as $type) {
230 if ($only_available && !in_array($type, self::$available_types)) {
231 continue;
232 }
233 $obj = new self($type);
234 $obj->initCachingService();
235 $types[$type] = $obj;
236 }
237
238 return $types;
239 }
240
241
247 public static function lookupServiceClassName($service_type) {
248 switch ($service_type) {
249 case self::TYPE_APC:
250 return 'ilApc';
251 break;
253 return 'ilMemcache';
254 break;
256 return 'ilXcache';
257 break;
258 default:
259 return 'ilStaticCache';
260 break;
261 }
262 }
263
264
268 protected static $active_cache = array();
269
270
274 public function isActive() {
275 if (self::$active_cache[$this->getComponent()] !== null) {
276 return self::$active_cache[$this->getComponent()];
277 }
278 if (!self::ACTIVE) {
279 self::$active_cache[$this->getComponent()] = false;
280
281 return false;
282 }
283 if (!$this->getActive()) {
284 self::log($this->getComponent() . '-wrapper is inactive...', ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
285 self::$active_cache[$this->getComponent()] = false;
286
287 return false;
288 }
289
290 $isActive = $this->global_cache->isActive();
291 self::log('component ' . $this->getComponent() . ', service is active: '
292 . ($isActive ? 'yes' : 'no'), ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
293 self::$active_cache[$this->getComponent()] = $isActive;
294
295 return $isActive;
296 }
297
298
304 public function isValid($key) {
305 return $this->global_cache->isValid($key);
306 }
307
308
312 public function isInstallable() {
313 return count(self::getAllInstallableTypes()) > 0;
314 }
315
316
320 public function isCacheServiceInstallable() {
321 return $this->global_cache->isInstallable();
322 }
323
324
329 return $this->global_cache->getInstallationFailureReason();
330 }
331
332
339 public function exists($key) {
340 if (!$this->global_cache->isActive()) {
341 return false;
342 }
343
344 return $this->global_cache->exists($key);
345 }
346
347
356 public function set($key, $value, $ttl = null) {
357 if (!$this->isActive()) {
358 return false;
359 }
360 self::log($key . ' set in component ' . $this->getComponent(), ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
361 $this->global_cache->setValid($key);
362
363 return $this->global_cache->set($key, $this->global_cache->serialize($value), $ttl);
364 }
365
366
373 public function get($key) {
374 if (!$this->isActive()) {
375 return false;
376 }
377 $unserialized_return = $this->global_cache->unserialize($this->global_cache->get($key));
378 if ($unserialized_return) {
379 $service_name = ' [' . self::lookupServiceClassName($this->getServiceType()) . ']';
380 if ($this->global_cache->isValid($key)) {
381 self::log($key . ' from component ' . $this->getComponent() . $service_name, ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
382
383 return $unserialized_return;
384 } else {
385 self::log($key . ' from component ' . $this->getComponent() . ' is invalid' . $service_name, ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
386 }
387 }
388
389 return null;
390 }
391
392
398 public function delete($key) {
399 if (!$this->isActive()) {
400 return false;
401 }
402
403 return $this->global_cache->delete($key);
404 }
405
406
413 public function flush($complete = false) {
414 if ($this->global_cache->isActive()) {
415 if ($complete) {
416 return $this->global_cache->flush();
417 } else {
418 $this->global_cache->setInvalid();
419 }
420 }
421
422 return false;
423 }
424
425
426 public function getInfo() {
427 return $this->global_cache->getInfo();
428 }
429
430
434 public function setComponent($component) {
435 $this->component = $component;
436 }
437
438
442 public function getComponent() {
443 return $this->component;
444 }
445
446
450 public function setActive($active) {
451 $this->active = $active;
452 }
453
454
458 public function getActive() {
459 return $this->active;
460 }
461
462
466 public function setServiceType($service_type) {
467 if ($this->global_cache instanceof ilGlobalCacheService) {
468 $this->global_cache->setServiceType($service_type);
469 }
470 $this->service_type = $service_type;
471 }
472
473
477 public function getServiceType() {
478 if ($this->global_cache instanceof ilGlobalCacheService) {
479 return $this->global_cache->getServiceType();
480 }
481
482 return $this->service_type;
483 }
484
485
489 public static function getSettings() {
490 return (self::$settings instanceof ilGlobalCacheSettings ? self::$settings : new ilGlobalCacheSettings());
491 }
492
493
497 public static function setSettings($settings) {
498 self::$settings = $settings;
499 }
500
501
505 public static function getActiveComponents() {
507 }
508
509
513 public static function setActiveComponents($active_components) {
514 self::$active_components = $active_components;
515 }
516
517
521 public static function getAvailableComponents() {
523 }
524
525
530 self::$available_components = $available_components;
531 }
532}
533
534?>
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)
global $DIC