From aab64b038dff9be17d263b5b67df39978100600c Mon Sep 17 00:00:00 2001 From: JT Smith Date: Wed, 18 Dec 2002 04:03:18 +0000 Subject: [PATCH] Adding caching mechanism. --- lib/WebGUI.pm | 20 ++++++++++++++++++-- lib/WebGUI/Navigation.pm | 20 ++++++++++---------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/WebGUI.pm b/lib/WebGUI.pm index 375022ada..b9015d20f 100644 --- a/lib/WebGUI.pm +++ b/lib/WebGUI.pm @@ -11,6 +11,10 @@ our $VERSION = "4.9.4"; # 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::ErrorHandler; @@ -223,6 +227,14 @@ sub _processOperations { sub page { my ($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}; + if ($useCache) { + $cache = new Cache::FileCache({'namespace'=>$_[1]}); + $content = $cache->get($cacheKey); + } $operationOutput = _processOperations(); $wobjectOutput = _processFunctions(); if ($operationOutput eq "" && $wobjectOutput eq "" && $session{form}{action2} ne "") { @@ -254,11 +266,15 @@ sub page { return $httpHeader; } elsif ($wobjectOutput ne "") { $positions->{"page.position1"} = $wobjectOutput; - } else { + } elsif (!($useCache && defined $content)) { ($positions, $template, $pageEdit) = _generatePage(); } $httpHeader = WebGUI::Session::httpHeader(); - $content = WebGUI::Template::process(WebGUI::Style::get($pageEdit.WebGUI::Page::getTemplate($template)), $positions); + unless ($useCache && defined $content) { + $content = WebGUI::Template::process(WebGUI::Style::get($pageEdit.WebGUI::Page::getTemplate($template)), + $positions); + $cache->set($cacheKey, $content, $session{config}{cachePages}) if ($useCache); + } $debug = _generateDebug(); WebGUI::Session::close(); return $httpHeader.$content.$debug; diff --git a/lib/WebGUI/Navigation.pm b/lib/WebGUI/Navigation.pm index f85fe0f91..620177838 100644 --- a/lib/WebGUI/Navigation.pm +++ b/lib/WebGUI/Navigation.pm @@ -184,23 +184,23 @@ The depth the tree should be traversed. Defaults to "0". If set to "0" the entir sub tree { my ($sth, %data, %tree); - tie %tree, 'Tie::IxHash'; - tie %data, 'Tie::CPHash'; my ($parentId, $toLevel, $depth) = @_; $toLevel = 99 if ($toLevel > 100 || $toLevel < 1); - if ($depth < $toLevel) { - $sth = WebGUI::SQL->read("select urlizedTitle, menuTitle, pageId, synopsis from page + tie %tree, 'Tie::IxHash'; + tie %data, 'Tie::CPHash'; + if ($depth < $toLevel) { + $sth = WebGUI::SQL->read("select urlizedTitle, menuTitle, pageId, synopsis from page where parentId='$parentId' order by sequenceNumber"); - while (%data = $sth->hash) { - if (WebGUI::Privilege::canViewPage($data{pageId})) { + while (%data = $sth->hash) { + if (WebGUI::Privilege::canViewPage($data{pageId})) { $tree{$data{pageId}}{url} = WebGUI::URL::gateway($data{urlizedTitle}); $tree{$data{pageId}}{title} = $data{menuTitle}; $tree{$data{pageId}}{synopsis} = $data{synopsis}; - $tree{$data{pageId}}{sub} = tree($data{pageId},$toLevel,($depth+1)); - } + $tree{$data{pageId}}{sub} = tree($data{pageId},$toLevel,($depth+1)); + } } - $sth->finish; - } + $sth->finish; + } return \%tree; }