From 913348ce9a1a0285ef59ae1731a1bc7ba353f79c Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 20 Dec 2016 16:20:49 +0100 Subject: [PATCH] =?utf8?q?SL::DB::Helper::Manager:=20neue=20Funktion=20zum?= =?utf8?q?=20Pre-Cachen=20aller=20Eintr=C3=A4ge=20einer=20Tabelle?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/DB/Helper/Manager.pm | 73 +++++++++++++++++++++++++++++++++++++++++ SL/DB/Object.pm | 8 +++++ 2 files changed, 81 insertions(+) diff --git a/SL/DB/Helper/Manager.pm b/SL/DB/Helper/Manager.pm index d456e76b0..c44f67953 100644 --- a/SL/DB/Helper/Manager.pm +++ b/SL/DB/Helper/Manager.pm @@ -2,6 +2,8 @@ package SL::DB::Helper::Manager; use strict; +use Carp; + use Rose::DB::Object::Manager; use base qw(Rose::DB::Object::Manager); @@ -36,4 +38,75 @@ sub get_first { )->[0]; } +sub cache_all { + my $manager_class = shift; + my $class = $manager_class; + $class =~ s{Manager::}{}; + + croak "Caching can only be used with classes with exactly one primary key column" if 1 != scalar(@{ $class->meta->primary_key_columns }); + + my $all_have_been_cached = $::request->cache("::SL::DB::Manager::cache_all"); + return if $all_have_been_cached->{$class}; + + $all_have_been_cached->{$class} = 1; + + my $item_cache = $::request->cache("::SL::DB::Object::object_cache::${class}"); + my $primary_key = $class->meta->primary_key_columns->[0]->name; + my $objects = $class->_get_manager_class->get_all; + + $item_cache->{$_->$primary_key} = $_ for @{ $objects}; +} + 1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::DB::Helper::Manager - Base class & helper functions for all Rose manager classes + +=head1 FUNCTIONS + +=over 4 + +=item C + +Pre-caches all items from a table. Use this is you expect to need all +items from a table. You can retrieve them later with the +C function from the corresponding Rose DB object class. + +For example, if you expect to need all unit objects, you can use +Ccache_all> before you start the actual +work. Later you can use Cload_cached> to retrieve +individual objects and be sure that they're already cached. + +=item C + +TODO: Describe find_by + +=item C + +TODO: Describe find_by_or_create + +=item C + +TODO: Describe get_first + +=item C + +TODO: Describe make_manager_methods + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut diff --git a/SL/DB/Object.pm b/SL/DB/Object.pm index 4c3e18439..485fab2ce 100755 --- a/SL/DB/Object.pm +++ b/SL/DB/Object.pm @@ -324,6 +324,14 @@ Loads objects from the database which haven't been cached before and caches them for the duration of the current request (see L). +If you know in advance that you will likely need all objects of a +particular type then you can pre-cache them by calling the manager's +C function. For example, if you expect to need all unit +objects, you can use Ccache_all> before +you start the actual work. Later you can use +Cload_cached> to retrieve individual objects and be +sure that they're already cached. + This method can be called both as an instance method and a class method. It loads objects for the corresponding class (e.g. both Cload_cached(…)> and -- 2.20.1