diff --git a/docs/gotcha.txt b/docs/gotcha.txt index ffe5d5ea8..eb49bed9c 100644 --- a/docs/gotcha.txt +++ b/docs/gotcha.txt @@ -60,6 +60,10 @@ save you many hours of grief. * The setting "cachePages" has been removed from the config file. It has been moved to the content settings page inside the UI. + * The setting "cacheInternational" has been removed from the config + file. The international messages are now automatically cached + if caching is available. + 5.1.0 -------------------------------------------------------------------- diff --git a/lib/WebGUI/Cache.pm b/lib/WebGUI/Cache.pm index 903d0d3e3..7e32aad0b 100644 --- a/lib/WebGUI/Cache.pm +++ b/lib/WebGUI/Cache.pm @@ -19,6 +19,7 @@ package WebGUI::Cache; my $hasCache=1; eval " use Cache::FileCache; "; $hasCache=0 if $@; +use HTTP::Headers; use HTTP::Request; use LWP::UserAgent; use WebGUI::Session; @@ -67,6 +68,35 @@ sub delete { } +#------------------------------------------------------------------- + +=head2 deleteByRegex ( regex ) + +Remove content from the filesystem cache where the key meets the condition of the regular expression. + +=over + +=item regex + +A regular expression that will match keys in the current namespace. Example: m/^navigation_.*/ + +=back + +=cut + +sub deleteByRegex { + if (_canCache()) { + my @keys = $_[0]->{_cache}->get_keys(); + foreach my $key (@keys) { + if ($key =~ $_[1]) { + $_[0]->{_cache}->remove($key); + } + } + } else { + $_[0]->{_cache} = ""; + } +} + #------------------------------------------------------------------- =head2 get ( ) @@ -99,7 +129,7 @@ A key unique to this namespace. It is used to uniquely identify the cached conte =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. +Defaults to the config filename for the current site. The only reason to override the default is if you want the cached content to be shared among all WebGUI instances on this machine. A common alternative namespace is "URL", which is typically used when caching content using the setByHTTP method. =back @@ -109,8 +139,8 @@ sub new { my $cache; my $class = shift; my $key = shift; - my $namespace = shift || $session{config}{dsn}; - $cache = new Cache::FileCache({'namespace'=>$namespace}) if (_canCache()); + my $namespace = shift || $session{config}{configFile}; + $cache = new Cache::FileCache({namespace=>$namespace, auto_purge_on_set=>1}) if (_canCache()); bless {_cache => $cache, _key => $key}, $class; } @@ -167,7 +197,11 @@ sub setByHTTP { my $userAgent = new LWP::UserAgent; $userAgent->agent("WebGUI/".$WebGUI::VERSION); $userAgent->timeout(30); - my $request = new HTTP::Request (GET => $_[1]); + my $header = new HTTP::Headers; + my $referer = "http://webgui.http.request/".$session{env}{SERVER_NAME}.$session{env}{REQUEST_URI}; + chomp $referer; + $header->referer($referer); + my $request = new HTTP::Request (GET => $_[1], $header); my $response = $userAgent->request($request); $_[0]->set($response->content,$_[2]); } diff --git a/lib/WebGUI/International.pm b/lib/WebGUI/International.pm index a27b8d441..64123f497 100644 --- a/lib/WebGUI/International.pm +++ b/lib/WebGUI/International.pm @@ -15,14 +15,12 @@ package WebGUI::International; =cut -#Test to see if Cache::FileCache will load. -my $hasCache=1; -eval " use Cache::FileCache; "; $hasCache=0 if $@; - use strict; +use WebGUI::Cache; use WebGUI::Session; use WebGUI::SQL; + =head1 NAME Package WebGUI::International @@ -70,7 +68,6 @@ An integer that specifies the language that the user should see. Defaults to th sub get { my ($output, $language, $namespace, $cache); - my $useCache = ($hasCache && $session{config}{cacheInternational}); if ($_[2] ne "") { $language = $_[2]; } elsif ($session{user}{language} ne "") { @@ -83,17 +80,15 @@ sub get { } else { $namespace = "WebGUI"; } - if ($useCache) { - $cache = new Cache::FileCache({'namespace'=>'International'}); - $output = $cache->get($language."_".$namespace."_".$_[0]); - } + $cache = WebGUI::Cache->new($language."_".$namespace."_".$_[0],"International"); + $output = $cache->get; if (not defined $output) { ($output) = WebGUI::SQL->quickArray("select message from international where internationalId=$_[0] and namespace='$namespace' and languageId='$language'"); if ($output eq "" && $language ne 1) { $output = get($_[0],$namespace,1); } - $cache->set($language."_".$namespace."_".$_[0], $output, $session{config}{cacheInternational}) if ($useCache); + $cache->set($output, 3600); } return $output; } diff --git a/lib/WebGUI/Operation/Statistics.pm b/lib/WebGUI/Operation/Statistics.pm index d95283773..06c484e17 100644 --- a/lib/WebGUI/Operation/Statistics.pm +++ b/lib/WebGUI/Operation/Statistics.pm @@ -11,10 +11,8 @@ package WebGUI::Operation::Statistics; #------------------------------------------------------------------- use Exporter; -use HTTP::Request; -use HTTP::Headers; -use LWP::UserAgent; use strict; +use WebGUI::Cache; use WebGUI::DateTime; use WebGUI::Icon; use WebGUI::International; @@ -155,17 +153,14 @@ sub www_viewPageReport { #------------------------------------------------------------------- sub www_viewStatistics { return WebGUI::Privilege::adminOnly() unless (WebGUI::Privilege::isInGroup(3)); - my ($output, $data, $header, $userAgent, $request, $response, $version, $referer); - $userAgent = new LWP::UserAgent; - $userAgent->agent("WebGUI-Check/2.0"); - $userAgent->timeout(10); - $header = new HTTP::Headers; - $referer = "http://webgui.web.getversion/".$session{env}{SERVER_NAME}.$session{env}{REQUEST_URI}; - chomp $referer; - $header->referer($referer); - $request = new HTTP::Request (GET => "http://www.plainblack.com/downloads/latest-version.txt", $header); - $response = $userAgent->request($request); - $version = $response->content; + my ($output, $data); + my $url = "http://www.plainblack.com/downloads/latest-version.txt"; + my $cache = WebGUI::Cache->new($url,"URL"); + my $version = $cache->get; + if (not defined $version) { + $cache->setByHTTP($url,43200); + $version = $cache->get; + } chomp $version; $output .= helpIcon(12); $output .= '

'.WebGUI::International::get(437).'

';