Commit | Line | Data |
fb691af9 |
1 | package Catalyst::TraitFor::Model::DBIC::Schema::Caching; |
fdd5f3df |
2 | |
73f72d28 |
3 | use namespace::autoclean; |
fdd5f3df |
4 | use Moose::Role; |
bd309c0c |
5 | use Carp::Clan '^Catalyst::Model::DBIC::Schema'; |
61ed82a5 |
6 | use Catalyst::Model::DBIC::Schema::Types 'CursorClass'; |
7 | use MooseX::Types::Moose qw/Int Str/; |
fdd5f3df |
8 | |
fdd5f3df |
9 | =head1 NAME |
10 | |
fb691af9 |
11 | Catalyst::TraitFor::Model::DBIC::Schema::Caching - Query caching support for |
c4fee9b8 |
12 | Catalyst::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 | |
26 | Enable caching support using L<DBIx::Class::Cursor::Cached> and |
27 | L<Catalyst::Plugin::Cache>. |
28 | |
29 | In order for this to work, L<Catalyst::Plugin::Cache> must be configured and |
30 | loaded. A possible configuration would look like this: |
31 | |
32 | <Plugin::Cache> |
33 | <backend> |
34 | class Cache::FastMmap |
35 | unlink_on_exit 1 |
36 | </backend> |
37 | </Plugin::Cache> |
38 | |
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.: |
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 | |
48 | Turn caching on or off, you can use: |
49 | |
50 | $c->model('DB')->caching(0); |
51 | |
fdd5f3df |
52 | =cut |
53 | |
c34bcab6 |
54 | has caching => (is => 'rw', isa => Int, default => 1); |
fdd5f3df |
55 | |
56 | after 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 | |
82 | before 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 | |
108 | L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>, L<Catalyst::Plugin::Cache>, |
109 | L<Cache::FastMmap>, L<DBIx::Class::Cursor::Cached> |
110 | |
111 | =head1 AUTHOR |
112 | |
c7d7b849 |
113 | Rafael Kitover, C<rkitover at cpan.org> |
fdd5f3df |
114 | |
115 | =head1 COPYRIGHT |
116 | |
117 | This program is free software, you can redistribute it and/or modify it |
118 | under the same terms as Perl itself. |
119 | |
120 | =cut |
121 | |
122 | 1; |