1 package Catalyst::TraitFor::Model::DBIC::Schema::Caching;
3 use namespace::autoclean;
5 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
6 use Catalyst::Model::DBIC::Schema::Types 'CursorClass';
7 use MooseX::Types::Moose qw/Int Str/;
11 Catalyst::TraitFor::Model::DBIC::Schema::Caching - Query caching support for
12 Catalyst::Model::DBIC::Schema
17 traits => ['Caching'],
19 ['dbi:mysql:db', 'user', 'pass'],
22 $c->model('DB::Table')->search({ foo => 'bar' }, { cache_for => 18000 });
26 Enable caching support using L<DBIx::Class::Cursor::Cached> and
27 L<Catalyst::Plugin::Cache>.
29 In order for this to work, L<Catalyst::Plugin::Cache> must be configured and
30 loaded. A possible configuration would look like this:
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.:
42 $c->model('DB::Table')->search({ foo => 'bar' }, { cache_for => 18000 });
44 =head1 CONFIG PARAMETERS
48 Turn caching on or off, you can use:
50 $c->model('DB')->caching(0);
54 has caching => (is => 'rw', isa => Int, default => 1);
59 return if !$self->caching;
63 my $cursor_class = $self->connect_info->{cursor_class}
64 || 'DBIx::Class::Cursor::Cached';
66 unless (eval { Class::MOP::load_class($cursor_class) }) {
67 carp "Caching disabled, cannot load cursor class"
68 . " $cursor_class: $@";
72 unless ($cursor_class->can('clear_cache')) {
73 carp "Caching disabled, cursor_class $cursor_class does not"
78 $self->connect_info->{cursor_class} = $cursor_class;
82 before ACCEPT_CONTEXT => sub {
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.");
92 $self->_reset_cursor_class;
96 if (ref $self->schema->default_resultset_attributes) {
97 $self->schema->default_resultset_attributes->{cache_object} =
100 $self->schema->default_resultset_attributes({
101 cache_object => $c->cache
108 L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>, L<Catalyst::Plugin::Cache>,
109 L<Cache::FastMmap>, L<DBIx::Class::Cursor::Cached>
113 Rafael Kitover, C<rkitover at cpan.org>
117 This program is free software, you can redistribute it and/or modify it
118 under the same terms as Perl itself.