ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilGlobalCache.php
Go to the documentation of this file.
1 <?php
2 
25 {
29  public const MSG = 'Global Cache not active, can not access cache';
33  public const ACTIVE = true;
37  public const TYPE_STATIC = 0;
41  public const TYPE_MEMCACHED = 2;
45  public const TYPE_APC = 3;
49  public const TYPE_FALLBACK = self::TYPE_STATIC;
53  public const COMP_CLNG = 'clng';
57  public const COMP_OBJ_DEF = 'obj_def';
61  public const COMP_TEMPLATE = 'tpl';
65  public const COMP_ILCTRL = 'ilctrl';
69  public const COMP_PLUGINS = 'plugins';
73  public const COMP_RBAC_UA = 'rbac_ua';
77  public const COMP_EVENTS = 'events';
81  public const COMP_TPL_BLOCKS = 'tpl_blocks';
85  public const COMP_TPL_VARIABLES = 'tpl_variables';
89  public const COMP_GLOBAL_SCREEN = 'global_screen';
90  protected static array $types = array(
91  self::TYPE_MEMCACHED,
92  self::TYPE_APC,
93  self::TYPE_STATIC,
94  );
95  protected static array $available_types = array(
96  self::TYPE_MEMCACHED,
97  self::TYPE_APC,
98  self::TYPE_STATIC,
99  );
100  protected static array $active_components = array();
101  protected static array $available_components = array(
102  self::COMP_CLNG,
103  self::COMP_OBJ_DEF,
104  self::COMP_ILCTRL,
105  self::COMP_TEMPLATE,
106  self::COMP_TPL_BLOCKS,
107  self::COMP_TPL_VARIABLES,
108  self::COMP_EVENTS,
109  self::COMP_GLOBAL_SCREEN,
110  );
111  protected static array $type_per_component = array();
112  protected static ?string $unique_service_id = null;
113  protected static ?array $instances = null;
114  protected \ilGlobalCacheService $global_cache;
115  protected ?string $component = null;
116  protected bool $active = true;
118  protected static ?\ilGlobalCacheSettings $settings = null;
119 
120  public static function setup(ilGlobalCacheSettings $ilGlobalCacheSettings): void
121  {
122  self::setSettings($ilGlobalCacheSettings);
123  self::setActiveComponents($ilGlobalCacheSettings->getActivatedComponents());
124  }
125 
126  public static function getInstance(?string $component): \ilGlobalCache
127  {
128  if (!isset(self::$instances[$component])) {
129  $service_type = self::getSettings()->getService();
130  $ilGlobalCache = new self($service_type);
131  $ilGlobalCache->setComponent($component);
132  $ilGlobalCache->initCachingService();
133 
134  self::$instances[$component] = $ilGlobalCache;
135  }
136 
137  return self::$instances[$component];
138  }
139 
140  protected function __construct(int $service_type)
141  {
142  self::generateServiceId();
143  $this->setServiceType($service_type);
144  }
145 
146  protected function initCachingService(): void
147  {
151  if ($this->getComponent() === '') {
152  $this->setComponent('default');
153  }
154  $serviceName = self::lookupServiceClassName($this->getServiceType());
155  $ilGlobalCacheService = new $serviceName(self::$unique_service_id, $this->getComponent());
156  $ilGlobalCacheService->setServiceType($this->getServiceType());
157 
158  $this->global_cache = $ilGlobalCacheService;
159  $this->setActive(in_array($this->getComponent(), self::getActiveComponents()));
160  }
161 
162  protected function checkSettings(): void
163  {
164  }
165 
166  public static function log(string $message, int $log_level): void
167  {
168  if ($log_level <= self::getSettings()->getLogLevel()) {
169  global $DIC;
170  $ilLog = $DIC[\ilLog::class];
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  protected static function generateServiceId(): string
181  {
182  if (!isset(self::$unique_service_id)) {
183  $raw_service_id = '_';
184  if (defined('CLIENT_ID')) {
185  $raw_service_id .= 'il_' . CLIENT_ID;
186  }
187  self::$unique_service_id = substr(md5($raw_service_id), 0, 6);
188  }
189  return self::$unique_service_id;
190  }
191 
192  public static function flushAll(): void
193  {
194  self::log('requested...', ilGlobalCacheSettings::LOG_LEVEL_NORMAL);
198  foreach (self::$types as $type) {
199  $serviceName = self::lookupServiceClassName($type);
200  $service = new $serviceName(self::generateServiceId(), 'flush');
201  if ($service->isActive()) {
202  self::log('Told ' . $serviceName . ' to flush', ilGlobalCacheSettings::LOG_LEVEL_NORMAL);
203  $returned = $service->flush();
204  self::log(
205  $serviceName . ' returned status ' . ($returned ? 'ok' : 'failure'),
207  );
208  }
209  }
210  }
211 
212  public static function getAllInstallableTypes(): array
213  {
214  $types = array();
215  foreach (self::getAllTypes() as $type) {
216  if ($type->isCacheServiceInstallable()) {
217  $types[] = $type;
218  }
219  }
220 
221  return $types;
222  }
223 
224  public static function getAllTypes(bool $only_available = true): array
225  {
226  $types = array();
227  foreach (self::$types as $type) {
228  if ($only_available && !in_array($type, self::$available_types)) {
229  continue;
230  }
231  $obj = new self($type);
232  $obj->initCachingService();
233  $types[$type] = $obj;
234  }
235 
236  return $types;
237  }
238 
239  public static function lookupServiceClassName(int $service_type): string
240  {
241  switch ($service_type) {
242  case self::TYPE_APC:
243  return ilApc::class;
244  case self::TYPE_MEMCACHED:
245  return ilMemcache::class;
246  default:
247  return ilStaticCache::class;
248  }
249  }
250 
251  public static function lookupServiceConfigName(int $service_type): string
252  {
253  switch ($service_type) {
254  case self::TYPE_APC:
255  return 'apc';
256  case self::TYPE_MEMCACHED:
257  return 'memcached';
258  default:
259  return 'static';
260  }
261  }
262 
263  protected static array $active_cache = array();
264 
265  public function isActive(): bool
266  {
267  $c = $this->getComponent();
268  if (isset(self::$active_cache[$c]) && self::$active_cache[$c] !== null) {
269  return self::$active_cache[$c];
270  }
271  if (!self::ACTIVE) {
272  self::$active_cache[$c] = false;
273 
274  return false;
275  }
276  if (!$this->getActive()) {
277  self::log($c . '-wrapper is inactive...', ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
278  self::$active_cache[$c] = false;
279 
280  return false;
281  }
282 
283  $isActive = $this->global_cache->isActive();
284  self::log('component ' . $c . ', service is active: '
285  . ($isActive ? 'yes' : 'no'), ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
286  self::$active_cache[$c] = $isActive;
287 
288  return $isActive;
289  }
290 
291  public function isValid(string $key): bool
292  {
293  return $this->global_cache->isValid($key);
294  }
295 
296  public function isInstallable(): bool
297  {
298  return count(self::getAllInstallableTypes()) > 0;
299  }
300 
301  public function isCacheServiceInstallable(): bool
302  {
303  return $this->global_cache->isInstallable();
304  }
305 
306  public function getInstallationFailureReason(): string
307  {
308  return $this->global_cache->getInstallationFailureReason();
309  }
310 
314  public function exists(string $key): bool
315  {
316  if (!$this->global_cache->isActive()) {
317  return false;
318  }
319 
320  return $this->global_cache->exists($key);
321  }
322 
327  public function set(string $key, $value, int $ttl = null): bool
328  {
329  if (!$this->isActive()) {
330  return false;
331  }
332  self::log($key . ' set in component ' . $this->getComponent(), ilGlobalCacheSettings::LOG_LEVEL_CHATTY);
333  $this->global_cache->setValid($key);
334 
335  return $this->global_cache->set($key, $this->global_cache->serialize($value), $ttl);
336  }
337 
342  public function get(string $key)
343  {
344  if (!$this->isActive()) {
345  return false;
346  }
347  $unserialized_return = $this->global_cache->unserialize($this->global_cache->get($key));
348  if ($unserialized_return) {
349  $service_name = ' [' . self::lookupServiceClassName($this->getServiceType()) . ']';
350  if ($this->global_cache->isValid($key)) {
351  self::log(
352  $key . ' from component ' . $this->getComponent() . $service_name,
354  );
355 
356  return $unserialized_return;
357  } else {
358  self::log(
359  $key . ' from component ' . $this->getComponent() . ' is invalid' . $service_name,
361  );
362  }
363  }
364 
365  return null;
366  }
367 
368  public function delete(string $key): bool
369  {
370  if (!$this->isActive()) {
371  return false;
372  }
373 
374  return $this->global_cache->delete($key);
375  }
376 
380  public function flush(bool $complete = false): bool
381  {
382  if ($this->global_cache->isActive()) {
383  return $this->global_cache->flush($complete);
384  }
385 
386  return false;
387  }
388 
389  public function getInfo(): array
390  {
391  return $this->global_cache->getInfo();
392  }
393 
394  public function setComponent(string $component): void
395  {
396  $this->component = $component;
397  }
398 
399  public function getComponent(): ?string
400  {
401  return $this->component;
402  }
403 
404  public function setActive(bool $active): void
405  {
406  $this->active = $active;
407  }
408 
409  public function getActive(): bool
410  {
411  return $this->active;
412  }
413 
414  public function setServiceType(int $service_type): void
415  {
416  $this->service_type = $service_type;
417  }
418 
419  public function getServiceType(): int
420  {
421  return $this->service_type;
422  }
423 
424  public static function getSettings(): ilGlobalCacheSettings
425  {
427  }
428 
429  public static function setSettings(ilGlobalCacheSettings $settings): void
430  {
432  }
433 
434  public static function getActiveComponents(): array
435  {
436  return self::$active_components;
437  }
438 
439  public static function setActiveComponents(array $active_components): void
440  {
441  self::$active_components = $active_components;
442  }
443 
444  public static function getAvailableComponents(): array
445  {
446  return self::$available_components;
447  }
448 
449  public static function setAvailableComponents(array $available_components): void
450  {
451  self::$available_components = $available_components;
452  }
453 }
static array $types
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
setServiceType(int $service_type)
$c
Definition: cli.php:38
if(empty($path)) $serviceName
Definition: ltiservices.php:38
static setActiveComponents(array $active_components)
$type
static setAvailableComponents(array $available_components)
static getAllInstallableTypes()
isValid(string $key)
Class ilGlobalCacheSettings.
static setup(ilGlobalCacheSettings $ilGlobalCacheSettings)
static array $available_components
flush(bool $complete=false)
Component logger with individual log levels by component id.
static log(string $message, int $log_level)
global $DIC
Definition: feed.php:28
static array $instances
static array $active_cache
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getSettings()
Get an array of all setting values.
Definition: System.php:287
setActive(bool $active)
static lookupServiceClassName(int $service_type)
static getAvailableComponents()
const CLIENT_ID
Definition: constants.php:41
ilGlobalCacheService $global_cache
string $key
Consumer key/client ID value.
Definition: System.php:193
static array $type_per_component
static getAllTypes(bool $only_available=true)
static lookupServiceConfigName(int $service_type)
__construct(int $service_type)
static ilGlobalCacheSettings $settings
setSettings(array $settings)
Set an array of all setting values.
Definition: System.php:296
static getActiveComponents()
static array $active_components
static array $available_types
$message
Definition: xapiexit.php:32
setComponent(string $component)
static setSettings(ilGlobalCacheSettings $settings)
static getInstance(?string $component)
$service
Definition: ltiservices.php:43
static string $unique_service_id