Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 913348ce

Von Moritz Bunkus vor mehr als 7 Jahren hinzugefügt

  • ID 913348ce9a1a0285ef59ae1731a1bc7ba353f79c
  • Vorgänger 6b92d724
  • Nachfolger 78ed32f8

SL::DB::Helper::Manager: neue Funktion zum Pre-Cachen aller Einträge einer Tabelle

Unterschiede anzeigen:

SL/DB/Helper/Manager.pm
2 2

  
3 3
use strict;
4 4

  
5
use Carp;
6

  
5 7
use Rose::DB::Object::Manager;
6 8
use base qw(Rose::DB::Object::Manager);
7 9

  
......
36 38
  )->[0];
37 39
}
38 40

  
41
sub cache_all {
42
  my $manager_class =  shift;
43
  my $class         =  $manager_class;
44
  $class            =~ s{Manager::}{};
45

  
46
  croak "Caching can only be used with classes with exactly one primary key column" if 1 != scalar(@{ $class->meta->primary_key_columns });
47

  
48
  my $all_have_been_cached =  $::request->cache("::SL::DB::Manager::cache_all");
49
  return if $all_have_been_cached->{$class};
50

  
51
  $all_have_been_cached->{$class} = 1;
52

  
53
  my $item_cache                  = $::request->cache("::SL::DB::Object::object_cache::${class}");
54
  my $primary_key                 = $class->meta->primary_key_columns->[0]->name;
55
  my $objects                     = $class->_get_manager_class->get_all;
56

  
57
  $item_cache->{$_->$primary_key} = $_ for @{ $objects};
58
}
59

  
39 60
1;
61
__END__
62

  
63
=pod
64

  
65
=encoding utf8
66

  
67
=head1 NAME
68

  
69
SL::DB::Helper::Manager - Base class & helper functions for all Rose manager classes
70

  
71
=head1 FUNCTIONS
72

  
73
=over 4
74

  
75
=item C<cache_all>
76

  
77
Pre-caches all items from a table. Use this is you expect to need all
78
items from a table. You can retrieve them later with the
79
C<load_cached> function from the corresponding Rose DB object class.
80

  
81
For example, if you expect to need all unit objects, you can use
82
C<SL::DB::Manager::Unit-E<gt>cache_all> before you start the actual
83
work. Later you can use C<SL::DB::Unit-E<gt>load_cached> to retrieve
84
individual objects and be sure that they're already cached.
85

  
86
=item C<find_by>
87

  
88
TODO: Describe find_by
89

  
90
=item C<find_by_or_create>
91

  
92
TODO: Describe find_by_or_create
93

  
94
=item C<get_first>
95

  
96
TODO: Describe get_first
97

  
98
=item C<make_manager_methods>
99

  
100
TODO: Describe make_manager_methods
101

  
102
=back
103

  
104
=head1 BUGS
105

  
106
Nothing here yet.
107

  
108
=head1 AUTHOR
109

  
110
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
111

  
112
=cut
SL/DB/Object.pm
324 324
caches them for the duration of the current request (see
325 325
L<SL::Request/cache>).
326 326

  
327
If you know in advance that you will likely need all objects of a
328
particular type then you can pre-cache them by calling the manager's
329
C<cache_all> function. For example, if you expect to need all unit
330
objects, you can use C<SL::DB::Manager::Unit-E<gt>cache_all> before
331
you start the actual work. Later you can use
332
C<SL::DB::Unit-E<gt>load_cached> to retrieve individual objects and be
333
sure that they're already cached.
334

  
327 335
This method can be called both as an instance method and a class
328 336
method. It loads objects for the corresponding class (e.g. both
329 337
C<SL::DB::Part-E<gt>load_cached(…)> and

Auch abrufbar als: Unified diff