| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package Catalyst::Plugin::Cache; | ||
| 4 | 5 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | with 'Catalyst::ClassData'; | ||
| 7 | |||
| 8 | our $VERSION = "0.12"; | ||
| 9 | |||
| 10 | 5 | ▶ | use Scalar::Util (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 5 | ▶ | use Catalyst::Utils (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 5 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 5 | ▶ | use MRO::Compat; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 5 | ▶ | use Scalar::Util qw/ blessed /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 0 | ▶ | use Catalyst::Plugin::Cache::Curried; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 16 | |||
| 17 | __PACKAGE__->mk_classdata( "_cache_backends" ); | ||
| 18 | has _default_curried_cache => ( | ||
| 19 | is => 'rw', | ||
| 20 | ); | ||
| 21 | no Moose; | ||
| 22 | |||
| 23 | sub setup { | ||
| 24 | my $app = shift; | ||
| 25 | |||
| 26 | # set it once per app, not once per plugin, | ||
| 27 | # and don't overwrite if some plugin was wicked | ||
| 28 | $app->_cache_backends({}) unless $app->_cache_backends; | ||
| 29 | |||
| 30 | my $ret = $app->maybe::next::method( @_ ); | ||
| 31 | |||
| 32 | $app->setup_cache_backends; | ||
| 33 | |||
| 34 | $ret; | ||
| 35 | } | ||
| 36 | { | ||
| 37 | my %has_warned_for; | ||
| 38 | sub _get_cache_plugin_config { | ||
| 39 | my ($app) = @_; | ||
| 40 | my $config = $app->config->{'Plugin::Cache'}; | ||
| 41 | if (!$config) { | ||
| 42 | $config = $app->config->{cache}; | ||
| 43 | my $appname = ref($app); | ||
| 44 | if (! $has_warned_for{$appname}++ ) { | ||
| 45 | $app->log->warn($config ? | ||
| 46 | 'Catalyst::Plugin::Cache config found in deprecated $c->config->{cache}, please move to $c->config->{"Plugin::Cache"}.' | ||
| 47 | : 'Catalyst::Plugin::Cache config not found, using empty config!' | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | return $config || {}; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | sub get_default_cache_backend_config { | ||
| 56 | my ( $app, $name ) = @_; | ||
| 57 | $app->_get_cache_plugin_config->{backend} || $app->get_cache_backend_config("default"); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub get_cache_backend_config { | ||
| 61 | my ( $app, $name ) = @_; | ||
| 62 | $app->_get_cache_plugin_config->{backends}{$name}; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub setup_cache_backends { | ||
| 66 | my $app = shift; | ||
| 67 | |||
| 68 | # give plugins a chance to find things for themselves | ||
| 69 | $app->maybe::next::method; | ||
| 70 | |||
| 71 | # FIXME - Don't know why the _get_cache_plugin_config method doesn't work here! | ||
| 72 | my $conf = $app->_get_cache_plugin_config->{backends}; | ||
| 73 | foreach my $name ( keys %$conf ) { | ||
| 74 | next if $app->get_cache_backend( $name ); | ||
| 75 | $app->setup_generic_cache_backend( $name, $app->get_cache_backend_config( $name ) || {} ); | ||
| 76 | } | ||
| 77 | |||
| 78 | if ( !$app->get_cache_backend("default") ) { | ||
| 79 | ### XXX currently we dont have a fallback scenario | ||
| 80 | ### so die here with the error message. Once we have | ||
| 81 | ### an in memory fallback, we may consider silently | ||
| 82 | ### logging the error and falling back to that. | ||
| 83 | ### If we dont die here, the app will silently start | ||
| 84 | ### up and then explode at the first cache->get or | ||
| 85 | ### cache->set request with a FIXME error | ||
| 86 | #local $@; | ||
| 87 | #eval { | ||
| 88 | $app->setup_generic_cache_backend( default => $app->get_default_cache_backend_config || {} ); | ||
| 89 | #}; | ||
| 90 | |||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | sub default_cache_store { | ||
| 95 | my $app = shift; | ||
| 96 | $app->_get_cache_plugin_config->{default_store} || $app->guess_default_cache_store; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub guess_default_cache_store { | ||
| 100 | my $app = shift; | ||
| 101 | |||
| 102 | my @stores = map { /Cache::Store::(.*)$/ ? $1 : () } $app->registered_plugins; | ||
| 103 | |||
| 104 | if ( @stores == 1 ) { | ||
| 105 | return $stores[0]; | ||
| 106 | } else { | ||
| 107 | Carp::croak "You must configure a default store type unless you use exactly one store plugin."; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | sub setup_generic_cache_backend { | ||
| 112 | my ( $app, $name, $config ) = @_; | ||
| 113 | my %config = %$config; | ||
| 114 | |||
| 115 | if ( my $class = delete $config{class} ) { | ||
| 116 | |||
| 117 | ### try as list and as hashref, collect the | ||
| 118 | ### error if things go wrong | ||
| 119 | ### if all goes well, exit the loop | ||
| 120 | my @errors; | ||
| 121 | for my $aref ( [%config], [\%config] ) { | ||
| 122 | eval { $app->setup_cache_backend_by_class( | ||
| 123 | $name, $class, @$aref | ||
| 124 | ); | ||
| 125 | } ? do { @errors = (); last } | ||
| 126 | : push @errors, "\t$@"; | ||
| 127 | } | ||
| 128 | |||
| 129 | ### and die with the errors if we have any | ||
| 130 | die "Couldn't construct $class with either list style or hash ref style param passing:\n @errors" if @errors; | ||
| 131 | |||
| 132 | } elsif ( my $store = delete $config->{store} || $app->default_cache_store ) { | ||
| 133 | my $method = lc("setup_${store}_cache_backend"); | ||
| 134 | |||
| 135 | Carp::croak "You must load the $store cache store plugin (if it exists). ". | ||
| 136 | "Please consult the Catalyst::Plugin::Cache documentation on how to configure hetrogeneous stores." | ||
| 137 | unless $app->can($method); | ||
| 138 | |||
| 139 | $app->$method( $name, \%config ); | ||
| 140 | } else { | ||
| 141 | $app->log->warn("Couldn't setup the cache backend named '$name'"); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | sub setup_cache_backend_by_class { | ||
| 146 | my ( $app, $name, $class, @args ) = @_; | ||
| 147 | Catalyst::Utils::ensure_class_loaded( $class ); | ||
| 148 | $app->register_cache_backend( $name => $class->new( @args ) ); | ||
| 149 | } | ||
| 150 | |||
| 151 | # end of spaghetti setup DWIM | ||
| 152 | |||
| 153 | sub cache { | ||
| 154 | my ( $c, @meta ) = @_; | ||
| 155 | |||
| 156 | if ( @meta == 1 ) { | ||
| 157 | my $name = $meta[0]; | ||
| 158 | return ( $c->get_preset_curried($name) || $c->get_cache_backend($name) ); | ||
| 159 | } elsif ( !@meta && blessed $c ) { | ||
| 160 | # be nice and always return the same one for the simplest case | ||
| 161 | return ( $c->_default_curried_cache || $c->_default_curried_cache( $c->curry_cache( @meta ) ) ); | ||
| 162 | } else { | ||
| 163 | return $c->curry_cache( @meta ); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | sub construct_curried_cache { | ||
| 168 | my ( $c, @meta ) = @_; | ||
| 169 | return $c->curried_cache_class( @meta )->new( @meta ); | ||
| 170 | } | ||
| 171 | |||
| 172 | sub curried_cache_class { | ||
| 173 | my ( $c, @meta ) = @_; | ||
| 174 | $c->_get_cache_plugin_config->{curried_class} || "Catalyst::Plugin::Cache::Curried"; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub curry_cache { | ||
| 178 | my ( $c, @meta ) = @_; | ||
| 179 | return $c->construct_curried_cache( $c, $c->_cache_caller_meta, @meta ); | ||
| 180 | } | ||
| 181 | |||
| 182 | sub get_preset_curried { | ||
| 183 | my ( $c, $name ) = @_; | ||
| 184 | |||
| 185 | if ( ref( my $preset = $c->_get_cache_plugin_config->{profiles}{$name} ) ) { | ||
| 186 | return $preset if Scalar::Util::blessed($preset); | ||
| 187 | |||
| 188 | my @meta = ( ( ref $preset eq "HASH" ) ? %$preset : @$preset ); | ||
| 189 | return $c->curry_cache( @meta ); | ||
| 190 | } | ||
| 191 | |||
| 192 | return; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub get_cache_backend { | ||
| 196 | my ( $c, $name ) = @_; | ||
| 197 | $c->_cache_backends->{$name}; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub register_cache_backend { | ||
| 201 | my ( $c, $name, $backend ) = @_; | ||
| 202 | |||
| 203 | no warnings 'uninitialized'; | ||
| 204 | Carp::croak("$backend does not look like a cache backend - " | ||
| 205 | . "it must be an object supporting get, set and remove") | ||
| 206 | unless eval { $backend->can("get") && $backend->can("set") && $backend->can("remove") }; | ||
| 207 | |||
| 208 | $c->_cache_backends->{$name} = $backend; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub unregister_cache_backend { | ||
| 212 | my ( $c, $name ) = @_; | ||
| 213 | delete $c->_cache_backends->{$name}; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub default_cache_backend { | ||
| 217 | my $c = shift; | ||
| 218 | $c->get_cache_backend( "default" ) || $c->temporary_cache_backend; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub temporary_cache_backend { | ||
| 222 | my $c = shift; | ||
| 223 | die "FIXME - make up an in memory cache backend, that hopefully works well for the current engine"; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub _cache_caller_meta { | ||
| 227 | my $c = shift; | ||
| 228 | |||
| 229 | my ( $caller, $component, $controller ); | ||
| 230 | |||
| 231 | for my $i ( 0 .. 15 ) { # don't look to far | ||
| 232 | my @info = caller(2 + $i) or last; | ||
| 233 | |||
| 234 | $caller ||= \@info unless $info[0] =~ /Plugin::Cache/; | ||
| 235 | $component ||= \@info if $info[0]->isa("Catalyst::Component"); | ||
| 236 | $controller ||= \@info if $info[0]->isa("Catalyst::Controller"); | ||
| 237 | |||
| 238 | last if $caller && $component && $controller; | ||
| 239 | } | ||
| 240 | |||
| 241 | my ( $caller_pkg, $component_pkg, $controller_pkg ) = | ||
| 242 | map { $_ ? $_->[0] : undef } $caller, $component, $controller; | ||
| 243 | |||
| 244 | return ( | ||
| 245 | 'caller' => $caller_pkg, | ||
| 246 | component => $component_pkg, | ||
| 247 | controller => $controller_pkg, | ||
| 248 | caller_frame => $caller, | ||
| 249 | component_frame => $component, | ||
| 250 | controller_frame => $controller, | ||
| 251 | ); | ||
| 252 | } | ||
| 253 | |||
| 254 | # this gets a shit name so that the plugins can override a good name | ||
| 255 | sub choose_cache_backend_wrapper { | ||
| 256 | my ( $c, @meta ) = @_; | ||
| 257 | |||
| 258 | Carp::croak("metadata must be an even sized list") unless @meta % 2 == 0; | ||
| 259 | |||
| 260 | my %meta = @meta; | ||
| 261 | |||
| 262 | unless ( exists $meta{'caller'} ) { | ||
| 263 | my %caller = $c->_cache_caller_meta; | ||
| 264 | @meta{keys %caller} = values %caller; | ||
| 265 | } | ||
| 266 | |||
| 267 | # allow the cache client to specify who it wants to cache with (but loeave room for a hook) | ||
| 268 | if ( exists $meta{backend} ) { | ||
| 269 | if ( Scalar::Util::blessed($meta{backend}) ) { | ||
| 270 | return $meta{backend}; | ||
| 271 | } else { | ||
| 272 | return $c->get_cache_backend( $meta{backend} ) || $c->default_cache_backend; | ||
| 273 | } | ||
| 274 | }; | ||
| 275 | |||
| 276 | if ( my $chosen = $c->choose_cache_backend( %meta ) ) { | ||
| 277 | $chosen = $c->get_cache_backend( $chosen ) unless Scalar::Util::blessed($chosen); # if it's a name find it | ||
| 278 | return $chosen if Scalar::Util::blessed($chosen); # only return if it was an object or name lookup worked | ||
| 279 | |||
| 280 | # FIXME | ||
| 281 | # die "no such backend"? | ||
| 282 | # currently, we fall back to default | ||
| 283 | } | ||
| 284 | |||
| 285 | return $c->default_cache_backend; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub choose_cache_backend { shift->maybe::next::method( @_ ) } # a convenient fallback | ||
| 289 | |||
| 290 | sub cache_set { | ||
| 291 | my ( $c, $key, $value, %meta ) = @_; | ||
| 292 | $c->choose_cache_backend_wrapper( key => $key, value => $value, %meta ) | ||
| 293 | ->set( $key, $value, exists $meta{expires} ? $meta{expires} : () ); | ||
| 294 | } | ||
| 295 | |||
| 296 | sub cache_get { | ||
| 297 | my ( $c, $key, @meta ) = @_; | ||
| 298 | $c->choose_cache_backend_wrapper( key => $key, @meta )->get( $key ); | ||
| 299 | } | ||
| 300 | |||
| 301 | sub cache_remove { | ||
| 302 | my ( $c, $key, @meta ) = @_; | ||
| 303 | $c->choose_cache_backend_wrapper( key => $key, @meta )->remove( $key ); | ||
| 304 | } | ||
| 305 | |||
| 306 | sub cache_compute { | ||
| 307 | my ($c, $key, $code, %meta) = @_; | ||
| 308 | |||
| 309 | my $backend = $c->choose_cache_backend_wrapper( key => $key, %meta ); | ||
| 310 | if ($backend->can('compute')) { | ||
| 311 | return $backend->compute( $key, $code, exists $meta{expires} ? $meta{expires} : () ); | ||
| 312 | } | ||
| 313 | |||
| 314 | Carp::croak "must specify key and code" unless defined($key) && defined($code); | ||
| 315 | |||
| 316 | my $value = $c->cache_get( $key, %meta ); | ||
| 317 | if ( !defined $value ) { | ||
| 318 | $value = $code->(); | ||
| 319 | $c->cache_set( $key, $value, %meta ); | ||
| 320 | } | ||
| 321 | return $value; | ||
| 322 | } | ||
| 323 | |||
| 324 | __PACKAGE__; | ||
| 325 | |||
| 326 | __END__ | ||