Catalyst-Manual
view release on metacpan or search on metacpan
lib/Catalyst/Manual/Cookbook.pod view on Meta::CPAN
path might be to install Fink, then use C<apt-get install
shared-mime-info>. Restart the server, and everything should be fine.
Make sure you are using the latest version (>= 0.16) for best
results. If you are having errors serving CSS files, or if they get
served as text/plain instead of text/css, you may have an outdated
shared-mime-info version. You may also wish to simply use the following
code in your Static controller:
if ($c->req->path =~ /css$/i) {
$c->serve_static( "text/css" );
} else {
$c->serve_static;
}
=head3 Serving Static Files with Apache
When using Apache, you can bypass Catalyst and any Static
plugins/controllers controller by intercepting requests for the
F<root/static> path at the server level. All that is required is to
define a DocumentRoot and add a separate Location block for your static
content. Here is a complete config for this application under mod_perl
1.x:
<Perl>
use lib qw(/var/www/MyApp/lib);
</Perl>
PerlModule MyApp
<VirtualHost *>
ServerName myapp.example.com
DocumentRoot /var/www/MyApp/root
<Location />
SetHandler perl-script
PerlHandler MyApp
</Location>
<LocationMatch "/(static|favicon.ico)">
SetHandler default-handler
</LocationMatch>
</VirtualHost>
And here's a simpler example that'll get you started:
Alias /static/ "/my/static/files/"
<Location "/static">
SetHandler none
</Location>
=head2 Caching
Catalyst makes it easy to employ several different types of caching to
speed up your applications.
=head3 Cache Plugins
There are three wrapper plugins around common CPAN cache modules:
Cache::FastMmap, Cache::FileCache, and Cache::Memcached. These can be
used to cache the result of slow operations.
The Catalyst Advent Calendar uses the FileCache plugin to cache the
rendered XHTML version of the source POD document. This is an ideal
application for a cache because the source document changes
infrequently but may be viewed many times.
use Catalyst qw/Cache::FileCache/;
...
use File::stat;
sub render_pod : Local {
my ( self, $c ) = @_;
# the cache is keyed on the filename and the modification time
# to check for updates to the file.
my $file = $c->path_to( 'root', '2005', '11.pod' );
my $mtime = ( stat $file )->mtime;
my $cached_pod = $c->cache->get("$file $mtime");
if ( !$cached_pod ) {
$cached_pod = do_slow_pod_rendering();
# cache the result for 12 hours
$c->cache->set( "$file $mtime", $cached_pod, '12h' );
}
$c->stash->{pod} = $cached_pod;
}
We could actually cache the result forever, but using a value such as 12 hours
allows old entries to be automatically expired when they are no longer needed.
=head3 Page Caching
Another method of caching is to cache the entire HTML page. While this is
traditionally handled by a frontend proxy server like Squid, the Catalyst
PageCache plugin makes it trivial to cache the entire output from
frequently-used or slow actions.
Many sites have a busy content-filled front page that might look something
like this. It probably takes a while to process, and will do the exact same
thing for every single user who views the page.
sub front_page : Path('/') {
my ( $self, $c ) = @_;
$c->forward( 'get_news_articles' );
$c->forward( 'build_lots_of_boxes' );
$c->forward( 'more_slow_stuff' );
$c->stash->{template} = 'index.tt';
}
We can add the PageCache plugin to speed things up.
use Catalyst qw/Cache::FileCache PageCache/;
sub front_page : Path ('/') {
my ( $self, $c ) = @_;
$c->cache_page( 300 );
# same processing as above
}
( run in 2.753 seconds using v1.01-cache-2.11-cpan-600a1bdf6e4 )