X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FPlugin%2FCache.pm;h=15a73fafa64a0ea3600d9a1476dd27d386aa40ac;hb=23b2d59b8f60ce3913d9ba13e22ac2ae3a15d9fe;hp=f8bd7a8ad1ce5de981482c2f1ecd69e6a82b529d;hpb=aed484dac6fde4b3d17cebfaccea5c14560f8ce9;p=catagits%2FCatalyst-Plugin-Cache.git diff --git a/lib/Catalyst/Plugin/Cache.pm b/lib/Catalyst/Plugin/Cache.pm index f8bd7a8..15a73fa 100644 --- a/lib/Catalyst/Plugin/Cache.pm +++ b/lib/Catalyst/Plugin/Cache.pm @@ -7,6 +7,7 @@ use strict; use warnings; use Scalar::Util (); +use Catalyst::Utils (); use Carp (); use NEXT; @@ -29,8 +30,75 @@ sub setup { $ret; } -# don't die even if we don't have cache backends -sub setup_cache_backends { shift->NEXT::setup_cache_backends(@_) } +sub get_default_cache_backend_config { + my ( $app, $name ) = @_; + $app->config->{cache}{backend} || $app->get_cache_backend_config("default"); +} + +sub get_cache_backend_config { + my ( $app, $name ) = @_; + $app->config->{cache}{backends}{$name}; +} + +sub setup_cache_backends { + my $app = shift; + + # give plugins a chance to find things for themselves + $app->NEXT::setup_cache_backends; + + foreach my $name ( keys %{ $app->config->{cache}{backends} } ) { + next if $app->get_cache_backend( $name ); + $app->setup_generic_cache_backend( $name, $app->get_cache_backend_config( $name ) || {} ); + } + + if ( !$app->get_cache_backend("default") and my $default_config = $app->get_default_cache_backend_config) { + $app->setup_generic_cache_backend( default => $default_config ); + } +} + +sub default_cache_store { + my $app = shift; + $app->config->{cache}{default_store} || $app->guess_default_cache_store; +} + +sub guess_default_cache_store { + my $app = shift; + + my @stores = map { /Cache::Store::(.*)$/ ? $1 : () } $app->registered_plugins; + + if ( @stores == 1 ) { + return $stores[0]; + } else { + Carp::croak "You must configure a default store type unless you use exactly one store plugin."; + } +} + +sub setup_generic_cache_backend { + my ( $app, $name, $config ) = @_; + my %config = %$config; + + if ( my $class = delete $config{class} ) { + $app->setup_cache_backend_by_class( $name, $class, %config ); + } elsif ( my $store = delete $config->{store} || $app->default_cache_store ) { + my $method = lc("setup_${store}_cache_backend"); + + Carp::croak "You must load the $store cache store plugin (if it exists). ". + "Please consult the Catalyst::Plugin::Cache documentation on how to configure hetrogeneous stores." + unless $app->can($method); + + $app->$method( $name, %config ); + } else { + $app->log->warn("Couldn't setup the cache backend named '$name'"); + } +} + +sub setup_cache_backend_by_class { + my ( $app, $name, $class, @args ) = @_; + Catalyst::Utils::ensure_class_loaded( $class ); + $app->register_cache_backend( $name => $class->new( @args ) ); +} + +# end of spaghetti setup DWIM sub cache { my ( $c, @meta ) = @_; @@ -160,6 +228,26 @@ Catalyst::Plugin::Cache - =head1 DESCRIPTION +=head1 TERMINOLIGY + +=over 4 + +=item backend + +An object that responds to the methods detailed in +L (or more). + +=item store + +A generic "type" of backend. Typically a plugin used to construct backends. + +=item curried cache + +An object that responds to C, C and C, and will automatically +add meta data to calls to C<< $c->cache_get >>, etc. + +=back + =cut