ILIAS  release_7 Revision v7.30-3-g800a261c036
class.arObjectCache.php
Go to the documentation of this file.
1<?php
2require_once(dirname(__FILE__) . '/../Exception/class.arException.php');
3
12{
13
17 protected static $cache = array();
18
19
26 public static function isCached($class, $id)
27 {
28 $instance = new $class();
29 if ($instance instanceof CachedActiveRecord && $instance->getCacheIdentifier() != '') {
30 if ($instance->getCache()->exists($instance->getCacheIdentifier())) {
31 return true;
32 }
33 }
34
35 if (!isset(self::$cache[$class])) {
36 return false;
37 }
38 if (!isset(self::$cache[$class][$id]) || !self::$cache[$class][$id] instanceof ActiveRecord) {
39 return false;
40 }
41
42 return in_array($id, array_keys(self::$cache[$class]));
43 }
44
45
49 public static function store(ActiveRecord $object)
50 {
51 if ($object instanceof CachedActiveRecord && $object->getCacheIdentifier() != '') {
52 if ($object->getCache()->set($object->getCacheIdentifier(), $object, $object->getTTL())) {
53 return;
54 }
55 }
56 if (!isset($object->is_new)) {
57 self::$cache[get_class($object)][$object->getPrimaryFieldValue()] = $object;
58 }
59 }
60
61
62 public static function printStats()
63 {
64 foreach (self::$cache as $class => $objects) {
65 echo $class;
66 echo ": ";
67 echo count($objects);
68 echo " Objects<br>";
69 }
70 }
71
72
80 public static function get($class, $id)
81 {
82 $instance = new $class();
83 if ($instance instanceof CachedActiveRecord && $instance->getCacheIdentifier() != '') {
84 if ($instance->getCache()->exists($instance->getCacheIdentifier())) {
85 return $instance->getCache()->get($instance->getCacheIdentifier());
86 }
87 }
88 if (!self::isCached($class, $id)) {
89 throw new arException(arException::GET_UNCACHED_OBJECT, $class . ': ' . $id);
90 }
91
92 return self::$cache[$class][$id];
93 }
94
95
99 public static function purge(ActiveRecord $object)
100 {
101 if ($object instanceof CachedActiveRecord && $object->getCacheIdentifier() != '') {
102 $object->getCache()->delete($object->getCacheIdentifier());
103 }
104 unset(self::$cache[get_class($object)][$object->getPrimaryFieldValue()]);
105 }
106
107
111 public static function flush($class_name)
112 {
113 $instance = new $class_name();
114 if ($instance instanceof CachedActiveRecord && $instance->getCacheIdentifier() != '') {
115 $instance->getCache()->flush();
116 }
117
118 if ($class_name instanceof ActiveRecord) {
119 $class_name = get_class($class_name);
120 }
121 unset(self::$cache[$class_name]);
122 }
123}
Class ActiveRecord.
An exception for terminatinating execution or to throw for unit testing.
Class CachedActiveRecord.
Class arException.
const GET_UNCACHED_OBJECT
Class arObjectCache.
static isCached($class, $id)
static store(ActiveRecord $object)
static purge(ActiveRecord $object)
static flush($class_name)