Stop using deprecated Class::MOP::load_module
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / TraitFor / Model / DBIC / Schema / Caching.pm
CommitLineData
fb691af9 1package Catalyst::TraitFor::Model::DBIC::Schema::Caching;
fdd5f3df 2
73f72d28 3use namespace::autoclean;
fdd5f3df 4use Moose::Role;
bd309c0c 5use Carp::Clan '^Catalyst::Model::DBIC::Schema';
7314403a 6use MooseX::Types::Moose 'Int';
45b10191 7use Module::Runtime 'use_module';
fdd5f3df 8
fdd5f3df 9=head1 NAME
10
fb691af9 11Catalyst::TraitFor::Model::DBIC::Schema::Caching - Query caching support for
c4fee9b8 12Catalyst::Model::DBIC::Schema
fdd5f3df 13
14=head1 SYNOPSIS
15
16 __PACKAGE__->config({
c34bcab6 17 traits => ['Caching'],
61ed82a5 18 connect_info =>
19 ['dbi:mysql:db', 'user', 'pass'],
fdd5f3df 20 });
21
fdd5f3df 22 $c->model('DB::Table')->search({ foo => 'bar' }, { cache_for => 18000 });
23
24=head1 DESCRIPTION
25
26Enable caching support using L<DBIx::Class::Cursor::Cached> and
27L<Catalyst::Plugin::Cache>.
28
29In order for this to work, L<Catalyst::Plugin::Cache> must be configured and
30loaded. A possible configuration would look like this:
31
32 <Plugin::Cache>
33 <backend>
34 class Cache::FastMmap
35 unlink_on_exit 1
3d8d37d3 36 share_file /tmp/myapp_share
fdd5f3df 37 </backend>
38 </Plugin::Cache>
39
40Then in your queries, set the C<cache_for> ResultSet attribute to the number of
41seconds you want the query results to be cached for, eg.:
42
43 $c->model('DB::Table')->search({ foo => 'bar' }, { cache_for => 18000 });
44
2ff00e2b 45=head1 CONFIG PARAMETERS
fdd5f3df 46
c4fee9b8 47=head2 caching
fdd5f3df 48
49Turn caching on or off, you can use:
50
51 $c->model('DB')->caching(0);
52
fdd5f3df 53=cut
54
c34bcab6 55has caching => (is => 'rw', isa => Int, default => 1);
fdd5f3df 56
57after setup => sub {
58 my $self = shift;
59
61ed82a5 60 return if !$self->caching;
fdd5f3df 61
62 $self->caching(0);
63
61ed82a5 64 my $cursor_class = $self->connect_info->{cursor_class}
65 || 'DBIx::Class::Cursor::Cached';
fdd5f3df 66
45b10191 67 unless (eval { use_module($cursor_class) }) {
61ed82a5 68 carp "Caching disabled, cannot load cursor class"
69 . " $cursor_class: $@";
70 return;
71 }
fdd5f3df 72
61ed82a5 73 unless ($cursor_class->can('clear_cache')) {
74 carp "Caching disabled, cursor_class $cursor_class does not"
75 . " support it.";
76 return;
fdd5f3df 77 }
78
61ed82a5 79 $self->connect_info->{cursor_class} = $cursor_class;
fdd5f3df 80 $self->caching(1);
81};
82
83before ACCEPT_CONTEXT => sub {
84 my ($self, $c) = @_;
85
86 return $self unless
87 $self->caching;
61ed82a5 88
fdd5f3df 89 unless ($c->can('cache') && ref $c->cache) {
90 $c->log->warn("DBIx::Class cursor caching disabled, you don't seem to"
91 . " have a working Cache plugin.");
92 $self->caching(0);
93 $self->_reset_cursor_class;
94 return $self;
95 }
96
97 if (ref $self->schema->default_resultset_attributes) {
98 $self->schema->default_resultset_attributes->{cache_object} =
99 $c->cache;
100 } else {
101 $self->schema->default_resultset_attributes({
102 cache_object => $c->cache
103 });
104 }
105};
106
fdd5f3df 107=head1 SEE ALSO
108
109L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>, L<Catalyst::Plugin::Cache>,
110L<Cache::FastMmap>, L<DBIx::Class::Cursor::Cached>
111
112=head1 AUTHOR
113
4e251d1a 114See L<Catalyst::Model::DBIC::Schema/AUTHOR> and
115L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>.
fdd5f3df 116
117=head1 COPYRIGHT
118
4e251d1a 119See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>.
120
121=head1 LICENSE
122
fdd5f3df 123This program is free software, you can redistribute it and/or modify it
124under the same terms as Perl itself.
125
126=cut
127
1281;