Exposed a Cache API and moved page caching settings from the config file to the UI.

This commit is contained in:
JT Smith 2003-03-11 06:50:59 +00:00
parent 2b7b9ea4d2
commit c3c06514f3
6 changed files with 218 additions and 12 deletions

View file

@ -26,6 +26,8 @@ Contributing Developers..............Peter Beardsley / Appropriate Solutions
Christophe Marcant
Tavis Parker / ParkerOne Consulting
Daniel Quinlan
Steve Simms
Ben Simpson
Jeff Szpak / Plain Black
Sean Tu / WDI

View file

@ -57,6 +57,9 @@ save you many hours of grief.
* The discussions on USS and Articles have been turned off. They'll
need to be manually turned back on.
* The setting "cachePages" has been removed from the config file. It
has been moved to the content settings page inside the UI.
5.1.0
--------------------------------------------------------------------

View file

@ -3095,6 +3095,14 @@ alter table Article drop column allowDiscussion;
alter table USS drop column allowDiscussion;
delete from settings where name='usernameBinding';
delete from settings where name='addEditStampToPosts';
insert into settings values ("cachePages","60");
insert into settings values ("cachePagesVisitor","600");
delete from international where languageId=1 and namespace='WebGUI' and internationalId=887;
insert into international (internationalId,languageId,namespace,message,lastUpdated) values (887,1,'WebGUI','Midas (Mozilla 1.3+)', 1047342950);
delete from international where languageId=1 and namespace='WebGUI' and internationalId=896;
insert into international (internationalId,languageId,namespace,message,lastUpdated) values (896,1,'WebGUI','Page Cache Timeout (Visitors)', 1047342926);
delete from international where languageId=1 and namespace='WebGUI' and internationalId=895;
insert into international (internationalId,languageId,namespace,message,lastUpdated) values (895,1,'WebGUI','Page Cache Timeout', 1047342910);

View file

