add caching and find_one to Storage and Groups

This commit is contained in:
Isaac Connor committed 2018-04-10 13:05:37 -07:00
1 parent ae3ccf6ffe
commit 03ff2fcde4
4 files changed
+64 -21

No files matched your search

+3 -1
View File
@@ -49,7 +49,9 @@ class Event {
$this->{'Storage'} = $new;
}
if ( ! ( array_key_exists( 'Storage', $this ) and $this->{'Storage'} ) ) {
$this->{'Storage'} = new Storage( isset($this->{'StorageId'}) ? $this->{'StorageId'} : NULL );
$this->{'Storage'} = isset($this->{'StorageId'}) ?
Storage::find_one(array('Id'=>$this->{'StorageId'})) :
new Storage(NULL);
}
return $this->{'Storage'};
}
+28 -4
View File
@@ -1,5 +1,7 @@
<?php
$group_cache = array();
class Group {
public $defaults = array(
@@ -9,6 +11,8 @@ class Group {
);
public function __construct( $IdOrRow=NULL ) {
global $group_cache;
$row = NULL;
if ( $IdOrRow ) {
if ( is_integer($IdOrRow) or is_numeric($IdOrRow) ) {
@@ -33,13 +37,14 @@ class Group {
$this->{$k} = $v;
}
}
$group_cache[$row['Id']] = $this;
} // end function __construct
public function __call( $fn, array $args ) {
if ( count( $args ) ) {
public function __call($fn, array $args) {
if ( count($args) ) {
$this->{$fn} = $args[0];
}
if ( array_key_exists( $fn, $this ) ) {
if ( array_key_exists($fn, $this) ) {
return $this->{$fn};
} else if ( array_key_exists( $fn, $this->defaults ) ) {
$this->{$fn} = $this->defaults{$fn};
@@ -53,6 +58,25 @@ class Group {
}
}
public static function find_one( $parameters = null, $options = null ) {
global $group_cache;
if (
( count($parameters) == 1 ) and
isset($parameters['Id']) and
isset($group_cache[$parameters['Id']]) ) {
return $group_cache[$parameters['Id']];
}
$results = Group::find_all($parameters, $options);
if ( count($results) > 1 ) {
Error("Group::find_one Returned more than 1");
return $results[0];
} else if ( count($results) ) {
return $results[0];
} else {
return null;
}
}
public static function find_all( $parameters = null ) {
$filters = array();
$sql = 'SELECT * FROM Groups ';
@@ -289,7 +313,7 @@ $group_options[$Group->Id()] = str_repeat( '&nbsp;', $depth ) . $Group->Name();
public function Parent( ) {
if ( $this->{'ParentId'} ) {
return new Group($this->{'ParentId'});
return Group::find_one(array('Id'=>$this->{'ParentId'}));
}
return null;
}
+4 -3
View File
@@ -348,7 +348,7 @@ private $control_fields = array(
$url = '?user='.$_SESSION['username'];
}
}
Logger::Debug("sending command to $url");
Logger::Debug("sending command to $url");
$data = array('Monitor[Function]' => $this->{'Function'} );
// use key 'http' even if you send the request to https://...
@@ -442,10 +442,11 @@ Logger::Debug("sending command to $url");
$this->{'Storage'} = $new;
}
if ( ! ( array_key_exists('Storage', $this) and $this->{'Storage'} ) ) {
$this->{'Storage'} = new Storage( isset($this->{'StorageId'}) ? $this->{'StorageId'} : NULL );
$this->{'Storage'} = isset($this->{'StorageId'}) ?
Storage::find_one(array('Id'=>$this->{'StorageId'})) :
new Storage(NULL);
}
return $this->{'Storage'};
}
} // end class Monitor
?>
+29 -13
View File
@@ -1,18 +1,14 @@
<?php
require_once( 'database.php' );
require_once('database.php');
#$storage_cache = array();
$storage_cache = array();
class Storage {
public function __construct( $IdOrRow = NULL ) {
global $storage_cache;
$row = NULL;
if ( $IdOrRow ) {
if ( is_integer( $IdOrRow ) or is_numeric( $IdOrRow ) ) {
#if ( isset( $storage_cache[$IdOrRow] ) ) {
#Warning("using cached object for $dOrRow");
#return $storage_cache[$IdOrRow];
#} else {
#Warning("Not using cached object for $dOrRow");
$row = dbFetchOne( 'SELECT * FROM Storage WHERE Id=?', NULL, array( $IdOrRow ) );
if ( ! $row ) {
Error("Unable to load Storage record for Id=" . $IdOrRow );
@@ -25,7 +21,7 @@ class Storage {
foreach ($row as $k => $v) {
$this->{$k} = $v;
}
#$storage_cache[$IdOrRow] = $this;
$storage_cache[$IdOrRow] = $this;
} else {
$this->{'Name'} = '';
$this->{'Path'} = '';
@@ -70,7 +66,25 @@ class Storage {
Warning( "Unknown function call Storage->$fn from $file:$line" );
}
}
public static function find_all( $parameters = null, $options = null ) {
public static function find_one( $parameters = null, $options = null ) {
global $storage_cache;
if (
( count($parameters) == 1 ) and
isset($parameters['Id']) and
isset($storage_cache[$parameters['Id']]) ) {
return $storage_cache[$parameters['Id']];
}
$results = Storage::find_all( $parameters, $options );
if ( count($results) > 1 ) {
Error("Storage Returned more than 1");
return $results[0];
} else if ( count($results) ) {
return $results[0];
} else {
return null;
}
}
public static function find_all( $parameters = null, $options = null ) {
$filters = array();
$sql = 'SELECT * FROM Storage ';
$values = array();
@@ -97,9 +111,11 @@ public static function find_all( $parameters = null, $options = null ) {
$sql .= ' ORDER BY ' . $options['order'];
}
$result = dbQuery($sql, $values);
$results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Storage');
foreach ( $results as $row => $obj ) {
$filters[] = $obj;
if ( $result ) {
$results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Storage');
foreach ( $results as $row => $obj ) {
$filters[] = $obj;
}
}
return $filters;
}