4b43a693eed38a31dd32c7e3371b5fabac9161bf
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSource / Table / Cached.pm
1 package DBIx::Class::ResultSource::Table::Cached;
2
3 use Scalar::Util qw/weaken/;
4
5 use base qw/DBIx::Class/;
6 __PACKAGE__->load_components(qw/ResultSource::Table/);
7
8 sub resultset {
9   my $self = shift;
10   return $self->{_resultset} ||= do {
11       my $rs = $self->next::method;
12       weaken $rs->result_source;
13       $rs;
14   };
15 }
16
17 1;
18
19 __END__
20
21 =head1 NAME 
22
23 DBIx::Class::ResultSource::Table::Cached - Table object that caches its own resultset
24
25 =head1 SYNOPSIS
26     
27     # in a table class or base of table classes (_before_ you call ->table)
28     __PACKAGE__->table_class('DBIx::Class::ResultSource::Table::Cached');
29
30 =head1 DESCRIPTION
31
32 This is a modified version of L<DBIx::Class::ResultSource::Table> that caches
33 its resultset, so when you call $schema->resultset('Foo') it does not 
34 re-instantiate the resultset each time. In pathological cases this may not
35 work correctly, e.g. if you change important attributes of the result source
36 object.
37
38 =head1 AUTHORS
39
40 David Kamholz <dkamholz@cpan.org>
41
42 =head1 LICENSE
43
44 You may distribute this code under the same terms as Perl itself.
45
46 =cut