From b09c4f8ef06643cf01a364845dbc80a91414afb2 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 30 Jun 2016 10:34:34 +0200 Subject: [PATCH] Db/Cache: first step to externalize lookup caches --- .../Director/Db/Cache/CustomVariableCache.php | 65 +++++++++++++ library/Director/Db/Cache/PrefetchCache.php | 97 +++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 library/Director/Db/Cache/CustomVariableCache.php create mode 100644 library/Director/Db/Cache/PrefetchCache.php diff --git a/library/Director/Db/Cache/CustomVariableCache.php b/library/Director/Db/Cache/CustomVariableCache.php new file mode 100644 index 00000000..2041951f --- /dev/null +++ b/library/Director/Db/Cache/CustomVariableCache.php @@ -0,0 +1,65 @@ +getConnection()->getDbAdapter(); + + $query = $db->select()->from( + array('v' => $object->getVarsTableName()), + array( + 'id' => sprintf('v.%s', $object->getVarsIdColumn()), + 'varname' => 'v.varname', + 'varvalue' => 'v.varvalue', + 'format' => 'v.format', + ) + ); + + foreach ($db->fetchAll($query) as $row) { + + $id = $row->id; + unset($row->id); + + if (array_key_exists($id, $this->rowsById)) { + $this->rowsById[$id][] = $row; + } else { + $this->rowsById[$id] = array($row); + } + } + } + + public function getVarsForObject(IcingaObject $object) + { + $id = $object->id; + + if (array_key_exists($id, $this->rowsById)) { + if (! array_key_exists($id, $this->varsById)) { + $this->varsById[$id] = CustomVariables::forStoredRows( + $this->rowsById[$id] + ); + } + + return $this->varsById[$id]; + } else { + return new CustomVariables(); + } + } + + public function __destruct() + { + unset($this->db); + } +} diff --git a/library/Director/Db/Cache/PrefetchCache.php b/library/Director/Db/Cache/PrefetchCache.php new file mode 100644 index 00000000..512dfa62 --- /dev/null +++ b/library/Director/Db/Cache/PrefetchCache.php @@ -0,0 +1,97 @@ +db = $db; + } + + public static function instance() + { + if (static::$instance === null) { + throw new ProgrammingError('Prefetch cache has not been loaded'); + } + + return static::$instance; + } + + public static function forget() + { + self::$instance = null; + } + + public static function shouldBeUsed() + { + return self::$instance !== null; + } + + public function vars(IcingaObject $object) + { + return $this->varsCache($object)->getVarsForObject($object); + } + + public function groups(IcingaObject $object) + { + return $this->groupsCache($object)->getGroupsForObject($object); + } + + public function byObjectType($type) + { + if (! array_key_exists($type, $this->caches)) { + $this->caches[$type] = new ObjectCache($type); + } + + return $this->caches[$type]; + } + + protected function varsCache(IcingaObject $object) + { + $key = $object->getShortTableName(); + + if (! array_key_exists($key, $this->varsCaches)) { + $this->varsCaches[$key] = new CustomVariableCache($object); + } + + return $this->varsCaches[$key]; + } + + protected function groupsCache(IcingaObject $object) + { + $key = $object->getShortTableName(); + + if (! array_key_exists($key, $this->groupsCaches)) { + $this->groupsCaches[$key] = new GroupMembershipCache($object); + } + + return $this->groupsCaches[$key]; + } + + public function __destruct() + { + unset($this->caches); + unset($this->groupsCaches); + unset($this->varsCaches); + } +}