From 24a7eaa8ddbc81d238ef98465d30e24e145027e6 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 15 Jun 2020 09:09:08 +0200 Subject: [PATCH] cache botconfig, optimze updateArray --- bashbot.sh | 9 +++++++-- modules/jsonDB.sh | 6 ++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 96e4ef2..2078e9d 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-19-g4e043c5 +#### $$VERSION$$ v0.98-dev-20-g3e4e904 # # Exit Codes: # - 0 sucess (hopefully) @@ -41,7 +41,8 @@ _exists() { # execute function if exists _exec_if_function() { - [ "$(LC_ALL=C type -t "${1}")" != "function" ] || "$@" + [ "$(LC_ALL=C type -t "${1}")" != "function" ] && return 1 + "$@" } # returns true if function exist _is_function() { @@ -57,11 +58,15 @@ setConfigKey() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTDATABASE}.jssh" } +declare -A BASHBOTCONFIG getConfigKey() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 + # cache if aupdateArray exists, else read from file + _exec_if_function jssh_updateArray_async "BASHBOTCONFIG" "${BOTDATABASE}" && printf '%s' "${BASHBOTCONFIG[${1}]}" && return [ -r "${BOTDATABASE}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTDATABASE}.jssh" | tail -n 1 } + # get location and name of bashbot.sh SCRIPT="$0" REALME="${BASH_SOURCE[0]}" diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index ccb489a..e8e3b57 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-18-g852ab9d +#### $$VERSION$$ v0.98-dev-20-g3e4e904 # # source from commands.sh to use jsonDB functions # @@ -235,7 +235,9 @@ jssh_checkDB(){ # $2 database # $3 id used to identify caller jssh_updateArray() { - local DB; DB="$(jssh_checkDB "$2")" + local DB="${2}.jssh" # name check in async + [ -z "${DB}" ] && return 1 + [ ! -f "${DB}" ] && return 2 { flock -s -w 1 200; jssh_updateArray_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}" } function jssh_updateArray_async() {