From 96c15c56b29b6472226a0dd627fa323fa45e3c5e Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 16 Feb 2009 21:03:31 +0000 Subject: [PATCH] Optimize canShowDebug for speed by caching the calculated check. --- lib/WebGUI/Session/ErrorHandler.pm | 15 +++++++++------ t/Session/ErrorHandler.t | 18 ++++++++++++------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/WebGUI/Session/ErrorHandler.pm b/lib/WebGUI/Session/ErrorHandler.pm index 50c5fd1eb..7d62ea385 100644 --- a/lib/WebGUI/Session/ErrorHandler.pm +++ b/lib/WebGUI/Session/ErrorHandler.pm @@ -105,18 +105,21 @@ sub canShowBasedOnIP { =head2 canShowDebug ( ) Returns true if the user meets the condition to see debugging information and debug mode is enabled. +This method caches its value, so long processes may need to manually clear the cached in $self->{_canShowDebug}. =cut sub canShowDebug { - my $self = shift; + my $self = shift; ##This check prevents in infinite loop during startup. - return 0 unless ($self->session->hasSettings); - - return 0 unless ($self->session->setting->get("showDebug")); - return 0 unless (substr($self->session->http->getMimeType(),0,9) eq "text/html"); - return $self->canShowBasedOnIP('debugIp'); + return 0 unless ($self->session->hasSettings); + return $self->{_canShowDebug} if exists ($self->{_canShowDebug}); + my $canShow = $self->session->setting->get("showDebug") + && substr($self->session->http->getMimeType(),0,9) eq "text/html" + && $self->canShowBasedOnIP('debugIp'); + $self->{_canShowDebug} = $canShow; + return $canShow; } #------------------------------------------------------------------- diff --git a/t/Session/ErrorHandler.t b/t/Session/ErrorHandler.t index cf60f78f2..c714458cd 100644 --- a/t/Session/ErrorHandler.t +++ b/t/Session/ErrorHandler.t @@ -153,23 +153,29 @@ my $origDebugIp = $session->setting->get('debugIp'); my $origShowDebug = $session->setting->get('showDebug'); $session->setting->set('showDebug', 0); -is($eh->canShowDebug, 0, 'canShowDebug: returns 0 if not enabled'); +delete $eh->{_canShowDebug}; +ok(! $eh->canShowDebug, 'canShowDebug: returns 0 if not enabled'); $session->setting->set('showDebug', 1); $session->http->setMimeType('audio/mp3'); -is($eh->canShowDebug, 0, 'canShowDebug: returns 0 if mime type is wrong'); +delete $eh->{_canShowDebug}; +ok(! $eh->canShowDebug, 'canShowDebug: returns 0 if mime type is wrong'); $session->http->setMimeType('text/html'); $session->setting->set('debugIp', ''); -is($eh->canShowDebug, 1, 'canShowDebug: returns 1 if debugIp is empty string'); +delete $eh->{_canShowDebug}; +ok($eh->canShowDebug, 'canShowDebug: returns 1 if debugIp is empty string'); $session->setting->set('debugIp', '10.0.0.5/32, 192.168.0.4/30'); $newEnv{REMOTE_ADDR} = '172.17.0.5'; -is($eh->canShowDebug, 0, 'canShowDebug: returns 0 if debugIp is set and IP address is out of filter'); +delete $eh->{_canShowDebug}; +ok(! $eh->canShowDebug, 'canShowDebug: returns 0 if debugIp is set and IP address is out of filter'); $newEnv{REMOTE_ADDR} = '10.0.0.5'; -is($eh->canShowDebug, 1, 'canShowDebug: returns 1 if debugIp is set and IP address matches filter'); +delete $eh->{_canShowDebug}; +ok($eh->canShowDebug, 'canShowDebug: returns 1 if debugIp is set and IP address matches filter'); $newEnv{REMOTE_ADDR} = '192.168.0.5'; -is($eh->canShowDebug, 1, 'canShowDebug: returns 1 if debugIp is set and IP address matches filter'); +delete $eh->{_canShowDebug}; +ok($eh->canShowDebug, 'canShowDebug: returns 1 if debugIp is set and IP address matches filter'); #################################################### #