Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision db0ab48c

Von Moritz Bunkus vor mehr als 10 Jahren hinzugefügt

  • ID db0ab48cb88dffc3906bb906bac090f3863c4486
  • Vorgänger bba2bff3
  • Nachfolger 529e6feb

SL::DB::Object: generische Methoden zum Cachen von RDBO-Instanzen

Unterschiede anzeigen:

SL/DB/Object.pm
2 2

  
3 3
use strict;
4 4

  
5
use Carp;
5 6
use English qw(-no_match_vars);
6 7
use Rose::DB::Object;
7 8
use List::MoreUtils qw(any);
......
179 180
  return $result;
180 181
}
181 182

  
183
sub load_cached {
184
  my $class_or_self = shift;
185
  my @ids           = @_;
186
  my $class         = ref($class_or_self) || $class_or_self;
187
  my $cache         = $::request->cache("::SL::DB::Object::object_cache::${class}");
188

  
189
  croak "Missing ID" unless @ids;
190

  
191
  my @missing_ids = grep { !exists $cache->{$_} } @ids;
192

  
193
  return $cache->{$ids[0]} if !@missing_ids;
194

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

  
197
  my $primary_key = $class->meta->primary_key_columns->[0]->name;
198
  my $objects     = $class->_get_manager_class->get_all(where => [ $primary_key => \@missing_ids ]);
199

  
200
  $cache->{$_->$primary_key} = $_ for @{ $objects};
201

  
202
  return $cache->{$ids[0]};
203
}
204

  
205
sub invalidate_cached {
206
  my ($class_or_self, @ids) = @_;
207
  my $class                 = ref($class_or_self) || $class_or_self;
208

  
209
  if (ref($class_or_self) && !@ids) {
210
    croak "Caching can only be used with classes with exactly one primary key column" if 1 != scalar(@{ $class->meta->primary_key_columns });
211

  
212
    my $primary_key = $class->meta->primary_key_columns->[0]->name;
213
    @ids            = ($class_or_self->$primary_key);
214
  }
215

  
216
  delete @{ $::request->cache("::SL::DB::Object::object_cache::${class}") }{ @ids };
217

  
218
  return $class_or_self;
219
}
220

  
182 221
1;
183 222

  
184 223
__END__
185 224

  
186 225
=pod
187 226

  
227
=encoding utf8
228

  
188 229
=head1 NAME
189 230

  
190 231
SL::DB::Object: Base class for all of our model classes
......
257 298
be used to check whether or not an object's columns are unique before
258 299
saving or during validation.
259 300

  
301
=item C<load_cached @ids>
302

  
303
Loads objects from the database which haven't been cached before and
304
caches them for the duration of the current request (see
305
L<SL::Request/cache>).
306

  
307
This method can be called both as an instance method and a class
308
method. It loads objects for the corresponding class (e.g. both
309
C<SL::DB::Part-E<gt>load_cached(…)> and
310
C<$some_part-E<gt>load_cached(…)> will load parts).
311

  
312
Currently only classes with a single primary key column are supported.
313

  
314
Returns the cached object for the first ID.
315

  
316
=item C<invalidate_cached @ids>
317

  
318
Deletes all cached instances of this class (see L</load_cached>) for
319
the given IDs.
320

  
321
If called as an instance method without further arguments then the
322
object's ID is used.
323

  
324
Returns the object/class it was called on.
325

  
260 326
=back
261 327

  
262 328
=head1 AUTHOR

Auch abrufbar als: Unified diff