2016-04-12 15:28:29 +02:00
|
|
|
# Zaf cache related functions
|
|
|
|
|
2016-04-12 18:38:19 +02:00
|
|
|
zaf_cache_clean(){
|
|
|
|
if [ -n "$ZAF_CACHE_DIR" ]; then
|
|
|
|
zaf_wrn "Removing cache entries"
|
|
|
|
rm -rf "$ZAF_CACHE_DIR"
|
|
|
|
else
|
|
|
|
zaf_err "Cache dir not set."
|
|
|
|
fi
|
2016-04-12 15:28:29 +02:00
|
|
|
mkdir -p "$ZAF_CACHE_DIR"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get cache key from requested param
|
2016-04-13 12:42:12 +02:00
|
|
|
zaf_cache_key(){
|
2016-04-12 19:40:17 +02:00
|
|
|
echo "$1" | md5sum - | 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-13 16:04:00 +02:00
|
|
|
|
2016-04-13 12:42:12 +02:00
|
|
|
key=$(zaf_cache_key "$1")
|
2016-04-12 15:28:29 +02:00
|
|
|
echo "$2" >$ZAF_CACHE_DIR/$key
|
2016-04-13 16:04:00 +02:00
|
|
|
echo "$1" >$ZAF_CACHE_DIR/$key.info
|
|
|
|
touch -m -d "$3 seconds" $ZAF_CACHE_DIR/$key.info
|
2016-04-12 18:38:19 +02:00
|
|
|
zaf_trc "Cache: Saving entry $1($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-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-13 16:04:00 +02:00
|
|
|
|
2016-04-13 12:42:12 +02:00
|
|
|
key=$(zaf_cache_key "$1")
|
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
|
|
|
|
zaf_trc "Cache: Saving entry $1($key)"
|
2016-04-13 16:04:00 +02:00
|
|
|
echo "$1" >$ZAF_CACHE_DIR/$key.info
|
|
|
|
touch -m -d "$2 seconds" $ZAF_CACHE_DIR/$key.info
|
2016-04-13 12:42:12 +02:00
|
|
|
cat $ZAF_CACHE_DIR/$key
|
|
|
|
else
|
|
|
|
rm $ZAF_CACHE_DIR/$key
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
# 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-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
|
|
|
|
}
|
|
|
|
|