From f848a170d5c9c94ed29c1e48719237269672e343 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 8 Jun 2015 14:37:02 +0200 Subject: [PATCH] Db: caching command/zone name lookup methods --- library/Director/Db.php | 59 +++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/library/Director/Db.php b/library/Director/Db.php index 2718cf9c..72574e7c 100644 --- a/library/Director/Db.php +++ b/library/Director/Db.php @@ -8,6 +8,10 @@ class Db extends DbConnection { protected $modules = array(); + protected static $zoneCache; + + protected static $commandCache; + protected function db() { return $this->getDbAdapter(); @@ -40,21 +44,42 @@ class Db extends DbConnection public function enumCommands() { - $select = $this->db()->select()->from('icinga_command', array( - 'id', - 'object_name', - )) - ->order('object_name ASC'); - return $this->db()->fetchPairs($select); + if (self::$commandCache === null) { + $select = $this->db()->select()->from('icinga_command', array( + 'id', + 'object_name', + )) + ->order('object_name ASC'); + + self::$commandCache = $this->db()->fetchPairs($select); + } + return self::$commandCache; } public function enumZones() { - $select = $this->db()->select()->from('icinga_zone', array( - 'id', - 'object_name', - ))->where('object_type', 'object')->order('object_name ASC'); - return $this->db()->fetchPairs($select); + if (self::$zoneCache === null) { + $select = $this->db()->select()->from('icinga_zone', array( + 'id', + 'object_name', + ))->where('object_type', 'object')->order('object_name ASC'); + + self::$zoneCache = $this->db()->fetchPairs($select); + } + + return self::$zoneCache; + } + + public function getZoneName($id) + { + $objects = $this->enumZones(); + return $objects[$id]; + } + + public function getCommandName($id) + { + $objects = $this->enumCommands(); + return $objects[$id]; } public function enumHosts() @@ -92,4 +117,16 @@ class Db extends DbConnection ))->where('object_type', 'object')->order('object_name ASC'); return $this->db()->fetchPairs($select); } + + public function clearZoneCache() + { + // TODO: wipe cache on update/insert/delete + self::$zoneCache = null; + } + + public function clearCommandCache() + { + // TODO: wipe cache on update/insert/delete + self::$commandCache = null; + } }