Apache-PAR

 view release on metacpan or  search on metacpan

PAR/Static.pm  view on Meta::CPAN

	my $cache_max_size = lc($r->dir_config->get('PARStaticCacheMaxSize'));

	my $cache_debug = $r->dir_config->get('PARStaticCacheDebug');
	
	$r->log->debug('Apache::PAR::Static: Initializing caching') if $cache_debug;
	
	my $cache_obj = undef;
	
	unless(defined $cache_type and $cache_type ne '') {
		$r->log->debug('Apache::PAR::Static: No cache type specified') if $cache_debug;
		return undef;
	}

	if($cache_type eq $CACHE_MEMORY)    { $cache_type = 'Memory'; }
	elsif($cache_type eq $CACHE_SHARED) { $cache_type = 'SharedMemory'; }
	elsif($cache_type eq $CACHE_FILE)   { $cache_type = 'File'; }

	# makes a module name like Cache::SizeAwareMemoryCache
	my $cache_module =
		'Cache::' .
		($cache_max_size ? 'SizeAware' : '') .
		$cache_type .
		'Cache';

	$r->log->debug("Apache::PAR::Static: Using cache module: $cache_module") if $cache_debug;
	eval "require $cache_module; \$cache_obj = $cache_module->new({
		namespace          => 'APACHE_PAR_STATIC',
		default_expires_in => \$cache_expires,
		max_size           => \$cache_max_size,
		});";

	unless (defined($cache_obj)) {
		$r->log->warn("Apache::PAR::Static: failed to initialize cache object - $@");
		return undef;
	}
	$r->log->debug("Apache::PAR::Static: Caching initialized sucessfully.") if $cache_debug;

	return $cache_obj;
}

sub _get_cache {
	my ($r, $filename, $location, $file_mtime, $cache_obj) = @_;

	return undef unless
		defined $filename and
		defined $location and
		defined $cache_obj;

	my $cache_debug = $r->dir_config->get('PARStaticCacheDebug');

	$r->log->debug('Apache::PAR::Static: Attempting to get cache results') if $cache_debug;
	
	my $cache_results = $cache_obj->get($filename . '!!' . $location);

	# Cache miss
	unless (defined $cache_results) {
		$r->log->debug("Apache::PAR::Static: Cache miss for $location in $filename") if $cache_debug;
		return undef;
	}

	# Check for updated file
	my $cache_mtime = $cache_results->[0];
	unless ($cache_mtime == $file_mtime) {
		$cache_obj->remove($location);
		$r->log->debug("Apache::PAR::Static: Cache time does not match file modified time for $location in $filename") if $cache_debug;
		return undef;
	}

	# Cache hit
	$r->log->debug("Apache::PAR::Static: Cache hit for $location in $filename") if $cache_debug;

	#FIXME pass by reference
	return $cache_results->[1];
}

sub _set_cache {
	#FIXME pass by reference
	my ($r, $filename, $location, $content, $file_mtime, $cache_obj) = @_;
	return unless 
		defined $filename and
		defined $location and 
		defined $content and 
		defined $file_mtime and
		defined $cache_obj;
	my $cache_debug = $r->dir_config->get('PARStaticCacheDebug');

	$r->log->debug("Apache::PAR::Static: Adding $location in $filename to cache") if $cache_debug;

	return $cache_obj->set($filename . '!!' . $location, [$file_mtime, $content]);
}


1;
__END__

=head1 NAME

Apache::PAR::Static - Serve static content to clients from within .par files.

=head1 SYNOPSIS

A sample configuration (within a web.conf) is below:

  Alias /myapp/static/ ##PARFILE##/
  <Location /myapp/static>
    SetHandler perl-script
    PerlHandler Apache::PAR::Static
    PerlSetVar PARStaticFilesPath htdocs/
    PerlSetVar PARStaticDirectoryIndex index.htm
    PerlAddVar PARStaticDirectoryIndex index.html
    PerlSetVar PARStaticDefaultMIME text/html
    PerlSetVar PARStaticCacheType memory
    PerlSetVar PARStaticCacheExpires "1 day"
    PerlSetVar PARStaticCacheMaxSize 1000
  </Location>

=head1 DESCRIPTION

The Apache::PAR::Static module allows a .par file creator to place any static content into a .par archive (under a configurable directory in the .par file) to be served directly to clients.

To use, add Apache::PAR::Static into the Apache configuration, either through an Apache configuration file, or through a web.conf file (discussed in more detail in L<Apache::PAR>.)



( run in 2.273 seconds using v1.01-cache-2.11-cpan-2398b32b56e )