Commit | Line | Data |
fdd5f3df |
1 | package Catalyst::Model::DBIC::Schema::Role::Caching; |
2 | |
3 | use Moose::Role; |
4 | use Carp::Clan '^Catalyst::Model::DBIC::Schema'; |
5 | |
6 | use namespace::clean -except => 'meta'; |
7 | |
8 | =head1 NAME |
9 | |
10 | Catalyst::Model::DBIC::Schema::Role::Caching - Query caching support for |
c4fee9b8 |
11 | Catalyst::Model::DBIC::Schema |
fdd5f3df |
12 | |
13 | =head1 SYNOPSIS |
14 | |
15 | __PACKAGE__->config({ |
16 | roles => ['Caching'] |
17 | ... |
18 | }); |
19 | |
20 | ... |
21 | |
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 | |
52 | to disable caching at runtime. |
53 | |
fdd5f3df |
54 | =cut |
55 | |
56 | has 'caching' => (is => 'rw', isa => 'Int', default => 1); |
57 | |
58 | after setup => sub { |
59 | my $self = shift; |
60 | |
61 | return if defined $self->caching && !$self->caching; |
62 | |
63 | $self->caching(0); |
64 | |
65 | if (my $cursor_class = $self->connect_info->{cursor_class}) { |
66 | unless ($cursor_class->can('clear_cache')) { |
67 | carp "Caching disabled, cursor_class $cursor_class does not" |
68 | . " support it."; |
69 | return; |
70 | } |
71 | } else { |
72 | my $cursor_class = 'DBIx::Class::Cursor::Cached'; |
73 | |
74 | unless (eval { Class::MOP::load_class($cursor_class) }) { |
75 | carp "Caching disabled, cannot load $cursor_class: $@"; |
76 | return; |
77 | } |
78 | |
79 | $self->connect_info->{cursor_class} = $cursor_class; |
80 | } |
81 | |
82 | $self->caching(1); |
83 | }; |
84 | |
85 | before ACCEPT_CONTEXT => sub { |
86 | my ($self, $c) = @_; |
87 | |
88 | return $self unless |
89 | $self->caching; |
90 | |
91 | unless ($c->can('cache') && ref $c->cache) { |
92 | $c->log->warn("DBIx::Class cursor caching disabled, you don't seem to" |
93 | . " have a working Cache plugin."); |
94 | $self->caching(0); |
95 | $self->_reset_cursor_class; |
96 | return $self; |
97 | } |
98 | |
99 | if (ref $self->schema->default_resultset_attributes) { |
100 | $self->schema->default_resultset_attributes->{cache_object} = |
101 | $c->cache; |
102 | } else { |
103 | $self->schema->default_resultset_attributes({ |
104 | cache_object => $c->cache |
105 | }); |
106 | } |
107 | }; |
108 | |
109 | =head1 METHODS |
110 | |
c4fee9b8 |
111 | =head2 _reset_cursor_class |
fdd5f3df |
112 | |
113 | Reset the cursor class to L<DBIx::Class::Storage::DBI::Cursor> if it's set to |
c4fee9b8 |
114 | L<DBIx::Class::Cursor::Cached>, if possible. |
fdd5f3df |
115 | |
116 | =cut |
117 | |
118 | sub _reset_cursor_class { |
119 | my $self = shift; |
120 | |
121 | if ($self->connect_info->{cursor_class} eq 'DBIx::Class::Cursor::Cached') { |
c4fee9b8 |
122 | $self->storage->cursor_class('DBIx::Class::Storage::DBI::Cursor') |
123 | if $self->storage->can('cursor_class'); |
fdd5f3df |
124 | } |
125 | |
126 | 1; |
127 | } |
128 | |
fdd5f3df |
129 | =head1 SEE ALSO |
130 | |
131 | L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>, L<Catalyst::Plugin::Cache>, |
132 | L<Cache::FastMmap>, L<DBIx::Class::Cursor::Cached> |
133 | |
134 | =head1 AUTHOR |
135 | |
136 | Rafael Kitover, C<rkitover@cpan.org> |
137 | |
138 | =head1 COPYRIGHT |
139 | |
140 | This program is free software, you can redistribute it and/or modify it |
141 | under the same terms as Perl itself. |
142 | |
143 | =cut |
144 | |
145 | 1; |