2016-04-12 15:28:29 +02:00
|
|
|
# Zaf cache related functions
|
|
|
|
|
2016-04-24 04:51:35 +02:00
|
|
|
zaf_cache_init(){
|
2016-12-01 16:57:26 +01:00
|
|
|
local files
|
|
|
|
local file
|
|
|
|
|
2016-12-02 09:44:48 +01:00
|
|
|
if [ "$ZAF_CACHE_DIR" = "/tmp/zafc" ] && ! [ -d "$ZAF_CACHE_DIR" ]; then
|
2016-11-22 14:03:27 +01:00
|
|
|
mkdir -p $ZAF_CACHE_DIR
|
2016-11-22 15:19:28 +01:00
|
|
|
chown $ZAF_FILES_UID $ZAF_CACHE_DIR >/dev/null 2>/dev/null
|
2016-11-22 14:03:27 +01:00
|
|
|
fi
|
2016-12-02 09:44:48 +01:00
|
|
|
if ! [ -w $ZAF_CACHE_DIR ]; then
|
2016-05-04 16:09:34 +02:00
|
|
|
zaf_dbg "Cache dir $ZAF_CACHE_DIR is not accessible! Disabling cache."
|
2016-04-24 04:51:35 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-04-12 18:38:19 +02:00
|
|
|
zaf_cache_clean(){
|
2016-12-01 16:57:26 +01:00
|
|
|
local files
|
|
|
|
|
2016-12-02 09:44:48 +01:00
|
|
|
if [ -w "$ZAF_CACHE_DIR" ]; then
|
2016-04-12 18:38:19 +02:00
|
|
|
zaf_wrn "Removing cache entries"
|
2016-12-01 16:57:26 +01:00
|
|
|
files=$(find $ZAF_CACHE_DIR/ -type f)
|
|
|
|
[ -n "$files" ] && rm -f $files
|
2016-04-12 18:38:19 +02:00
|
|
|
else
|
2016-05-04 16:09:34 +02:00
|
|
|
zaf_dbg "Cache dir not set. Disabling cache."
|
2016-04-12 18:38:19 +02:00
|
|
|
fi
|
2016-04-24 04:51:35 +02:00
|
|
|
zaf_cache_init
|
2016-04-12 15:28:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Get cache key from requested param
|
2016-04-13 12:42:12 +02:00
|
|
|
zaf_cache_key(){
|
2016-04-20 09:39:24 +02:00
|
|
|
echo "$1" | (md5sum - ||md5) 2>/dev/null | cut -d ' ' -f 1
|
2016-04-12 15:28:29 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 16:57:26 +01:00
|
|
|
# Wait for lock
|
|
|
|
# $1 - key
|
|
|
|
zaf_cache_lock(){
|
|
|
|
local lockfile
|
|
|
|
lockfile="${ZAF_CACHE_DIR}/${key}.lock"
|
|
|
|
|
|
|
|
[ -f "$lockfile" ] && sleep 1
|
|
|
|
[ -f "$lockfile" ] && return 1
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# Unlock cache key
|
|
|
|
# $1 - key
|
|
|
|
zaf_cache_unlock(){
|
|
|
|
local lockfile
|
|
|
|
lockfile="${ZAF_CACHE_DIR}/${key}.lock"
|
|
|
|
|
|
|
|
rm -f $lockfile
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:28:29 +02:00
|
|
|
# Put object into cache
|
|
|
|
# $1 key
|
|
|
|
# $2 value
|
|
|
|
# $3 lifetime in seconds
|
|
|
|
zaf_tocache(){
|
2016-12-05 11:30:10 +01:00
|
|
|
! [ -w $ZAF_CACHE_DIR ] && return
|
2016-04-12 15:28:29 +02:00
|
|
|
local key
|
|
|
|
local value
|
2016-04-19 15:48:12 +02:00
|
|
|
local expiry
|
2016-12-01 10:28:14 +01:00
|
|
|
local infofile
|
|
|
|
local datafile
|
2016-04-13 16:04:00 +02:00
|
|
|
|
2016-04-13 12:42:12 +02:00
|
|
|
key=$(zaf_cache_key "$1")
|
2016-12-01 16:57:26 +01:00
|
|
|
datafile=${ZAF_CACHE_DIR}/$key
|
|
|
|
infofile=${ZAF_CACHE_DIR}/${key}.info
|
|
|
|
|
|
|
|
zaf_cache_lock "$key" || { zaf_wrn "Cache: Entry $1[$key] locked!"; return 1; }
|
2016-12-01 10:28:14 +01:00
|
|
|
|
|
|
|
rm -f $datafile $infofile
|
|
|
|
echo "$2" >$datafile
|
|
|
|
echo "$1" >$infofile
|
2016-04-19 15:48:12 +02:00
|
|
|
expiry=$(zaf_date_add "$3")
|
2016-04-20 09:39:24 +02:00
|
|
|
zaf_trc "Cache: Saving entry $1[$key,expiry=$expiry]"
|
2016-12-01 10:28:14 +01:00
|
|
|
touch -m -d "$expiry" $infofile
|
2016-12-01 16:57:26 +01:00
|
|
|
zaf_cache_unlock $key
|
2016-04-12 15:28:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Put object into cache from stdin and copy to stdout
|
|
|
|
# $1 key
|
|
|
|
# $2 lifetime in seconds
|
|
|
|
zaf_tocache_stdin(){
|
2016-12-05 11:30:10 +01:00
|
|
|
! [ -w $ZAF_CACHE_DIR ] && { cat; return; }
|
2016-04-12 15:28:29 +02:00
|
|
|
local key
|
2016-04-19 15:48:12 +02:00
|
|
|
local expiry
|
2016-12-01 10:28:14 +01:00
|
|
|
local infofile
|
|
|
|
local datafile
|
2016-04-13 16:04:00 +02:00
|
|
|
|
2016-04-13 12:42:12 +02:00
|
|
|
key=$(zaf_cache_key "$1")
|
2016-12-01 16:57:26 +01:00
|
|
|
datafile=${ZAF_CACHE_DIR}/$key
|
|
|
|
lockfile=${ZAF_CACHE_DIR}/${key}.lock
|
|
|
|
infofile=${ZAF_CACHE_DIR}/${key}.info
|
2016-12-01 10:28:14 +01:00
|
|
|
|
2016-12-01 16:57:26 +01:00
|
|
|
zaf_cache_lock "$key" || { zaf_wrn "Cache: Entry $1[$key] locked!"; return 1; }
|
2016-12-01 10:28:14 +01:00
|
|
|
|
|
|
|
rm -f $datafile $infofile
|
|
|
|
cat >$datafile
|
2016-12-01 16:57:26 +01:00
|
|
|
expiry="$(zaf_date_add $2)"
|
|
|
|
echo "$1 [key=$key,expiry=$expiry]" >$infofile
|
|
|
|
zaf_trc "Cache: Saving entry $1[key=$key,expiry=$expiry]"
|
|
|
|
touch -m -d "$expiry" $infofile
|
|
|
|
zaf_cache_unlock "$key"
|
|
|
|
cat $datafile
|
2016-04-13 12:42:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Remove entry from cache
|
|
|
|
# $1 key
|
|
|
|
zaf_cache_delentry(){
|
2016-04-13 16:04:00 +02:00
|
|
|
! [ -w $ZAF_CACHE_DIR ] && return 1
|
2016-04-13 12:42:12 +02:00
|
|
|
local key
|
|
|
|
key=$(zaf_cache_key "$1")
|
|
|
|
zaf_trc "Cache: removing $1($key) from cache"
|
2016-12-01 16:57:26 +01:00
|
|
|
rm -f "${ZAF_CACHE_DIR}/$key*"
|
2016-04-12 15:28:29 +02:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:39:24 +02:00
|
|
|
# List entries in cache
|
|
|
|
zaf_cache_list(){
|
2016-12-02 09:44:48 +01:00
|
|
|
! [ -w $ZAF_CACHE_DIR ] && return 1
|
2016-04-20 09:39:24 +02:00
|
|
|
local i
|
2016-12-01 16:57:26 +01:00
|
|
|
ls ${ZAF_CACHE_DIR}/*info >/dev/null 2>/dev/null || return 1
|
2016-04-20 09:39:24 +02:00
|
|
|
local key
|
2016-12-01 16:57:26 +01:00
|
|
|
for i in ${ZAF_CACHE_DIR}/*info; do
|
2016-04-20 09:39:24 +02:00
|
|
|
cat $i
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:28:29 +02:00
|
|
|
# Get object from cache
|
|
|
|
# $1 key
|
|
|
|
zaf_fromcache(){
|
2016-12-02 09:44:48 +01:00
|
|
|
! [ -w $ZAF_CACHE_DIR ] && return 3
|
2016-04-12 15:28:29 +02:00
|
|
|
local key
|
|
|
|
local value
|
2016-12-01 10:28:14 +01:00
|
|
|
local infofile
|
|
|
|
local datafile
|
|
|
|
|
2016-04-13 12:42:12 +02:00
|
|
|
key=$(zaf_cache_key "$1")
|
2016-12-01 16:57:26 +01:00
|
|
|
datafile="${ZAF_CACHE_DIR}/${key}"
|
|
|
|
infofile="${ZAF_CACHE_DIR}/${key}.info"
|
2016-12-01 10:28:14 +01:00
|
|
|
|
|
|
|
if [ -f $datafile ]; then
|
|
|
|
if [ "$infofile" -nt "$datafile" ]; then
|
2016-04-12 15:28:29 +02:00
|
|
|
zaf_trc "Cache: serving $1($key) from cache"
|
2016-12-02 09:44:48 +01:00
|
|
|
cat "$datafile"
|
2016-04-12 15:28:29 +02:00
|
|
|
else
|
2016-12-02 09:44:48 +01:00
|
|
|
zaf_cache_delentry $key
|
2016-04-12 15:28:29 +02:00
|
|
|
return 2
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
zaf_trc "Cache: missing entry $1($key)"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-12-01 10:28:14 +01:00
|
|
|
|