Optimize canShowDebug for speed by caching the calculated check.
This commit is contained in:
parent
99a047e147
commit
96c15c56b2
2 changed files with 21 additions and 12 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
||||
####################################################
|
||||
#
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue