# Bloxsom 3.0 Plugin: BloxsomCache # Cache for speeding up # Author: Victor Hsieh # Tue, 22 Mar 2005 00:08:15 +0800 package Blosxom::Plugin::BlosxomCache; use strict; use DB_File; use Fcntl qw/:flock/; use Data::Serializer; use Time::localtime; use constant dbfile => '/tmp/dbfile'; my ($_fh, $_old_stdout); =cut Some plugins give you differnet result at different time even if you have the same entries, e.g. Calendar will change in the next month. So we need to put year and mon into the key. =cut sub request_key { my $self = shift; # XXX you could have two content of the same if you had different 'url' return new Data::Serializer->serialize([ $self->{request}, localtime->year(), localtime->mon(), map { $self->{entries}->{$_}->{mtime} } keys %{$self->{entries}}, ]); } sub try_cache { my $self = shift; my $obj; my %hash; my $serialized = request_key($self); tie %hash, 'DB_File', dbfile, O_CREAT | O_RDONLY | LOCK_EX, 0660, $DB_HASH or die $!; if (exists $hash{$serialized}) { print $hash{$serialized}; $self->{state}->{stop}->{entries}++; $self->{state}->{stop}->{handlers}->{flow}++; } else { # override STDOUT to save the outpuT # TODO write to variable instead of file? open $_fh, '+>', "/tmp/blosxomcache.$$" or die $!; $_old_stdout = *STDOUT; *STDOUT = $_fh; } untie %hash; 1; } sub save { my $self = shift; seek $_fh, 0, 0; my $content = join '',<$_fh>; close $_fh; unlink "/tmp/blosxomcache.$$"; *STDOUT = $_old_stdout; print $content; my %hash; tie %hash, 'DB_File', dbfile, O_CREAT | O_WRONLY | LOCK_EX, 0600, $DB_HASH or die $!; my $serialized = request_key($self); $hash{$serialized} = $content; untie %hash; 1; } 1; __END__ =head1 NAME Blosxom 3.0 Plugin: BlosxomCache =head1 SYNOPSIS This plugin speed up your blosxom by caching the web context of the requests. It works for Blosxom 3.0 since it doesn't have static rendering so far. B: The size of database is monotonically increasing. So you may want to rm the database periodly (e.g. on the first day of a month). =head1 INSTALL In handlers.flow, put C right after C, and C at the end of the flow. =head1 AUTHOR Victor Hsieh , L =head1 SEE ALSO Blosxom Home/Docs/Licensing: L Blosxom Plugin Docs: L =head1 COPYRIGHT Copyright (c) 2005. Victor Hsieh. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://www.perl.com/perl/misc/Artistic.html =cut