added caching

This commit is contained in:
2025-02-22 16:49:50 +01:00
parent b0c7d0192a
commit 412d09cbe1

View File

@@ -26,9 +26,15 @@ function currency_converter {
local f_currency=$2 local f_currency=$2
local f_currency_target=$3 local f_currency_target=$3
local f_currency_date=$4 local f_currency_date=$4
local f_return
unset f_currency_converter_result unset f_currency_converter_result
# check for cached result
local f_args=$@
[ -f CACHE_CURRENCY_CONVERTER ] && f_currency_converter_result=$(egrep "^${f_args}=" CACHE_CURRENCY_CONVERTER | cut -d= -f2)
[[ -n $f_currency_converter_result ]] && g_num_valid_number "$f_currency_converter_result" && return 0
local f_line f_rate f_histfile f_date_array f_stablecoin f_reverse f_file f_link_file f_timeframe local f_line f_rate f_histfile f_date_array f_stablecoin f_reverse f_file f_link_file f_timeframe
if [[ $f_currency_target =~ ^20.*-.*: ]] if [[ $f_currency_target =~ ^20.*-.*: ]]
@@ -159,22 +165,24 @@ function currency_converter {
# if no rate found # if no rate found
if [ -z "$f_rate" ] if [ -z "$f_rate" ]
then then
# if EUR spurce or traget try way over USD as workarount # if EUR source or traget try way over USD as workaround
if [[ ${f_currency_target} = EUR ]] && [[ $f_currency != USD ]] && [[ $f_currency != EUR ]] if [[ ${f_currency_target} = EUR ]] && [[ $f_currency != USD ]] && [[ $f_currency != EUR ]]
then then
g_echo_note "trying way over USD (workaround) Target EUR" g_echo_note "trying way over USD (workaround) Target EUR"
if currency_converter $f_currency_amount $f_currency USD "$f_currency_date" if currency_converter $f_currency_amount $f_currency USD "$f_currency_date"
then then
currency_converter $f_currency_converter_result USD EUR "$f_currency_date" currency_converter $f_currency_converter_result USD EUR "$f_currency_date" && f_return=$?
return $? [[ $f_return == 0 ]] && echo "$@=$f_currency_converter_result" >>CACHE_CURRENCY_CONVERTER
return $f_return
fi fi
elif [[ ${f_currency_target} != USD ]] && [[ ${f_currency_target} != EUR ]] && [[ $f_currency = EUR ]] elif [[ ${f_currency_target} != USD ]] && [[ ${f_currency_target} != EUR ]] && [[ $f_currency = EUR ]]
then then
g_echo_note "trying way over USD (workaround) Source EUR" g_echo_note "trying way over USD (workaround) Source EUR"
if currency_converter $f_currency_amount EUR USD "$f_currency_date" if currency_converter $f_currency_amount EUR USD "$f_currency_date"
then then
currency_converter $f_currency_converter_result USD ${f_currency_target} "$f_currency_date" currency_converter $f_currency_converter_result USD ${f_currency_target} "$f_currency_date" && f_return=$?
return $? [[ $f_return == 0 ]] && echo "$@=$f_currency_converter_result" >>CACHE_CURRENCY_CONVERTER
return $f_return
fi fi
fi fi
# end if no rate found # end if no rate found
@@ -187,4 +195,6 @@ function currency_converter {
[[ $f_reverse = false ]] && g_calc "1/${f_rate}*${f_currency_amount}" [[ $f_reverse = false ]] && g_calc "1/${f_rate}*${f_currency_amount}"
f_currency_converter_result=$g_calc_result f_currency_converter_result=$g_calc_result
echo "$@=$f_currency_converter_result" >>CACHE_CURRENCY_CONVERTER
} }