Add object caching

This commit is contained in:
Isaac Connor committed 2018-01-22 15:25:51 -05:00
1 parent d63594b5fa
commit 2068a5c684
1 file changed
+26 -4
+26 -4
View File
@@ -42,17 +42,26 @@ use ZoneMinder::Config qw(:all);
use ZoneMinder::Logger qw(:all);
use ZoneMinder::Database qw(:all);
use vars qw/ $AUTOLOAD $log $dbh/;
use vars qw/ $AUTOLOAD $log $dbh %cache $no_cache/;
*log = \$ZoneMinder::Logger::logger;
*dbh = \$ZoneMinder::Database::dbh;
my $debug = 0;
$no_cache = 0;
use constant DEBUG_ALL=>0;
sub init_cache {
$no_cache = 0;
%cache = ();
} # end sub init_cache
sub new {
my ( $parent, $id, $data ) = @_;
$cache{$parent} = {} if ! $cache{$parent};
my $sub_cache = $cache{$parent};
my $self = {};
bless $self, $parent;
no strict 'refs';
@@ -61,10 +70,23 @@ sub new {
Error( 'NO primary_key for type ' . $parent );
return;
} # end if
if ( ( $$self{$primary_key} = $id ) or $data ) {
#$log->debug("loading $parent $id") if $debug or DEBUG_ALL;
$self->load( $data );
if ( $id and (!$no_cache) and $$sub_cache{$id} ) {
if ( $data ) {
# The reason to use load is if we have overriden it in the object,
$$sub_cache{$id}->load( $data );
}
return $$sub_cache{$id};
}
if ( ( $$self{$primary_key} = $id ) or $data ) {
#$log->debug("loading $parent $id") if $debug or DEBUG_ALL;
$self->load( $data );
if ( !$no_cache ) {
$$sub_cache{$id} = $self;
} # end if
} # end if
return $self;
} # end sub new