2016-04-12 15:28:29 +02:00
|
|
|
# Zaf cache related functions
|
|
|
|
|
2016-04-24 04:51:35 +02:00
|
|
|
zaf_cache_init(){
|
2016-05-04 16:09:34 +02:00
|
|
|
[ -z "$ZAF_CACHE_DIR" ] && ZAF_CACHE_DIR=${ZAF_TMP_DIR}/zafc
|
|
|
|
if [ -w $ZAF_CACHE_DIR ]; then
|
|
|
|
zaf_trc "Cache: Removing stale entries"
|
|
|
|
(cd $ZAF_CACHE_DIR && find ./ -type f -name '*.info' -mmin +1 2>/dev/null | \
|
|
|
|
while read line ; do
|
|
|
|
rm -f $line $(basename $line .info)
|
|
|
|
done
|
|
|
|
)
|
2016-04-24 04:51:35 +02:00
|
|
|
else
|
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(){
|
|
|
|
if [ -n "$ZAF_CACHE_DIR" ]; then
|
|
|
|
zaf_wrn "Removing cache entries"
|
2016-05-04 16:09:34 +02:00
|
|
|
(cd $ZAF_CACHE_DIR && find ./ -type f -name '*.info' 2>/dev/null | \
|
|
|
|
while read line ; do
|
|
|
|
rm -f $line $(basename $line .info)
|
|
|
|
done
|
|
|
|
)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
# Put object into cache
|
|
|
|
# $1 key
|
|
|
|
# $2 value
|
|
|
|
# $3 lifetime in seconds
|
|
|
|
zaf_tocache(){
|
2016-04-13 16:04:00 +02:00
|
|
|
! [ -w $ZAF_CACHE_DIR ] && return 1
|
2016-04-12 15:28:29 +02:00
|
|
|
local key
|
|
|
|
local value
|
2016-04-19 15:48:12 +02:00
|
|
|
local expiry
|
2016-04-13 16:04:00 +02:00
|
|
|
|
2016-04-13 12:42:12 +02:00
|
|
|
key=$(zaf_cache_key "$1")
|
2016-04-21 15:59:29 +02:00
|
|
|
rm -f $ZAF_CACHE_DIR/$key $ZAF_CACHE_DIR/${key}.info
|
2016-04-12 15:28:29 +02:00
|
|
|
echo "$2" >$ZAF_CACHE_DIR/$key
|
2016-04-20 09:39:24 +02:00
|
|
|
echo "$1" >$ZAF_CACHE_DIR/${key}.info
|
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]"
|
|
|
|
touch -m -d "$expiry" $ZAF_CACHE_DIR/${key}.info
|
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-04-13 16:04:00 +02:00
|
|
|
! [ -w $ZAF_CACHE_DIR ] && return 1
|
2016-04-12 15:28:29 +02:00
|
|
|
local key
|
2016-04-19 15:48:12 +02:00
|
|
|
local expiry
|
2016-04-13 16:04:00 +02:00
|
|
|
|
2016-04-13 12:42:12 +02:00
|
|
|
key=$(zaf_cache_key "$1")
|
2016-04-21 15:59:29 +02:00
|
|
|
rm -f $ZAF_CACHE_DIR/$key $ZAF_CACHE_DIR/${key}.info
|
2016-04-12 15:28:29 +02:00
|
|
|
cat >$ZAF_CACHE_DIR/$key
|
2016-04-13 12:42:12 +02:00
|
|
|
if [ -s $ZAF_CACHE_DIR/$key ]; then
|
2016-04-20 09:39:24 +02:00
|
|
|
expiry="$(zaf_date_add $2)"
|
|
|
|
echo "$1 [key=$key,expiry=$expiry]" >$ZAF_CACHE_DIR/${key}.info
|
|
|
|
zaf_trc "Cache: Saving entry $1[key=$key,expiry=$expiry]"
|
2016-04-19 15:48:12 +02:00
|
|
|
touch -m -d "$expiry" $ZAF_CACHE_DIR/$key.info
|
2016-04-13 12:42:12 +02:00
|
|
|
cat $ZAF_CACHE_DIR/$key
|
|
|
|
else
|
2016-04-20 09:39:24 +02:00
|
|
|
rm -f "$ZAF_CACHE_DIR/$key"
|
2016-04-13 12:42:12 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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"
|
|
|
|
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(){
|
|
|
|
local i
|
|
|
|
ls $ZAF_CACHE_DIR/*info >/dev/null 2>/dev/null || return 1
|
|
|
|
local key
|
|
|
|
for i in $ZAF_CACHE_DIR/*info; do
|
|
|
|
cat $i
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:28:29 +02:00
|
|
|
# Get object from cache
|
|
|
|
# $1 key
|
|
|
|
zaf_fromcache(){
|
2016-04-13 16:04:00 +02:00
|
|
|
! [ -r $ZAF_CACHE_DIR ] || [ -n "$ZAF_NOCACHE" ] && return 1
|
2016-04-12 15:28:29 +02:00
|
|
|
local key
|
|
|
|
local value
|
2016-04-13 12:42:12 +02:00
|
|
|
key=$(zaf_cache_key "$1")
|
2016-04-12 15:28:29 +02:00
|
|
|
if [ -f $ZAF_CACHE_DIR/$key ]; then
|
2016-04-21 15:29:48 +02:00
|
|
|
! [ -f "$ZAF_CACHE_DIR/$key.info" ] && { return 3; }
|
2016-04-13 16:04:00 +02:00
|
|
|
if [ "$ZAF_CACHE_DIR/$key.info" -nt "$ZAF_CACHE_DIR/$key" ]; then
|
2016-04-12 15:28:29 +02:00
|
|
|
zaf_trc "Cache: serving $1($key) from cache"
|
|
|
|
cat $ZAF_CACHE_DIR/$key
|
|
|
|
else
|
|
|
|
zaf_trc "Cache: removing old entry $1"
|
|
|
|
rm -f "$ZAF_CACHE_DIR/$key*"
|
|
|
|
return 2
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
zaf_trc "Cache: missing entry $1($key)"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|