From 46171d0c662fb591ddf0541e10f796573bbd17dd Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Sun, 15 Nov 2009 16:50:27 +0100 Subject: [PATCH] fixed issue #29: config now caches returned value Qpsmtpd::config now checks cache, hooks, qmail_config, default in this order and returns the first match. In any case the returned value is stored in the cache, so subsequent calls to Qpsmtpd::config return the same value (unless the cache is cleared). --- lib/Qpsmtpd.pm | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/Qpsmtpd.pm b/lib/Qpsmtpd.pm index b4211fe..0da81fc 100644 --- a/lib/Qpsmtpd.pm +++ b/lib/Qpsmtpd.pm @@ -149,29 +149,41 @@ sub clear_config_cache { sub config { my ($self, $c, $type) = @_; - #my $timer = $SAMPLER->("config", undef, 1); + $self->log(LOGDEBUG, "in config($c)"); + + # first try the cache + # XXX - is this always the right thing to do? what if a config hook + # can return different values on subsequent calls? if ($_config_cache->{$c}) { + $self->log(LOGDEBUG, "config($c) returning (@{$_config_cache->{$c}}) from cache"); return wantarray ? @{$_config_cache->{$c}} : $_config_cache->{$c}->[0]; } - $_config_cache->{$c} = [$defaults{$c}] if exists($defaults{$c}); - - #warn "SELF->config($c) ", ref $self; - + # then run the hooks my ($rc, @config) = $self->run_hooks_no_respond("config", $c); - @config = () unless $rc == OK; + $self->log(LOGDEBUG, "config($c): hook returned ($rc, @config) "); + if ($rc == OK) { + $self->log(LOGDEBUG, "setting _config_cache for $c to [@config] from hooks and returning it"); + $_config_cache->{$c} = \@config; + return wantarray ? @{$_config_cache->{$c}} : $_config_cache->{$c}->[0]; + } - if (wantarray) { - @config = $self->get_qmail_config($c, $type) unless @config; - @config = $defaults{$c} if (!@config and $defaults{$c}); - return @config; + # and then get_qmail_config + @config = $self->get_qmail_config($c, $type); + if (@config) { + $self->log(LOGDEBUG, "setting _config_cache for $c to [@config] from get_qmail_config and returning it"); + $_config_cache->{$c} = \@config; + return wantarray ? @{$_config_cache->{$c}} : $_config_cache->{$c}->[0]; } - else { - return $config[0] if defined($config[0]); - my $val = $self->get_qmail_config($c, $type); - return $val if defined($val); - return $defaults{$c}; + + # finally we use the default if there is any: + if (exists($defaults{$c})) { + $self->log(LOGDEBUG, "setting _config_cache for $c to @{[$defaults{$c}]} from defaults and returning it"); + $_config_cache->{$c} = [$defaults{$c}]; + return wantarray ? @{$_config_cache->{$c}} : $_config_cache->{$c}->[0]; } + return; + } sub config_dir {