From f460f73ac2393401f85845f8df77af9fd0e8f781 Mon Sep 17 00:00:00 2001 From: Jesse Weaver Date: Tue, 1 Mar 2016 13:44:22 -0700 Subject: [PATCH] Bug 15350: Cache loaded DBIx::Class schema classes This separates the Koha::Schema creation into two stages: 1) The loading of the schemas themselves, which is done upon import of Koha::Database. This requires moving the import of Koha::Schema from an on-demand `require` to just a `use`; most everything in Koha uses C4::Context, which will indirectly end up calling Koha::Database->schema->new anyway, so this was no longer saving anything. 2) The actual database connection, which is done by cloning the schema created above and adding a database connection. Note that this saves time only when a Plack-based Koha needs to create a database connection, which is usually only the case for a new worker. Unscientific timings before patch: * First load of koha2marclinks.pl on a new worker: 0.70 seconds * Each load after: 0.17 seconds After patch: * First load: 0.31 seconds * Each load after: 0.17 seconds --- Koha/Database.pm | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Koha/Database.pm b/Koha/Database.pm index a8f4eab..b96918e 100644 --- a/Koha/Database.pm +++ b/Koha/Database.pm @@ -35,20 +35,23 @@ Koha::Database use Modern::Perl; use Carp; use C4::Context; +use Koha::Schema; use base qw(Class::Accessor); use vars qw($database); __PACKAGE__->mk_accessors(qw( )); +BEGIN { + # Initialize the base schema object (just loads the schema classes) + $database->{base_schema} = Koha::Schema->clone; +} + # _new_schema # Internal helper function (not a method!). This creates a new # database connection from the data given in the current context, and # returns it. sub _new_schema { - - require Koha::Schema; - my $context = C4::Context->new(); my $db_driver = $context->{db_driver}; @@ -70,7 +73,10 @@ sub _new_schema { $encoding_query = "set client_encoding = 'UTF8';"; $tz_query = qq(SET TIME ZONE = "$tz") if $tz; } - my $schema = Koha::Schema->connect( + + # This creates a new copy of the schema with its own database handle but with all the schema + # classes already loaded. + my $schema = $database->{base_schema}->connect( { dsn => "dbi:$db_driver:database=$db_name;host=$db_host;port=$db_port", user => $db_user, -- 2.7.0