ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 $active_components = array();
49 protected static $available_components = array(
50 self::COMP_CLNG,
51 self::COMP_OBJ_DEF,
52 self::COMP_ILCTRL,
53 self::COMP_COMPONENT,
54 self::COMP_TEMPLATE,
55 self::COMP_TPL_BLOCKS,
56 self::COMP_TPL_VARIABLES,
57 self::COMP_EVENTS,
58 );
62 protected static $type_per_component = array();
66 protected static $unique_service_id = NULL;
70 protected static $instances;
74 protected $global_cache;
78 protected $component;
82 protected $active = true;
90 protected static $settings;
91
92
96 public static function setup(ilGlobalCacheSettings $ilGlobalCacheSettings) {
97 self::setSettings($ilGlobalCacheSettings);
98 self::setActiveComponents($ilGlobalCacheSettings->getActivatedComponents());
99 }
100
101
107 public static function getInstance($component) {
108 if (! isset(self::$instances[$component])) {
109 $service_type = self::getSettings()->getService();
110 $ilGlobalCache = new self($service_type);
111 $ilGlobalCache->setComponent($component);
112 $ilGlobalCache->initCachingService();
113
114 self::$instances[$component] = $ilGlobalCache;
115 }
116
117 return self::$instances[$component];
118 }
119
120
124 protected function __construct($service_type) {
125 $this->checkSettings();
128 }
129
130
131 protected function initCachingService() {
135 if (! $this->getComponent()) {
136 $this->setComponent('default');
137 }
138 $serviceName = self::lookupServiceClassName($this->getServiceType());
139 $ilGlobalCacheService = new $serviceName(self::$unique_service_id, $this->getComponent());
140 $ilGlobalCacheService->setServiceType($this->getServiceType());
141 $this->global_cache = $ilGlobalCacheService;
142 $this->setActive(in_array($this->getComponent(), self::getActiveComponents()));
143 }
144
145
146 protected function checkSettings() {
147 if (! $this->getSettings() instanceof ilGlobalCacheSettings) {
148
149 $ilGlobalCacheSettings = new ilGlobalCacheSettings();
150 $this->setSettings($ilGlobalCacheSettings);
151 }
152 }
153
154
158 public static function log($message, $log_level) {
159 if ($log_level <= self::getSettings()->getLogLevel()) {
160 global $ilLog;
161 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
162 $function = $backtrace[1]['function'];
163 $class = $backtrace[1]['class'];
164 if ($ilLog instanceof ilLog) {
165 $ilLog->write($class . '::' . $function . '(): ' . $message);
166 }
167 }
168 }
169
170
174 protected static function generateServiceId() {
175 if (! isset(self::$unique_service_id)) {
176 self::$unique_service_id = substr(md5('il_' . CLIENT_ID), 0, 6);
177 }
178 }
179
180
181 public static function flushAll() {
186 foreach (self::$types as $type) {
187 $serviceName = self::lookupServiceClassName($type);
188 $service = new $serviceName(self::generateServiceId(), 'flush');
189 if ($service->isActive()) {
190 self::log('Told ' . $serviceName . ' to flush', ilGlobalCacheSettings::LOG_LEVEL_NORMAL);
191 $returned = $service->flush();
192 self::log($serviceName . ' returned status ' . ($returned ? 'ok' : 'failure'), ilGlobalCacheSettings::LOG_LEVEL_NORMAL);
193 }
194 }
195 }
196
197
201 public static function getAllInstallableTypes() {
202 $types = array();
203 foreach (self::getAllTypes() as $type) {
204 if ($type->isCacheServiceInstallable()) {
205 $types[] = $type;
206 }
207 }
208
209 return $types;
210 }
211
212
216 public static function getAllTypes() {
217 $types = array();
218 foreach (self::$types as $type) {
219 $obj = new self($type);
220 $obj->initCachingService();
221 $types[$type] = $obj;
222 }
223
224 return $types;
225 }
226
227
233 public static function lookupServiceClassName($service_type) {
234 switch ($service_type) {
235 case self::TYPE_APC:
236 return 'ilApc';
237 break;
239 return 'ilMemcache';
240 break;
242 return 'ilXcache';
243 break;
244 default:
245 return 'ilStaticCache';
246 break;
247 }
248 }
249
250
254 protected static $active_cache = array();
255
256
260 public function isActive() {
261 if (self::$active_cache[$this->getComponent()] !== NULL) {
262 return self::$active_cache[$this->getComponent()];
263 }
264 if (! self::ACTIVE) {
265 self::$active_cache[$this->getComponent()] = false;
266
267 return false;
268 }
269 if (! $this->getActive()) {
270 self::log($this->getComponent() . '-wrapper is inactive...', ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
271 self::$active_cache[$this->getComponent()] = false;
272
273 return false;
274 }
275
276 $isActive = $this->global_cache->isActive();
277 self::log('component ' . $this->getComponent() . ', service is active: '
278 . ($isActive ? 'yes' : 'no'), ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
279 self::$active_cache[$this->getComponent()] = $isActive;
280
281 return $isActive;
282 }
283
284
290 public function isValid($key) {
291 return $this->global_cache->isValid($key);
292 }
293
294
298 public function isInstallable() {
299 return count(self::getAllInstallableTypes()) > 0;
300 }
301
302
306 public function isCacheServiceInstallable() {
307 return $this->global_cache->isInstallable();
308 }
309
310
315 return $this->global_cache->getInstallationFailureReason();
316 }
317
318
325 public function exists($key) {
326 if (! $this->global_cache->isActive()) {
327 return false;
328 }
329
330 return $this->global_cache->exists($key);
331 }
332
333
342 public function set($key, $value, $ttl = NULL) {
343 if (! $this->isActive()) {
344 return false;
345 }
346 self::log($key . ' set in component ' . $this->getComponent(), ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
347 $this->global_cache->setValid($key);
348
349 return $this->global_cache->set($key, $this->global_cache->serialize($value), $ttl);
350 }
351
352
359 public function get($key) {
360 if (! $this->isActive()) {
361 return false;
362 }
363 $unserialized_return = $this->global_cache->unserialize($this->global_cache->get($key));
364 if ($unserialized_return) {
365 $service_name = ' [' . self::lookupServiceClassName($this->getServiceType()) . ']';
366 if ($this->global_cache->isValid($key)) {
367 self::log($key . ' from component ' . $this->getComponent() . $service_name, ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
368
369 return $unserialized_return;
370 } else {
371 self::log($key . ' from component ' . $this->getComponent() . ' is invalid' . $service_name, ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
372 }
373 }
374
375 return NULL;
376 }
377
378
384 public function delete($key) {
385 if (! $this->isActive()) {
386 return false;
387 }
388
389 return $this->global_cache->delete($key);
390 }
391
392
399 public function flush($complete = false) {
400 if ($this->global_cache->isActive()) {
401 if ($complete) {
402 return $this->global_cache->flush();
403 } else {
404 $this->global_cache->setInvalid();
405 }
406 }
407
408 return false;
409 }
410
411
412 public function getInfo() {
413 return $this->global_cache->getInfo();
414 }
415
416
420 public function setComponent($component) {
421 $this->component = $component;
422 }
423
424
428 public function getComponent() {
429 return $this->component;
430 }
431
432
436 public function setActive($active) {
437 $this->active = $active;
438 }
439
440
444 public function getActive() {
445 return $this->active;
446 }
447
448
452 public function setServiceType($service_type) {
453 if ($this->global_cache instanceof ilGlobalCacheService) {
454 $this->global_cache->setServiceType($service_type);
455 }
456 $this->service_type = $service_type;
457 }
458
459
463 public function getServiceType() {
464 if ($this->global_cache instanceof ilGlobalCacheService) {
465 return $this->global_cache->getServiceType();
466 }
467
468 return $this->service_type;
469 }
470
471
475 public static function getSettings() {
476 return (self::$settings instanceof ilGlobalCacheSettings ? self::$settings : new ilGlobalCacheSettings());
477 }
478
479
483 public static function setSettings($settings) {
484 self::$settings = $settings;
485 }
486
487
491 public static function getActiveComponents() {
493 }
494
495
499 public static function setActiveComponents($active_components) {
500 self::$active_components = $active_components;
501 }
502
503
507 public static function getAvailableComponents() {
509 }
510
511
516 self::$available_components = $available_components;
517 }
518}
519
520?>
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 setActiveComponents($active_components)
static getActiveComponents()
static log($message, $log_level)
__construct($service_type)
setComponent($component)
static getAvailableComponents()
static getInstance($component)
logging
Definition: class.ilLog.php:19