1 package Catalyst::Model::DBIC::Schema::Role::Caching;
4 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
5 use Catalyst::Model::DBIC::Schema::Types 'CursorClass';
6 use MooseX::Types::Moose qw/Int Str/;
8 use namespace::clean -except => 'meta';
12 Catalyst::Model::DBIC::Schema::Role::Caching - Query caching support for
13 Catalyst::Model::DBIC::Schema
20 ['dbi:mysql:db', 'user', 'pass'],
23 $c->model('DB::Table')->search({ foo => 'bar' }, { cache_for => 18000 });
27 Enable caching support using L<DBIx::Class::Cursor::Cached> and
28 L<Catalyst::Plugin::Cache>.
30 In order for this to work, L<Catalyst::Plugin::Cache> must be configured and
31 loaded. A possible configuration would look like this:
40 Then in your queries, set the C<cache_for> ResultSet attribute to the number of
41 seconds you want the query results to be cached for, eg.:
43 $c->model('DB::Table')->search({ foo => 'bar' }, { cache_for => 18000 });
45 =head1 CONFIG PARAMETERS
49 Turn caching on or off, you can use:
51 $c->model('DB')->caching(0);
55 has 'caching' => (is => 'rw', isa => Int, default => 1);
60 return if !$self->caching;
64 my $cursor_class = $self->connect_info->{cursor_class}
65 || 'DBIx::Class::Cursor::Cached';
67 unless (eval { Class::MOP::load_class($cursor_class) }) {
68 carp "Caching disabled, cannot load cursor class"
69 . " $cursor_class: $@";
73 unless ($cursor_class->can('clear_cache')) {
74 carp "Caching disabled, cursor_class $cursor_class does not"
79 $self->connect_info->{cursor_class} = $cursor_class;
83 before ACCEPT_CONTEXT => sub {
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.");
93 $self->_reset_cursor_class;
97 if (ref $self->schema->default_resultset_attributes) {
98 $self->schema->default_resultset_attributes->{cache_object} =
101 $self->schema->default_resultset_attributes({
102 cache_object => $c->cache
109 L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>, L<Catalyst::Plugin::Cache>,
110 L<Cache::FastMmap>, L<DBIx::Class::Cursor::Cached>
114 Rafael Kitover, C<rkitover@cpan.org>
118 This program is free software, you can redistribute it and/or modify it
119 under the same terms as Perl itself.