Skip to content
Snippets Groups Projects
Commit 69dc75df authored by Nigel Kukard's avatar Nigel Kukard
Browse files

Added caching support for mod_accounting_sql

parent d42585b6
No related branches found
No related tags found
No related merge requests found
......@@ -22,6 +22,7 @@ use warnings;
# Modules we need
use smradius::constants;
use awitpt::cache;
use awitpt::db::dblayer;
use smradius::logging;
use smradius::util;
......@@ -228,6 +229,9 @@ sub init
ID = %{query.DuplicateID}
';
$config->{'accounting_usage_cache_time'} = 300;
# Setup SQL queries
if (defined($scfg->{'mod_accounting_sql'})) {
# Pull in queries
......@@ -294,6 +298,25 @@ sub init
$config->{'accounting_delete_duplicates_query'} = $scfg->{'mod_accounting_sql'}->{'accounting_delete_duplicates_query'};
}
}
if (defined($scfg->{'mod_accounting_sql'}->{'accounting_usage_cache_time'})) {
if ($scfg->{'mod_accounting_sql'}{'accounting_usage_cache_time'} =~ /^\s*(yes|true|1)\s*$/i) {
# Default?
} elsif ($scfg->{'mod_accounting_sql'}{'accounting_usage_cache_time'} =~ /^\s*(no|false|0)\s*$/i) {
$config->{'mod_accounting_sql'}{'accounting_usage_cache_time'} = undef;
} elsif ($scfg->{'mod_accounting_sql'}{'accounting_usage_cache_time'} =~ /^[0-9]+$/) {
$scfg->{'mod_accounting_sql'}{'accounting_usage_cache_time'} = $scfg->{'mod_accounting_sql'}{'accounting_usage_cache_time'};
} else {
$server->log(LOG_NOTICE,"[MOD_ACCOUNTING_SQL] Value for 'accounting_usage_cache_time' is invalid");
}
}
}
# Log this for info sake
if ($config->{'mod_accounting_sql'}->{'accounting_usage_cache_time'}) {
$server->log(LOG_NOTICE,"[MOD_ACCOUNTING_SQL] getUsage caching ENABLED, cache time is %ds.",
$config->{'mod_accounting_sql'}->{'accounting_usage_cache_time'});
} else {
$server->log(LOG_NOTICE,"[MOD_ACCOUNTING_SQL] getUsage caching DISABLED");
}
}
......@@ -314,6 +337,14 @@ sub getUsage
my $now = DateTime->now->set_time_zone($server->{'smradius'}->{'event_timezone'});
$template->{'query'}->{'PeriodKey'} = $now->strftime("%Y-%m");
# If we using caching, check how old the result is
if (defined($config->{'accounting_usage_cache_time'})) {
my ($res,$val) = cacheGetKeyPair('mod_accounting_sql(getUsage)',$user->{'Username'}."/".$template->{'query'}->{'PeriodKey'});
if (defined($val) && $val->{'CachedUntil'} < $now) {
return $val;
}
}
# Replace template entries
my (@dbDoParams) = templateReplace($config->{'accounting_usage_query'},$template);
......@@ -371,6 +402,14 @@ sub getUsage
$res{'TotalDataUsage'} = $totalData->bdiv('1024')->bdiv('1024')->bceil()->bstr();
$res{'TotalSessionTime'} = $totalTime->bdiv('60')->bceil()->bstr();
# If we using caching and got here, it means that we must cache the result
if (defined($config->{'accounting_usage_cache_time'})) {
$res{'CachedUntil'} = $now + $config->{'accounting_usage_cache_time'};
# Cache the result
cacheStoreKeyPair('mod_accounting_sql(getUsage)',$user->{'Username'}."/".$template->{'query'}->{'PeriodKey'},\%res);
}
return \%res;
}
......
......@@ -407,6 +407,13 @@ accounting_delete_duplicates_query=<<EOT
ID = %{query.DuplicateID}
EOT
# This is how long we going to cache the usage query for
# Default: 300 (seconds)
#
# You can use "no", "0", "false" to disable, specify a number > 1, or use
# "yes", "1", "true" to enable with the default value
accounting_usage_cache_time=300
# MOD_USERDB_SQL
[mod_userdb_sql]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment