ILIAS  trunk Revision v11.0_alpha-2658-ge2404539063
class.arObjectCache.php
Go to the documentation of this file.
1 <?php
2 
25 {
26  protected static array $cache = [];
27 
32  public static function isCached($class, $id): bool
33  {
34  new $class();
35 
36  if (!isset(self::$cache[$class])) {
37  return false;
38  }
39  if (!isset(self::$cache[$class][$id])) {
40  return false;
41  }
42  if (!self::$cache[$class][$id] instanceof ActiveRecord) {
43  return false;
44  }
45 
46  return array_key_exists($id, self::$cache[$class]);
47  }
48 
49  public static function store(ActiveRecord $activeRecord): void
50  {
51  if (!isset($activeRecord->is_new)) {
52  self::$cache[$activeRecord::class][$activeRecord->getPrimaryFieldValue()] = $activeRecord;
53  }
54  }
55 
56  public static function printStats(): void
57  {
58  foreach (self::$cache as $class => $objects) {
59  echo $class;
60  echo ": ";
61  echo is_countable($objects) ? count($objects) : 0;
62  echo " Objects<br>";
63  }
64  }
65 
71  public static function get(string $class, string $id): \ActiveRecord
72  {
73  new $class();
74  if (!self::isCached($class, $id)) {
75  throw new arException(arException::GET_UNCACHED_OBJECT, $class . ': ' . $id);
76  }
77 
78  return self::$cache[$class][$id];
79  }
80 
81  public static function purge(ActiveRecord $activeRecord): void
82  {
83  unset(self::$cache[$activeRecord::class][$activeRecord->getPrimaryFieldValue()]);
84  }
85 
89  public static function flush($class_name): void
90  {
91  new $class_name();
92 
93  if ($class_name instanceof ActiveRecord) {
94  $class_name = $class_name::class;
95  }
96  unset(self::$cache[$class_name]);
97  }
98 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const GET_UNCACHED_OBJECT
static store(ActiveRecord $activeRecord)
static flush($class_name)
static isCached($class, $id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static array $cache
static purge(ActiveRecord $activeRecord)