C::M::DBIC::Schema - cleanups
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Model / DBIC / Schema / Role / Caching.pm
1 package Catalyst::Model::DBIC::Schema::Role::Caching;
2
3 use Moose::Role;
4 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
5
6 use namespace::clean -except => 'meta';
7
8 =head1 NAME
9
10 Catalyst::Model::DBIC::Schema::Role::Caching - Query caching support for
11 DBIx::Class
12
13 =head1 SYNOPSIS
14
15     __PACKAGE__->config({
16         roles => ['Caching']
17     ...
18     });
19
20     ...
21
22     $c->model('DB::Table')->search({ foo => 'bar' }, { cache_for => 18000 });
23
24 =head1 DESCRIPTION
25
26 Enable caching support using L<DBIx::Class::Cursor::Cached> and
27 L<Catalyst::Plugin::Cache>.
28
29 In order for this to work, L<Catalyst::Plugin::Cache> must be configured and
30 loaded. A possible configuration would look like this:
31
32   <Plugin::Cache>
33     <backend>       
34       class Cache::FastMmap
35       unlink_on_exit 1
36     </backend>
37   </Plugin::Cache>
38
39 Then in your queries, set the C<cache_for> ResultSet attribute to the number of
40 seconds you want the query results to be cached for, eg.:
41
42   $c->model('DB::Table')->search({ foo => 'bar' }, { cache_for => 18000 });
43
44 =head1 CONFIG PARAMETERS
45
46 =over 4
47
48 =item caching
49
50 Turn caching on or off, you can use:
51
52     $c->model('DB')->caching(0);
53
54 to disable caching at runtime.
55
56 =back
57
58 =cut
59
60 has 'caching' => (is => 'rw', isa => 'Int', default => 1);
61
62 after setup => sub {
63     my $self = shift;
64
65     return if defined $self->caching && !$self->caching;
66
67     $self->caching(0);
68
69     if (my $cursor_class = $self->connect_info->{cursor_class}) {
70         unless ($cursor_class->can('clear_cache')) {
71             carp "Caching disabled, cursor_class $cursor_class does not"
72                  . " support it.";
73             return;
74         }
75     } else {
76         my $cursor_class = 'DBIx::Class::Cursor::Cached';
77
78         unless (eval { Class::MOP::load_class($cursor_class) }) {
79             carp "Caching disabled, cannot load $cursor_class: $@";
80             return;
81         }
82
83         $self->connect_info->{cursor_class} = $cursor_class;
84     }
85
86     $self->caching(1);
87 };
88
89 before ACCEPT_CONTEXT => sub {
90     my ($self, $c) = @_;
91
92     return $self unless 
93         $self->caching;
94     
95     unless ($c->can('cache') && ref $c->cache) {
96         $c->log->warn("DBIx::Class cursor caching disabled, you don't seem to"
97             . " have a working Cache plugin.");
98         $self->caching(0);
99         $self->_reset_cursor_class;
100         return $self;
101     }
102
103     if (ref $self->schema->default_resultset_attributes) {
104         $self->schema->default_resultset_attributes->{cache_object} =
105             $c->cache;
106     } else {
107         $self->schema->default_resultset_attributes({
108             cache_object => $c->cache
109         });
110     }
111 };
112
113 =head1 METHODS
114
115 =over 4
116
117 =item _reset_cursor_class
118
119 Reset the cursor class to L<DBIx::Class::Storage::DBI::Cursor> if it's set to
120 L<DBIx::Class::Cursor::Cached>.
121
122 =cut
123
124 sub _reset_cursor_class {
125     my $self = shift;
126
127     if ($self->connect_info->{cursor_class} eq 'DBIx::Class::Cursor::Cached') {
128         $self->storage->cursor_class('DBIx::Class::Storage::DBI::Cursor');
129     }
130     
131     1;
132 }
133
134 =back
135
136 =head1 SEE ALSO
137
138 L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>, L<Catalyst::Plugin::Cache>,
139 L<Cache::FastMmap>, L<DBIx::Class::Cursor::Cached>
140
141 =head1 AUTHOR
142
143 Rafael Kitover, C<rkitover@cpan.org>
144
145 =head1 COPYRIGHT
146
147 This program is free software, you can redistribute it and/or modify it
148 under the same terms as Perl itself.
149
150 =cut
151
152 1;