use strict;
use warnings;
-our $VERSION = "0.04";
+our $VERSION = "0.08";
use Scalar::Util ();
use Catalyst::Utils ();
use Carp ();
-use NEXT;
+use MRO::Compat;
use Catalyst::Plugin::Cache::Curried;
# and don't overwrite if some plugin was wicked
$app->_cache_backends({}) unless $app->_cache_backends;
- my $ret = $app->NEXT::setup( @_ );
+ my $ret = $app->maybe::next::method( @_ );
$app->setup_cache_backends;
$ret;
}
+sub _get_cache_plugin_config {
+ my ($app) = @_;
+ return $app->config->{'Plugin::Cache'} || $app->config->{cache};
+}
+
sub get_default_cache_backend_config {
my ( $app, $name ) = @_;
- $app->config->{cache}{backend} || $app->get_cache_backend_config("default");
+ $app->_get_cache_plugin_config->{backend} || $app->get_cache_backend_config("default");
}
sub get_cache_backend_config {
my ( $app, $name ) = @_;
- $app->config->{cache}{backends}{$name};
+ $app->_get_cache_plugin_config->{backends}{$name};
}
sub setup_cache_backends {
my $app = shift;
# give plugins a chance to find things for themselves
- $app->NEXT::setup_cache_backends;
+ $app->maybe::next::method;
- foreach my $name ( keys %{ $app->config->{cache}{backends} } ) {
+ # FIXME - Don't know why the _get_cache_plugin_config method doesn't work here!
+ my $conf = $app->config->{'Plugin::Cache'} ? $app->config->{'Plugin::Cache'}->{backends} : $app->config->{cache}->{backends};
+ foreach my $name ( keys %$conf ) {
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") ) {
- local $@;
- eval { $app->setup_generic_cache_backend( default => $app->get_default_cache_backend_config || {} ) };
+ ### XXX currently we dont have a fallback scenario
+ ### so die here with the error message. Once we have
+ ### an in memory fallback, we may consider silently
+ ### logging the error and falling back to that.
+ ### If we dont die here, the app will silently start
+ ### up and then explode at the first cache->get or
+ ### cache->set request with a FIXME error
+ #local $@;
+ #eval {
+ $app->setup_generic_cache_backend( default => $app->get_default_cache_backend_config || {} );
+ #};
+
}
}
sub default_cache_store {
my $app = shift;
- $app->config->{cache}{default_store} || $app->guess_default_cache_store;
+ $app->_get_cache_plugin_config->{default_store} || $app->guess_default_cache_store;
}
sub guess_default_cache_store {
my %config = %$config;
if ( my $class = delete $config{class} ) {
- eval { $app->setup_cache_backend_by_class( $name, $class, %config ) }
- ||
- eval { $app->setup_cache_backend_by_class( $name, $class, \%config ) }
- ||
- die "Couldn't construct $class with either list style or hash ref style param passing: $@";
+
+ ### try as list and as hashref, collect the
+ ### error if things go wrong
+ ### if all goes well, exit the loop
+ my @errors;
+ for my $aref ( [%config], [\%config] ) {
+ eval { $app->setup_cache_backend_by_class(
+ $name, $class, @$aref
+ );
+ } ? do { @errors = (); last }
+ : push @errors, "\t$@";
+ }
+
+ ### and die with the errors if we have any
+ die "Couldn't construct $class with either list style or hash ref style param passing:\n @errors" if @errors;
+
} elsif ( my $store = delete $config->{store} || $app->default_cache_store ) {
my $method = lc("setup_${store}_cache_backend");
sub curried_cache_class {
my ( $c, @meta ) = @_;
- $c->config->{cache}{curried_class} || "Catalyst::Plugin::Cache::Curried";
+ $c->_get_cache_plugin_config->{curried_class} || "Catalyst::Plugin::Cache::Curried";
}
sub curry_cache {
sub get_preset_curried {
my ( $c, $name ) = @_;
- if ( ref( my $preset = $c->config->{cache}{profiles}{$name} ) ) {
+ if ( ref( my $preset = $c->_get_cache_plugin_config->{profiles}{$name} ) ) {
return $preset if Scalar::Util::blessed($preset);
my @meta = ( ( ref $preset eq "HASH" ) ? %$preset : @$preset );
return $c->default_cache_backend;
}
-sub choose_cache_backend { shift->NEXT::choose_cache_backend( @_ ) } # a convenient fallback
+sub choose_cache_backend { shift->maybe::next::method( @_ ) } # a convenient fallback
sub cache_set {
my ( $c, $key, $value, %meta ) = @_;
/;
# configure a backend or use a store plugin
- __PACKAGE__->config->{cache}{backend} = {
+ __PACKAGE__->config->{'Plugin::Cache'}{backend} = {
class => "Cache::Bounded",
- # ... params ...
+ # ... params for Cache::Bounded...
};
+ # typical example for Cache::Memcached::libmemcached
+ __PACKAGE__->config->{'Plugin::Cache'}{backend} = {
+ class => "Cache::Memcached::libmemcached",
+ servers => ['127.0.0.1:11211'],
+ debug => 2,
+ };
+
+
# In a controller:
sub foo : Local {
=head1 CONFIGURATION
- $c->config->{cache} = {
+ $c->config->{'Plugin::Cache'} = {
...
};
All configuration parameters should be provided in a hash reference
-under the C<cache> key in the C<config> hash.
+under the C<Plugin::Cache> key in the C<config> hash.
=head2 Backend Configuration
Instantiate a backend from a L<Cache> compatible class. E.g.
- $c->config->{cache}{backends}{small_things} = {
+ $c->config->{'Plugin::Cache'}{backends}{small_things} = {
class => "Cache::Bounded",
interval => 1000,
size => 10000,
};
- $c->config->{cache}{backends}{large_things} = {
+ $c->config->{'Plugin::Cache'}{backends}{large_things} = {
class => "Cache::Memcached",
data => '1.2.3.4:1234',
};
Instantiate a backend using a store plugin, e.g.
- $c->config->{cache}{backend} = {
+ $c->config->{'Plugin::Cache'}{backend} = {
store => "FastMmap",
};
For example when you specify
- $c->config->{cache}{profiles}{thumbnails} = {
+ $c->config->{'Plugin::Cache'}{profiles}{thumbnails} = {
backend => "large_things",
};
Yuval Kogman, C<nothingmuch@woobling.org>
+Jos Boumans, C<kane@cpan.org>
+
=head1 COPYRIGHT & LICENSE
Copyright (c) Yuval Kogman, 2006. All rights reserved.