From 706c73c3c6125f43fc038601670d49e8c7ec80db Mon Sep 17 00:00:00 2001 From: David Cook Date: Tue, 20 Aug 2024 04:31:31 +0000 Subject: [PATCH] Bug 37682: Lazy-load modules for setters in koha-preferences tool This change lazy-loads modules for setters in the koha-preferences tool, so that getters are free to run super fast. Between BZ 37657 and BZ 37682 we effectively eliminate the overhead of running "get" or "dump" commands via the koha-preferences tool. Test plan: 1. time misc/admin/koha-preferences get SearchEngine 2. Note time is about .35 seconds 3. time misc/admin/koha-preferences dump 4. Note time is about .35 seconds 5. Create sysprefs.yml Signed-off-by: Martin Renvoize --- misc/admin/koha-preferences | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/misc/admin/koha-preferences b/misc/admin/koha-preferences index 11cbc42dc73..1cb866f5bae 100755 --- a/misc/admin/koha-preferences +++ b/misc/admin/koha-preferences @@ -19,19 +19,16 @@ # use Modern::Perl; -use Koha::Script; -use C4::Context; -use C4::Log; use Getopt::Long; use Pod::Usage; use YAML::XS; -use Koha::Logger; - use Koha::Database; our %NOT_SET_PREFS = map { $_, 1 } qw( Version ); +my $lazy_loaded; + =head1 NAME koha-preferences - Get, set, dump and load Koha system preferences @@ -42,6 +39,21 @@ misc/admin/koha-preferences COMMAND ... =cut +sub _lazy_load_modules { + if (!$lazy_loaded){ + #NOTE: "use" runs at compile time, so require/import() must be used instead + require Koha::Script; + Koha::Script->import(); + require C4::Context; + C4::Context->import(); + require C4::Log; + C4::Log->import; + require Koha::Logger; + Koha::Logger->import; + $lazy_loaded = 1; + } +} + sub print_usage { my ( $annoyed ) = @_; @@ -215,6 +227,7 @@ my %commands = ( }, load => sub { my ( $infile, $force_version ); + _lazy_load_modules(); GetOptions( 'i:s' => \$infile, @@ -246,6 +259,7 @@ my %commands = ( }, set => sub { my ( $preference, $value ) = @_; + _lazy_load_modules(); print_usage() unless ( $preference && defined($value) ); @@ -253,6 +267,7 @@ my %commands = ( }, clear => sub { my ( $preference ) = @_; + _lazy_load_modules(); print_usage() unless ( $preference ); -- 2.46.0