ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.arObjectCache.php
Go to the documentation of this file.
1 <?php
2 require_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 arObjectCache.
Class CachedActiveRecord.
Class ActiveRecord.
const GET_UNCACHED_OBJECT
if(!array_key_exists('StateId', $_REQUEST)) $id
static purge(ActiveRecord $object)
static store(ActiveRecord $object)
static flush($class_name)
static isCached($class, $id)
Class arException.