CGI-Buffer
view release on metacpan or search on metacpan
lib/CGI/Buffer.pm view on Meta::CPAN
=head3 PSEUDOCODE
if there's no cache, the page isn't cached
look up the key, ignoring expired entries and a failing cache
if there's no entry, or it has no data, the page isn't cached
if the script's age can't be found, the page isn't cached
if the script has changed since the entry was made
remove the entry, the page isn't cached
otherwise the page is cached
=cut
sub is_cached {
my $found = 0;
if(!$cache) {
_log('debug', "is_cached: cache hasn't been enabled");
} else {
my $key = _generate_key();
_log('debug', "is_cached: looking for key = $key");
$cobject = _cache_lookup($key);
if((!$cobject) || !defined($cobject->value())) {
_log('debug', 'not found in cache');
$cobject = undef;
} else {
# If the script has changed, don't use the cache since we
# may produce different output
my $age = _my_age();
if(!defined($age)) {
# Can't determine the age. Play it safe and assume
# we're not cached
_log('debug', "Can't determine script's age");
$cobject = undef;
} elsif($age > $cobject->created_at()) {
# Remove only this entry. Clearing the whole cache
# would also lose other scripts' pages (RT104471)
_log('debug', 'Script has been updated');
_try('removing an old entry', sub { $cache->remove($key) });
$cobject = undef;
} else {
_log('debug', 'Script is in the cache');
$found = 1;
}
}
}
return $found;
}
# Purpose: find when the script was last changed
# Entry criteria: none
# Exit status: the script's modification time, or undef if it can't be read
# Side effects: remembers the answer, may create $info. Croaks if CGI::Info
# can't find the script
sub _my_age {
unless(defined($script_mtime)) {
my $path = _info()->script_path();
croak(_i18n('no_script_path')) unless(defined($path));
if(my @statb = stat($path)) {
$script_mtime = $statb[9];
} else {
_log('debug', _i18n('no_script_age', { path => $path, error => "$!" }));
}
}
return $script_mtime;
}
# Purpose: choose the content encoding to use
# Entry criteria: @content_type and $script_encoded are set from the headers
# Exit status: the encoding, or '' for none
# Side effects: may load a compression module
#
# Premise 1: _compress() fails the request with a 406 if it can't load the
# module for the encoding
# Premise 2: the client is happy with any of the encodings it lists
# Conclusion: choose the first listed encoding that can be loaded, then the 406
# can't happen
sub _choose_encoding {
my $accept = $ENV{'HTTP_ACCEPT_ENCODING'} || $ENV{'HTTP_TE'};
my $chosen = '';
# Only text is compressed, and not if the script already has (twice is
# pointless)
if($compress_content && $accept && !$script_encoded &&
!(defined($content_type[0]) && (lc($content_type[0]) ne 'text'))) {
foreach my $method(split(/\s*,\s*/, lc($accept))) {
# e.g. "gzip;q=0.8", where q=0 means "not acceptable"
my ($name, @params) = split(/\s*;\s*/, $method);
next unless(defined($name) && $ENCODING_MODULE{$name});
next if(grep { /^q=0(?:\.0*)?$/ } @params);
if(_can_load($ENCODING_MODULE{$name})) {
$chosen = $name;
last;
}
}
}
return $chosen;
}
# Purpose: say whether a module can be loaded
# Entry criteria: $module is a module name, from a fixed list
# Exit status: true if it can be
# Side effects: loads the module, and remembers the answer
{
my %loadable;
sub _can_load
{
my $module = shift;
$loadable{$module} = (eval "require $module; 1") ? 1 : 0 unless(exists($loadable{$module}));
return $loadable{$module};
}
}
# Purpose: find the content type and content encoding in a block of headers
# Entry criteria: $headers is the block of headers
# Exit status: none
# Side effects: sets @content_type, and $script_encoded if there's an encoding
( run in 2.162 seconds using v1.01-cache-2.11-cpan-eb134014566 )