@ -11,12 +11,9 @@ our $VERSION = "5.2.0";
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
#Test to see if Cache::FileCache will load.
my $hasCache=1;
eval " use Cache::FileCache; "; $hasCache=0 if $@;
use strict qw(vars subs);
use Tie::CPHash;
use WebGUI::Cache;
use WebGUI::ErrorHandler;
use WebGUI::Icon;
use WebGUI::International;
@ -225,15 +222,12 @@ sub _processOperations {
#-------------------------------------------------------------------
sub page {
my ($debug, $positions, $wobjectOutput, $pageEdit, $httpHeader, $content, $operationOutput, $template);
my ($cache, $debug, $positions, $wobjectOutput, $pageEdit, $httpHeader, $content, $operationOutput, $template);
WebGUI::Session::open($_[0],$_[1]);
my $useCache = ($hasCache && $session{config}{cachePages} && $session{form}{op} eq ""
&& $session{form}{wid} eq "" && not $session{var}{adminOn});
my $cache;
my $cacheKey = "page_".$session{page}{pageId}."_".$session{user}{userId};
my $useCache = ($session{form}{op} eq "" && $session{form}{wid} eq "" && not $session{var}{adminOn});
if ($useCache) {
$cache = new Cache::FileCache({'namespace'=>$_[1]});
$content = $cache->get($cacheKey);
$cache = WebGUI::Cache->new("page_".$session{page}{pageId}."_".$session{user}{userId});
$content = $cache->get;
}
$operationOutput = _processOperations();
$wobjectOutput = _processFunctions();
@ -272,7 +266,13 @@ sub page {
$httpHeader = WebGUI::Session::httpHeader();
unless ($useCache && defined $content) {
$content = WebGUI::Macro::process(WebGUI::Template::process(WebGUI::Style::get($pageEdit.WebGUI::Page::getTemplate($template)), $positions));
$cache->set($cacheKey, $content, $session{config}{cachePages}) if ($useCache);
my $ttl;
if ($session{user}{userId} == 1) {
$ttl = $session{setting}{cachePagesVisitor};
} else {
$ttl = $session{setting}{cachePages};
}
$cache->set($content, $ttl) if ($useCache);
}
$debug = _generateDebug();
WebGUI::Session::close();

179
lib/WebGUI/Cache.pm Normal file
View file

@ -0,0 +1,179 @@
package WebGUI::Cache;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2003 Plain Black LLC.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
#Test to see if Cache::FileCache will load.
my $hasCache=1;
eval " use Cache::FileCache; "; $hasCache=0 if $@;
use HTTP::Request;
use LWP::UserAgent;
use WebGUI::Session;
=head1 NAME
Package WebGUI::Cache
=head1 DESCRIPTION
This package provides a means for WebGUI to cache data to the filesystem. Caching is only enabled, however, if Cache::Filecache is installed on the system.
=head1 SYNOPSIS
use WebGUI::Cache;
=head1 METHODS
These methods are available from this class:
=cut
#-------------------------------------------------------------------
sub _canCache {
return ($hasCache);
}
#-------------------------------------------------------------------
=head2 delete ( )
Remove content from the filesystem cache.
=cut
sub delete {
if (_canCache()) {
$_[0]->{_cache}->remove($_[0]->{_key});
} else {
$_[0]->{_cache} = "";
}
}
#-------------------------------------------------------------------
=head2 get ( )
Retrieve content from the filesystem cache.
=cut
sub get {
if (_canCache()) {
return $_[0]->{_cache}->get($_[0]->{_key});
} else {
return $_[0]->{_cache};
}
}
#-------------------------------------------------------------------
=head2 new ( key [, namespace ] )
Constructor.
=over
=item key
A key unique to this namespace. It is used to uniquely identify the cached content.
=item namespace
Defaults to the database DSN for your WebGUI database. The only reason to override the default is if you want the cached content to be shared among all WebGUI instances on this machine.
=back
=cut
sub new {
my $cache;
my $class = shift;
my $key = shift;
my $namespace = shift || $session{config}{dsn};
$cache = new Cache::FileCache({'namespace'=>$namespace}) if (_canCache());
bless {_cache => $cache, _key => $key}, $class;
}
#-------------------------------------------------------------------
=head2 set ( content [, ttl ] )
Save content to the filesystem cache.
=over
=item content
A scalar variable containing the content to be set.
=item ttl
The time to live for this content. This is the amount of time (in seconds) that the content will remain in the cache. Defaults to "60".
=back
=cut
sub set {
my $ttl = $_[2] || 60;
if (_canCache()) {
$_[0]->{_cache}->set($_[0]->{_key},$_[1],$ttl);
} else {
$_[0]->{_cache} = $_[1];
}
}
#-------------------------------------------------------------------
=head2 setByHTTP ( url [, ttl ] )
Retrieves a document via HTTP and stores it in the cache.
=over
=item url
The URL of the document to retrieve. It must begin with the standard "http://".
=item ttl
The time to live for this content. This is the amount of time (in seconds) that the content will remain in the cache. Defaults to "60".
=cut
sub setByHTTP {
my $userAgent = new LWP::UserAgent;
$userAgent->agent("WebGUI/".$WebGUI::VERSION);
$userAgent->timeout(30);
my $request = new HTTP::Request (GET => $_[1]);
my $response = $userAgent->request($request);
$_[0]->set($response->content,$_[2]);
}
1;

View file

@ -99,6 +99,20 @@ sub www_editContentSettings {
$f->integer("textAreaRows",WebGUI::International::get(463),$session{setting}{textAreaRows});
$f->integer("textAreaCols",WebGUI::International::get(464),$session{setting}{textAreaCols});
$f->integer("textBoxSize",WebGUI::International::get(465),$session{setting}{textBoxSize});
my @data = WebGUI::DateTime::secondsToInterval($session{setting}{cachePages});
$f->interval(
-name=>"cachePages",
-label=>WebGUI::International::get(895),
-intervalValue=>$data[0],
-unitsValue=>$data[1]
);
my @data = WebGUI::DateTime::secondsToInterval($session{setting}{cachePagesVisitor});
$f->interval(
-name=>"cachePagesVisitor",
-label=>WebGUI::International::get(896),
-intervalValue=>$data[0],
-unitsValue=>$data[1]
);
$f->submit;
$output .= $f->print;
return _submenu($output);