cache botconfig, optimze updateArray

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2020-06-15 09:09:08 +02:00
parent 3e4e90443c
commit 24a7eaa8dd
2 changed files with 11 additions and 4 deletions

View File

@ -11,7 +11,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.98-dev-19-g4e043c5 #### $$VERSION$$ v0.98-dev-20-g3e4e904
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -41,7 +41,8 @@ _exists() {
# execute function if exists # execute function if exists
_exec_if_function() { _exec_if_function() {
[ "$(LC_ALL=C type -t "${1}")" != "function" ] || "$@" [ "$(LC_ALL=C type -t "${1}")" != "function" ] && return 1
"$@"
} }
# returns true if function exist # returns true if function exist
_is_function() { _is_function() {
@ -57,11 +58,15 @@ setConfigKey() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTDATABASE}.jssh" printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${2//\"/\\\"}" >>"${BOTDATABASE}.jssh"
} }
declare -A BASHBOTCONFIG
getConfigKey() { getConfigKey() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 [[ "$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 [ -r "${BOTDATABASE}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTDATABASE}.jssh" | tail -n 1
} }
# get location and name of bashbot.sh # get location and name of bashbot.sh
SCRIPT="$0" SCRIPT="$0"
REALME="${BASH_SOURCE[0]}" REALME="${BASH_SOURCE[0]}"

View File

@ -5,7 +5,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # 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 # source from commands.sh to use jsonDB functions
# #
@ -235,7 +235,9 @@ jssh_checkDB(){
# $2 database # $2 database
# $3 id used to identify caller # $3 id used to identify caller
jssh_updateArray() { 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}" { flock -s -w 1 200; jssh_updateArray_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}"
} }
function jssh_updateArray_async() { function jssh_updateArray_async() {