1 package Catalyst::TraitFor::Model::DBIC::Schema::Caching;
3 use namespace::autoclean;
5 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
6 use MooseX::Types::Moose 'Int';
10 Catalyst::TraitFor::Model::DBIC::Schema::Caching - Query caching support for
11 Catalyst::Model::DBIC::Schema
16 traits => ['Caching'],
18 ['dbi:mysql:db', 'user', 'pass'],
21 $c->model('DB::Table')->search({ foo => 'bar' }, { cache_for => 18000 });
25 Enable caching support using L<DBIx::Class::Cursor::Cached> and
26 L<Catalyst::Plugin::Cache>.
28 In order for this to work, L<Catalyst::Plugin::Cache> must be configured and
29 loaded. A possible configuration would look like this:
35 share_file /tmp/myapp_share
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 See L<Catalyst::Model::DBIC::Schema/AUTHOR> and
114 L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>.
118 See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>.
122 This program is free software, you can redistribute it and/or modify it
123 under the same terms as Perl itself.