Db: caching command/zone name lookup methods

This commit is contained in:
Thomas Gelf 2015-06-08 14:37:02 +02:00
parent 26621f5ab1
commit f848a170d5
1 changed files with 48 additions and 11 deletions

View File

@ -8,6 +8,10 @@ class Db extends DbConnection
{ {
protected $modules = array(); protected $modules = array();
protected static $zoneCache;
protected static $commandCache;
protected function db() protected function db()
{ {
return $this->getDbAdapter(); return $this->getDbAdapter();
@ -40,21 +44,42 @@ class Db extends DbConnection
public function enumCommands() public function enumCommands()
{ {
$select = $this->db()->select()->from('icinga_command', array( if (self::$commandCache === null) {
'id', $select = $this->db()->select()->from('icinga_command', array(
'object_name', 'id',
)) 'object_name',
->order('object_name ASC'); ))
return $this->db()->fetchPairs($select); ->order('object_name ASC');
self::$commandCache = $this->db()->fetchPairs($select);
}
return self::$commandCache;
} }
public function enumZones() public function enumZones()
{ {
$select = $this->db()->select()->from('icinga_zone', array( if (self::$zoneCache === null) {
'id', $select = $this->db()->select()->from('icinga_zone', array(
'object_name', 'id',
))->where('object_type', 'object')->order('object_name ASC'); 'object_name',
return $this->db()->fetchPairs($select); ))->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() public function enumHosts()
@ -92,4 +117,16 @@ class Db extends DbConnection
))->where('object_type', 'object')->order('object_name ASC'); ))->where('object_type', 'object')->order('object_name ASC');
return $this->db()->fetchPairs($select); 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;
}
} }