| 1 | package CGI::Application::Plugin::Cache::Adaptive; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base qw(Exporter); | ||
| 7 | use Attribute::Handlers; | ||
| 8 | use Cache::Adaptive; | ||
| 9 | use Carp qw(croak); | ||
| 10 | use Class::Inspector; | ||
| 11 | use Storable qw(freeze); | ||
| 12 | |||
| 13 | our @EXPORT = qw(&cache_adaptive); | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | CGI::Application::Plugin::Cache::Adaptive - Provide cacheable to method using attribute. | ||
| 18 | |||
| 19 | =head1 VERSION | ||
| 20 | |||
| 21 | version 0.03 | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | our $VERSION = '0.03'; | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | package MyApp; | ||
| 30 | use base qw(CGI::Application); | ||
| 31 | use CGI::Application::Plugin::Cache::Adaptive; | ||
| 32 | |||
| 33 | use Cache::FileCache; | ||
| 34 | |||
| 35 | sub setup { | ||
| 36 | my $self = shift; | ||
| 37 | |||
| 38 | $self->start_mode('default'); | ||
| 39 | $self->run_modes( | ||
| 40 | 'default' => 'do_default' | ||
| 41 | ); | ||
| 42 | |||
| 43 | $self->cache_adaptive({ | ||
| 44 | backend => Cache::FileCache->new({ | ||
| 45 | namespace => 'html_cache', | ||
| 46 | max_size => 10 * 1024 * 1024, | ||
| 47 | }), | ||
| 48 | expires_min => 3, | ||
| 49 | expires_max => 60, | ||
| 50 | check_load => sub { | ||
| 51 | my $entry = shift; | ||
| 52 | int($entry->{process_time} * 2) - 1; | ||
| 53 | }, | ||
| 54 | }); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub do_default : Cacheable(qw/path path_info query/) { | ||
| 58 | sleep 10; | ||
| 59 | return "test"; | ||
| 60 | } | ||
| 61 | |||
| 62 | =head1 DESCRIPTION | ||
| 63 | |||
| 64 | This module provides adaptive cache to specified method by 'Cacheable' attribute. | ||
| 65 | Cache mechanism depends on L<Cache::Adaptive>. | ||
| 66 | |||
| 67 | =head1 USAGE | ||
| 68 | |||
| 69 | =head2 Cacheable attribute | ||
| 70 | |||
| 71 | Cacheable attribute is able to accept some arguments. | ||
| 72 | The arguments effects L<Cache::Adaptive> behavior. | ||
| 73 | |||
| 74 | The arguments must be array or hash reference. | ||
| 75 | See below the arguments detail. | ||
| 76 | |||
| 77 | =over 4 | ||
| 78 | |||
| 79 | =item Array arguments | ||
| 80 | |||
| 81 | Example, | ||
| 82 | |||
| 83 | sub do_something : Cacheable(qw/path session/) { | ||
| 84 | # process by user | ||
| 85 | } | ||
| 86 | |||
| 87 | Array arguments accepts 4 items, | ||
| 88 | |||
| 89 | =over 4 | ||
| 90 | |||
| 91 | =item path | ||
| 92 | |||
| 93 | Add path(script_name) to cache key salt. | ||
| 94 | |||
| 95 | =item query | ||
| 96 | |||
| 97 | Add query string to cache key salt. | ||
| 98 | |||
| 99 | =item path_info | ||
| 100 | |||
| 101 | Add path_info to cache key salt. | ||
| 102 | |||
| 103 | =item session | ||
| 104 | |||
| 105 | Add session_id to cache key salt. | ||
| 106 | |||
| 107 | =back | ||
| 108 | |||
| 109 | =item Hash reference arguments | ||
| 110 | |||
| 111 | Example, | ||
| 112 | |||
| 113 | sub do_something : Cacheable({key_from => [qw/path path_info/], label => 'memcached'}) { | ||
| 114 | # some process | ||
| 115 | } | ||
| 116 | |||
| 117 | Hash reference accepts 2 original key and any key permitted by L<Cache::Adaptive>'s access method. | ||
| 118 | |||
| 119 | =over 4 | ||
| 120 | |||
| 121 | =item key_from | ||
| 122 | |||
| 123 | Same as array arguments. See L<CGI::Application::Plugin::Cache::Adaptive/Array arguments>. | ||
| 124 | |||
| 125 | =item label | ||
| 126 | |||
| 127 | Change cache profile to specified labeled cache object. | ||
| 128 | See L<Cache::Adaptive/cache_adaptive($label)>. | ||
| 129 | |||
| 130 | =back | ||
| 131 | |||
| 132 | =back | ||
| 133 | |||
| 134 | =head1 METHODS | ||
| 135 | |||
| 136 | =head2 cache_adaptive() | ||
| 137 | |||
| 138 | Alias cache_adaptive('default'). | ||
| 139 | See L<CGI::Application::Plugin::Cache::Adaptive/cache_adaptive($label)> | ||
| 140 | |||
| 141 | =head2 cache_adaptive($label) | ||
| 142 | |||
| 143 | Get L<Cache::Adaptive> object by label. | ||
| 144 | |||
| 145 | =head2 cache_adaptive($hash_ref) | ||
| 146 | |||
| 147 | Set L<Cache::Adaptive> object to 'default' label. | ||
| 148 | The $hash_ref is L<Cache::Adaptive>'s new parameter. | ||
| 149 | |||
| 150 | =head2 cache_adaptive($cache_obj) | ||
| 151 | |||
| 152 | Set L<Cache::Adaptive> or that inheritance object to 'default' label. | ||
| 153 | |||
| 154 | =head2 cache_adaptive($label, $hash_ref) | ||
| 155 | |||
| 156 | Set L<Cache::Adaptive> object to specified label. | ||
| 157 | The $hash_ref is L<Cache::Adaptive>'s new parameter. | ||
| 158 | |||
| 159 | =head2 cache_adaptive($label, $cache_obj) | ||
| 160 | |||
| 161 | Set L<Cache::Adaptive> or that inheritance object to specified label. | ||
| 162 | |||
| 163 | =cut | ||
| 164 | |||
| 165 | sub cache_adaptive { | ||
| 166 | my $self = shift; | ||
| 167 | |||
| 168 | if (@_ == 2) { | ||
| 169 | if (UNIVERSAL::isa($_[1], 'Cache::Adaptive')) { | ||
| 170 | $self->{'Cache::Adaptive::cache_adaptive'}{$_[0]} = $_[1]; | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | $self->{'Cache::Adaptive::cache_adaptive'}{$_[0]} = Cache::Adaptive->new($_[1]); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | elsif (@_ == 1) { | ||
| 177 | if (UNIVERSAL::isa($_[0], 'Cache::Adaptive')) { | ||
| 178 | return $self->{'Cache::Adaptive::cache_adaptive'}{'default'} = $_[0]; | ||
| 179 | } | ||
| 180 | elsif (ref $_[0] eq 'HASH') { | ||
| 181 | $self->{'Cache::Adaptive::cache_adaptive'}{'default'} = Cache::Adaptive->new($_[0]); | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | return $self->{'Cache::Adaptive::cache_adaptive'}{$_[0]}; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | else { | ||
| 188 | return $self->{'Cache::Adaptive::cache_adaptive'}{'default'}; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | =head2 CGI::Application::Cacheable() | ||
| 193 | |||
| 194 | Provide cacheable to specified method. | ||
| 195 | See L<Attribute::Handlers> | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | sub CGI::Application::Cacheable : ATTR(CODE,BEGIN) { | ||
| 200 | my ($package, $symbol, $referent, $attr, $data, $phase) = @_; | ||
| 201 | |||
| 202 | my %key_index = ( | ||
| 203 | 'path' => 0, | ||
| 204 | 'query' => 1, | ||
| 205 | 'path_info' => 2, | ||
| 206 | 'session' => 3 | ||
| 207 | ); | ||
| 208 | |||
| 209 | $package->add_callback('init', | ||
| 210 | sub { | ||
| 211 | my $self = shift; | ||
| 212 | |||
| 213 | ### In the next process, runmode will be rededined. | ||
| 214 | ### In CAP::AutoRunmode, it was cached code reference before redefined. | ||
| 215 | ### Therefore now start_mode and error_mode must be static value. | ||
| 216 | if (Class::Inspector->loaded('CGI::Application::Plugin::AutoRunmode')) { | ||
| 217 | $self->start_mode; | ||
| 218 | $self->error_mode; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | ); | ||
| 222 | |||
| 223 | $package->add_callback('init', | ||
| 224 | sub { | ||
| 225 | my ($self, @args) = @_; | ||
| 226 | |||
| 227 | $data = {} unless ($data); | ||
| 228 | my $data_type = ref $data; | ||
| 229 | |||
| 230 | if ($data_type eq 'ARRAY' || $data_type eq 'HASH') { | ||
| 231 | my $label = ($data_type eq 'HASH') ? delete $data->{label} || 'default' : 'default'; | ||
| 232 | my $key_from = ($data_type eq 'HASH') ? delete $data->{key_from} : $data; | ||
| 233 | $key_from = [qw(path)] if (!$key_from || @$key_from == 0); | ||
| 234 | |||
| 235 | my %extra_params = ($data_type eq 'HASH') ? %$data : (); | ||
| 236 | |||
| 237 | my $method = (grep { $package->can($_) == $referent } @{Class::Inspector->methods($package)})[0]; | ||
| 238 | return unless ($method); | ||
| 239 | |||
| 240 | { | ||
| 241 | no strict 'refs'; | ||
| 242 | no warnings 'redefine'; | ||
| 243 | |||
| 244 | *{$package . "::" . $method} = sub { | ||
| 245 | my ($self, @args) = @_; | ||
| 246 | |||
| 247 | local $CGI::USE_PARAM_SEMICOLONS = 0; | ||
| 248 | my @key_array = (undef, undef, undef, undef); | ||
| 249 | |||
| 250 | for my $key (grep { exists $key_index{$_} } @$key_from) { | ||
| 251 | my $value = undef; | ||
| 252 | |||
| 253 | $value = $self->query->script_name if ($key eq 'path'); | ||
| 254 | $value = $self->query->query_string if ($key eq 'query'); | ||
| 255 | $value = $self->query->path_info if ($key eq 'path_info'); | ||
| 256 | $value = ($self->can('session')) ? $self->session->id : undef if ($key eq 'session'); | ||
| 257 | |||
| 258 | $key_array[$key_index{$key}] = $value if (exists $key_index{$key} && defined $value); | ||
| 259 | } | ||
| 260 | |||
| 261 | # { | ||
| 262 | # my %debug = (); | ||
| 263 | # $debug{$_} = $key_array[$key_index{$_}] for (keys %key_index); | ||
| 264 | # | ||
| 265 | # $self->cache_adaptive($label)->log->(\%debug); | ||
| 266 | # } | ||
| 267 | |||
| 268 | return $self->cache_adaptive($label)->access({ | ||
| 269 | key => freeze(\@key_array), | ||
| 270 | builder => sub { | ||
| 271 | return $referent->($self, @args); | ||
| 272 | }, | ||
| 273 | %extra_params | ||
| 274 | }); | ||
| 275 | }; | ||
| 276 | |||
| 277 | ### If using CAP::AutoRunmode, it's code reference cache table must be refleshed. | ||
| 278 | if (Class::Inspector->loaded('CGI::Application::Plugin::AutoRunmode')) { | ||
| 279 | if (exists $CGI::Application::Plugin::AutoRunmode::RUNMODES{"$referent"}) { | ||
| 280 | delete $CGI::Application::Plugin::AutoRunmode::RUNMODES{"$referent"}; | ||
| 281 | $CGI::Application::Plugin::AutoRunmode::RUNMODES{$package->can($method)} = 1; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | } | ||
| 285 | } | ||
| 286 | } | ||
| 287 | ); | ||
| 288 | } | ||
| 289 | |||
| 290 | =head1 SEE ALSO | ||
| 291 | |||
| 292 | =over 4 | ||
| 293 | |||
| 294 | =item L<Cache::Adaptive> | ||
| 295 | |||
| 296 | =item L<CGI::Application> | ||
| 297 | |||
| 298 | =item L<CGI::Application::Plugin::AutoRunmode> | ||
| 299 | |||
| 300 | =item L<Attribute::Handlers> | ||
| 301 | |||
| 302 | =back | ||
| 303 | |||
| 304 | =head1 AUTHOR | ||
| 305 | |||
| 306 | Toru Yamaguchi, C<< <zigorou@cpan.org> >> | ||
| 307 | |||
| 308 | =head1 THANKS | ||
| 309 | |||
| 310 | Kazuho Oku, C<< <kazuhooku@gmail.com> >> | ||
| 311 | |||
| 312 | =head1 BUGS | ||
| 313 | |||
| 314 | Please report any bugs or feature requests to | ||
| 315 | C<bug-cgi-application-plugin-cache-adaptive@rt.cpan.org>, or through the web interface at | ||
| 316 | L<http://rt.cpan.org>. I will be notified, and then you'll automatically be | ||
| 317 | notified of progress on your bug as I make changes. | ||
| 318 | |||
| 319 | =head1 COPYRIGHT & LICENSE | ||
| 320 | |||
| 321 | Copyright 2007 Toru Yamaguchi, All Rights Reserved. | ||
| 322 | |||
| 323 | This program is free software; you can redistribute it and/or modify it | ||
| 324 | under the same terms as Perl itself. | ||
| 325 | |||
| 326 | =cut | ||
| 327 | |||
| 328 | 1; # End of CGI::Application::Plugin::Cache::Adaptive |