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