Gantry
view release on metacpan or search on metacpan
lib/Gantry/Plugins/PageCache.pm view on Meta::CPAN
# set config
# 0/1
$gobj->gantry_pagecache_crud(
$gobj->fish_config( 'gantry_pagecache_crud' ) || 0
);
# do_add, do_delete, ...
$gobj->gantry_pagecache_action(
$gobj->fish_config( 'gantry_pagecache_action' ) || ''
);
# text/html, text/rss, ...
$gobj->gantry_pagecache_mime_type(
$gobj->fish_config( 'gantry_pagecache_mime_type' ) || ''
);
# /controller1, ...
$gobj->gantry_pagecache_location(
$gobj->fish_config( 'gantry_pagecache_location' ) || ''
);
# /controller1/view, ...,
$gobj->gantry_pagecache_uri(
$gobj->fish_config( 'gantry_pagecache_uri' ) || ''
);
my $cache_key;
# special CRUD caching
if ( $gobj->gantry_pagecache_crud() ) {
if ( $gobj->is_post()
&& $gobj->action() =~ /do_edit|do_add|do_delete/ ) {
$cache_key = _make_gantry_cache_key( $gobj );
# flag for cache store
$gobj->gantry_pagecache( $cache_key );
$gobj->cache_del( $cache_key . "content_type" );
$gobj->cache_del( $cache_key );
my @parts = split( '/', $cache_key );
pop( @parts );
$gobj->cache_del( join( '/', @parts ) . "content_type" );
$gobj->cache_del( join( '/', @parts ) );
}
}
# all other caching
elsif ( _caching_enabled( $gobj ) ) {
$cache_key = _make_gantry_cache_key( $gobj, { serialize => 1 } );
if ( my $page = $gobj->cache_get( $cache_key ) ) {
# set the content-type
$gobj->content_type(
$gobj->cache_get( $cache_key . "content_type" )
);
# set cached page
$gobj->gantry_response_page( $page );
}
# flag it for the cache store method
else {
$gobj->gantry_pagecache( $cache_key );
}
}
}
#-----------------------------------------------------------
# gantry_pagecache_put
#-----------------------------------------------------------
sub _caching_enabled {
my( $gobj ) = @_;
my $cache = 0;
if ( my @actions = split( /\s*,\s*/, $gobj->gantry_pagecache_action() ) ) {
my $current_action = $gobj->action();
foreach ( @actions ) {
if ( $_ eq $current_action ) {
return( 1 );
}
}
}
if ( my @types = split( /\s*,\s*/, $gobj->gantry_pagecache_mime_type() ) ) {
my $current_type = $gobj->content_type();
foreach ( @types ) {
if ( $_ eq $current_type ) {
return( 1 );
}
}
}
if ( my @locs = split( /\s*,\s*/, $gobj->gantry_pagecache_location() ) ) {
my $current_loc = $gobj->location();
foreach ( @locs ) {
if ( $_ eq $current_loc ) {
return( 1 );
}
}
}
if ( my @uris = split( /\s*,\s*/, $gobj->gantry_pagecache_uri() ) ) {
my $current_uri = $gobj->uri();
foreach ( @uris ) {
if ( $_ eq $current_uri ) {
return( 1 );
}
}
}
return( 0 );
}
#-----------------------------------------------------------
# gantry_pagecache_put
#-----------------------------------------------------------
sub gantry_pagecache_store {
my ( $gobj ) = @_;
# special CRUD caching
if ( $gobj->gantry_pagecache_crud() ) {
my $cache_key = $gobj->gantry_pagecache();
if ( $gobj->action() =~ /do_edit|do_add|do_delete/ ) {
# set page
$gobj->cache_set( $cache_key, $gobj->gantry_response_page() );
# set content-type
$gobj->cache_set(
( $cache_key . 'content_type' ),
$gobj->content_type()
);
}
}
# all other caching
elsif( _caching_enabled( $gobj ) ) {
my $cache_key = $gobj->gantry_pagecache();
# set page
$gobj->cache_set( $cache_key, $gobj->gantry_response_page() );
# set content-type
$gobj->cache_set(
( $cache_key . 'content_type' ),
$gobj->content_type()
);
}
}
#-----------------------------------------------------------
# _make_gantry_cache_key
#-----------------------------------------------------------
sub _make_gantry_cache_key {
my( $gobj, $opt ) = @_;
my @parts;
push( @parts,
$gobj->namespace,
$gobj->uri,
);
if ( $opt->{serialize} ) {
my $serial = _serialize_params( $gobj );
push( @parts,
( $serial ? ( '?' . $serial ) : '' ),
);
}
return join( '', @parts );
}
#-----------------------------------------------------------
# _serialize_params
#-----------------------------------------------------------
sub _serialize_params {
my( $gobj ) = @_;
my %param = $gobj->get_param_hash;
my @param_serial;
foreach my $k ( sort { $a cmp $b } keys %param ) {
next if substr( $k, 0, 1 ) eq '.';
push( @param_serial, "$k=$param{$k}" );
}
return join( '&', @param_serial );
}
#-------------------------------------------------
# $self->gantry_pagecache_crud
#-------------------------------------------------
sub gantry_pagecache_crud {
my ( $self, $p ) = ( shift, shift );
$$self{__GANTRY_PAGECACHE_CRUD__} = $p if defined $p;
return( $$self{__GANTRY_PAGECACHE_CRUD__} );
} # end gantry_pagecache_crud
( run in 0.928 second using v1.01-cache-2.11-cpan-d7f47b0818f )