3 package Catalyst::Plugin::Cache;
4 use base qw/Class::Data::Inheritable Class::Accessor::Fast/;
13 use Catalyst::Plugin::Cache::Curried;
15 __PACKAGE__->mk_classdata( "_cache_backends" );
16 __PACKAGE__->mk_accessors( "_default_curried_cache" );
21 # set it once per app, not once per plugin,
22 # and don't overwrite if some plugin was wicked
23 $app->_cache_backends({}) unless $app->_cache_backends;
25 my $ret = $app->NEXT::setup( @_ );
27 $app->setup_cache_backends;
32 # don't die even if we don't have cache backends
33 sub setup_cache_backends { shift->NEXT::setup_cache_backends(@_) }
36 my ( $c, @meta ) = @_;
40 return ( $c->get_preset_curried($name) || $c->get_cache_backend($name) );
42 # be nice and always return the same one for the simplest case
43 return ( $c->_default_curried_cache || $c->_default_curried_cache( $c->curry_cache( @meta ) ) );
45 return $c->curry_cache( @meta );
50 my ( $c, @meta ) = @_;
51 return Catalyst::Plugin::Cache::Curried->new( $c, @meta );
54 sub get_preset_curried {
55 my ( $c, $name ) = @_;
57 if ( ref( my $preset = $c->config->{cache}{profiles}{$name} ) ) {
58 return $preset if Scalar::Util::blessed($preset);
60 my @meta = ( ( ref $preset eq "HASH" ) ? %$preset : @$preset );
61 return $c->curry_cache( @meta );
67 sub get_cache_backend {
68 my ( $c, $name ) = @_;
69 $c->_cache_backends->{$name};
72 sub register_cache_backend {
73 my ( $c, $name, $backend ) = @_;
75 no warnings 'uninitialized';
76 Carp::croak("$backend does not look like a cache backend - "
77 . "it must be an object supporting get, set and delete")
78 unless eval { $backend->can("get") && $backend->can("set") && $backend->can("delete") };
80 $c->_cache_backends->{$name} = $backend;
83 sub unregister_cache_backend {
84 my ( $c, $name ) = @_;
85 delete $c->_cache_backends->{$name};
88 sub default_cache_backend {
90 $c->get_cache_backend( "default" ) || $c->temporary_cache_backend;
93 sub temporary_cache_backend {
95 die "FIXME - make up an in memory cache backend, that hopefully works well for the current engine";
98 # this gets a shit name so that the plugins can override a good name
99 sub choose_cache_backend_wrapper {
100 my ( $c, @meta ) = @_;
102 Carp::croak("meta data must be an even sized list") unless @meta % 2 == 0;
106 # allow the cache client to specify who it wants to cache with (but loeave room for a hook)
107 if ( exists $meta{backend} ) {
108 if ( Scalar::Util::blessed($meta{backend}) ) {
109 return $meta{backend};
111 return $c->get_cache_backend( $meta{backend} ) || $c->default_cache_backend;
116 $meta{caller} = [ caller(2) ] unless exists $meta{caller}; # might be interesting
118 if ( my $chosen = $c->choose_cache_backend( %meta ) ) {
119 $chosen = $c->get_cache_backend( $chosen ) unless Scalar::Util::blessed($chosen); # if it's a name find it
120 return $chosen if Scalar::Util::blessed($chosen); # only return if it was an object or name lookup worked
123 # die "no such backend"?
124 # currently, we fall back to default
127 return $c->default_cache_backend;
130 sub choose_cache_backend { shift->NEXT::choose_cache_backend( @_ ) } # a convenient fallback
133 my ( $c, $key, $value, @meta ) = @_;
134 $c->choose_cache_backend_wrapper( key => $key, value => $value, @meta )->set( $key, $value );
138 my ( $c, $key, @meta ) = @_;
139 $c->choose_cache_backend_wrapper( key => $key, @meta )->get( $key );
143 my ( $c, $key, @meta ) = @_;
144 $c->choose_cache_backend_wrapper( key => $key, @meta )->delete( $key );
155 Catalyst::Plugin::Cache -
159 use Catalyst::Plugin::Cache;