From: Sven Schöling Date: Tue, 8 Aug 2017 12:11:48 +0000 (+0200) Subject: CustomVariables: Requestlevel Caching für cvars_by_config X-Git-Tag: release-3.5.4~862 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=43de7ef11a1fe565968656c24ea0772a60667b3b;p=kivitendo-erp.git CustomVariables: Requestlevel Caching für cvars_by_config - Configs werden jetzt gecacht. - is_valid wird en bloc vorberechnet und im Objekt gecacht. --- diff --git a/SL/DB/CustomVariable.pm b/SL/DB/CustomVariable.pm index 26ff748f7..a34640518 100644 --- a/SL/DB/CustomVariable.pm +++ b/SL/DB/CustomVariable.pm @@ -124,8 +124,13 @@ sub is_valid { require SL::DB::CustomVariableValidity; - my $query = [config_id => $self->config_id, trans_id => $self->trans_id]; - return (SL::DB::Manager::CustomVariableValidity->get_all_count(query => $query) == 0) ? 1 : 0; + # only base level custom variables can be invalid. ovverloaded ones could potentially clash on trans_id, so disallow them + return 1 if $_->sub_module; + + $self->{is_valid} //= do { + my $query = [config_id => $self->config_id, trans_id => $self->trans_id]; + (SL::DB::Manager::CustomVariableValidity->get_all_count(query => $query) == 0) ? 1 : 0; + } } 1; diff --git a/SL/DB/Helper/CustomVariables.pm b/SL/DB/Helper/CustomVariables.pm index e80d6fda8..6373c32e0 100644 --- a/SL/DB/Helper/CustomVariables.pm +++ b/SL/DB/Helper/CustomVariables.pm @@ -4,7 +4,7 @@ use strict; use Carp; use Data::Dumper; use List::Util qw(first); -use List::UtilsBy qw(sort_by); +use List::UtilsBy qw(sort_by partition_by); use constant META_CVARS => 'cvars_config'; @@ -84,17 +84,23 @@ sub make_cvar_by_configs { my $configs = _all_configs(%params); my $cvars = $self->custom_variables; my %cvars_by_config = map { $_->config_id => $_ } @$cvars; + my $invalids = _all_invalids($self->${\ $self->meta->primary_key_columns->[0]->name }, $configs, %params); + my %invalids_by_config = map { $_->config_id => 1 } @$invalids; my @return = map( { + my $cvar; if ( $cvars_by_config{$_->id} ) { - $cvars_by_config{$_->id}; + $cvar = $cvars_by_config{$_->id}; + $cvar->{is_valid} = !$invalids_by_config{$_->id}; } else { - my $cvar = _new_cvar($self, %params, config => $_); + $cvar = _new_cvar($self, %params, config => $_); $self->add_custom_variables($cvar); - $cvar; + $cvar->{is_valid} = 1; } + $cvar->{config} = $_; + $cvar; } @$configs ); @@ -168,7 +174,17 @@ sub _all_configs { require SL::DB::CustomVariableConfig; - SL::DB::Manager::CustomVariableConfig->get_all_sorted($params{module} ? (query => [ module => $params{module} ]) : ()); + my $cache = $::request->cache("::SL::DB::Helper::CustomVariables::object_cache"); + + if (!$cache->{all}) { + my $configs = SL::DB::Manager::CustomVariableConfig->get_all_sorted; + $cache->{all} = $configs; + $cache->{module} = { partition_by { $_->module } @$configs }; + } + + $params{module} + ? $cache->{modules}{$params{module}} + : $cache->{all}; } sub _overload_by_module { @@ -353,6 +369,31 @@ sub make_cvar_custom_filter { ); } + +sub _all_invalids { + my ($trans_id, $configs, %params) = @_; + + require SL::DB::CustomVariableValidity; + + # easy 1: no trans_id, all valid by default. + return [] unless $trans_id; + + # easy 2: no module in params? no validity + return [] unless $params{module}; + + my @module_configs = grep { $_->module eq $params{module} } @$configs; + + return [] unless @module_configs; + + # nor find all entries for that and return + SL::DB::Manager::CustomVariableValidity->get_all( + query => [ + config_id => [ map { $_->id } @module_configs ], + trans_id => $trans_id, + ] + ); +} + 1; __END__