From 3e2f1502d11b38e9aa1cacddeb2b40df1da38652 Mon Sep 17 00:00:00 2001 From: Lukas Macura Date: Fri, 2 Dec 2016 09:44:48 +0100 Subject: [PATCH] Simplified caching --- lib/cache.lib.sh | 26 ++++++-------------- lib/ctrl.lib.sh | 62 +++++++++++++++++++----------------------------- zaf | 3 +++ 3 files changed, 35 insertions(+), 56 deletions(-) diff --git a/lib/cache.lib.sh b/lib/cache.lib.sh index 733a711..64cfd19 100644 --- a/lib/cache.lib.sh +++ b/lib/cache.lib.sh @@ -4,22 +4,11 @@ zaf_cache_init(){ local files local file - if [ -z "$ZAF_CACHE_DIR" ] || [ "$ZAF_CACHE_DIR" = "/tmp/zafc/" ]; then - ZAF_CACHE_DIR=${ZAF_TMP_DIR}/zafc + if [ "$ZAF_CACHE_DIR" = "/tmp/zafc" ] && ! [ -d "$ZAF_CACHE_DIR" ]; then mkdir -p $ZAF_CACHE_DIR chown $ZAF_FILES_UID $ZAF_CACHE_DIR >/dev/null 2>/dev/null fi - if [ -w $ZAF_CACHE_DIR ]; then - zaf_trc "Cache: Removing stale entries" - files=$(find $ZAF_CACHE_DIR/ -type f -name '*.lock' -mmin +1) - [ -n "$files" ] && rm -f $files - (cd $ZAF_CACHE_DIR && find ./ -type f -name '*.info' -mmin +1 2>/dev/null | \ - while read line ; do - file=$(basename $line .info) - [ "$line" -nt "$file" ] && { rm ${file}*; zaf_trc "rm ${file}*"; } - done - ) - else + if ! [ -w $ZAF_CACHE_DIR ]; then zaf_dbg "Cache dir $ZAF_CACHE_DIR is not accessible! Disabling cache." fi } @@ -27,7 +16,7 @@ zaf_cache_init(){ zaf_cache_clean(){ local files - if [ -n "$ZAF_CACHE_DIR" ]; then + if [ -w "$ZAF_CACHE_DIR" ]; then zaf_wrn "Removing cache entries" files=$(find $ZAF_CACHE_DIR/ -type f) [ -n "$files" ] && rm -f $files @@ -128,6 +117,7 @@ zaf_cache_delentry(){ # List entries in cache zaf_cache_list(){ + ! [ -w $ZAF_CACHE_DIR ] && return 1 local i ls ${ZAF_CACHE_DIR}/*info >/dev/null 2>/dev/null || return 1 local key @@ -139,7 +129,7 @@ zaf_cache_list(){ # Get object from cache # $1 key zaf_fromcache(){ - ! [ -r $ZAF_CACHE_DIR ] || [ -n "$ZAF_NOCACHE" ] && return 1 + ! [ -w $ZAF_CACHE_DIR ] && return 3 local key local value local infofile @@ -150,13 +140,11 @@ zaf_fromcache(){ infofile="${ZAF_CACHE_DIR}/${key}.info" if [ -f $datafile ]; then - #zaf_cache_lock $key || return 3 - #zaf_cache_unlock $key if [ "$infofile" -nt "$datafile" ]; then zaf_trc "Cache: serving $1($key) from cache" - cat "$datafile" || { ls -la "$datafile" >&2; zaf_err "auuu: $1"; } + cat "$datafile" else - #zaf_cache_delentry $key + zaf_cache_delentry $key return 2 fi else diff --git a/lib/ctrl.lib.sh b/lib/ctrl.lib.sh index c769d76..2246e4f 100644 --- a/lib/ctrl.lib.sh +++ b/lib/ctrl.lib.sh @@ -76,19 +76,15 @@ zaf_block_get_option() { zaf_ctrl_get_global_option() { local ctrlvar local ctrlopt - local key - - key="$1-$2" - if ! zaf_fromcache $key; then - ctrlopt="ZAF_CTRL_$(zaf_stripctrl $2)" - eval ctrlvar=\$$ctrlopt - if [ -n "$ctrlvar" ]; then - zaf_dbg "Overriding control field $2 from env $ctrlopt($ctrlvar)" - echo $ctrlopt - else - zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "$2" \ - || zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2" - fi | zaf_tocache_stdin "$key" "600" + + ctrlopt="ZAF_CTRL_$(zaf_stripctrl $2)" + eval ctrlvar=\$$ctrlopt + if [ -n "$ctrlvar" ]; then + zaf_dbg "Overriding control field $2 from env $ctrlopt($ctrlvar)" + echo $ctrlopt + else + zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "$2" \ + || zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2" fi } @@ -99,19 +95,15 @@ zaf_ctrl_get_global_option() { zaf_ctrl_get_item_option() { local ctrlvar local ctrlopt - local key - key="i$1-$2-$3" - if ! zaf_fromcache "$key"; then - ctrlopt="ZAF_CTRLI_$(zaf_stripctrl $2)_$(zaf_stripctrl $3)" - eval ctrlvar=\$$ctrlopt - if [ -n "$ctrlvar" ]; then - zaf_dbg "Overriding item control field $2/$3 from env $ctrlopt($ctrlvar)" - echo $ctrlopt - else - zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" \ - || zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3" - fi | zaf_tocache_stdin "$key" "600" + ctrlopt="ZAF_CTRLI_$(zaf_stripctrl $2)_$(zaf_stripctrl $3)" + eval ctrlvar=\$$ctrlopt + if [ -n "$ctrlvar" ]; then + zaf_dbg "Overriding item control field $2/$3 from env $ctrlopt($ctrlvar)" + echo $ctrlopt + else + zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" \ + || zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3" fi } @@ -122,19 +114,15 @@ zaf_ctrl_get_item_option() { zaf_ctrl_get_extitem_option() { local ctrlvar local ctrlopt - local key - key="e$1-$2-$3" - if ! zaf_fromcache "$key"; then - ctrlopt="ZAF_CTRLI_$(zaf_stripctrl $2)_$(zaf_stripctrl $3)" - eval ctrlvar=\$$ctrlopt - if [ -n "$ctrlvar" ]; then - zaf_dbg "Overriding item control field $2/$3 from env $ctrlopt($ctrlvar)" - echo $ctrlopt - else - zaf_ctrl_get_extitem_block <$1 "$2" | zaf_block_get_moption "$3" \ - || zaf_ctrl_get_extitem_block <$1 "$2" | zaf_block_get_option "$3" - fi | zaf_tocache_stdin "$key" "600" + ctrlopt="ZAF_CTRLI_$(zaf_stripctrl $2)_$(zaf_stripctrl $3)" + eval ctrlvar=\$$ctrlopt + if [ -n "$ctrlvar" ]; then + zaf_dbg "Overriding item control field $2/$3 from env $ctrlopt($ctrlvar)" + echo $ctrlopt + else + zaf_ctrl_get_extitem_block <$1 "$2" | zaf_block_get_moption "$3" \ + || zaf_ctrl_get_extitem_block <$1 "$2" | zaf_block_get_option "$3" fi } diff --git a/zaf b/zaf index 86a9f2a..16152a7 100755 --- a/zaf +++ b/zaf @@ -97,6 +97,9 @@ agent-config) | zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf done ;; +server-config) + zaf_wrn "Not implemented yet" + ;; ###### Plugins related commands update)