1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-13 05:59:06 +01:00
limosek-zaf/lib/cache.lib.sh

152 lines
3.3 KiB
Bash
Raw Normal View History

# Zaf cache related functions
zaf_cache_init(){
if [ -z "$ZAF_CACHE_DIR" ] || [ "$ZAF_CACHE_DIR" = "/tmp/zafc" ]; then
ZAF_CACHE_DIR=${ZAF_TMP_DIR}/zafc
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"
(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
)
else
zaf_dbg "Cache dir $ZAF_CACHE_DIR is not accessible! Disabling cache."
fi
}
zaf_cache_clean(){
if [ -n "$ZAF_CACHE_DIR" ]; then
zaf_wrn "Removing cache entries"
(cd $ZAF_CACHE_DIR && find ./ -type f -name '*.info' 2>/dev/null | \
while read line ; do
rm -f $line $(basename $line .info)
done
)
else
zaf_dbg "Cache dir not set. Disabling cache."
fi
zaf_cache_init
}
# Get cache key from requested param
2016-04-13 12:42:12 +02:00
zaf_cache_key(){
echo "$1" | (md5sum - ||md5) 2>/dev/null | cut -d ' ' -f 1
}
# Put object into cache
# $1 key
# $2 value
# $3 lifetime in seconds
zaf_tocache(){
! [ -w $ZAF_CACHE_DIR ] && return 1
local key
local value
local expiry
2016-12-01 10:28:14 +01:00
local lockfile
local infofile
local datafile
2016-04-13 12:42:12 +02:00
key=$(zaf_cache_key "$1")
2016-12-01 10:28:14 +01:00
datafile=$ZAF_CACHE_DIR/$key
lockfile=$ZAF_CACHE_DIR/${key}.lock
infofile=$ZAF_CACHE_DIR/${key}.info
[ -f $lockfile ] && return 1 # Key is locked, return
touch $lockfile
rm -f $datafile $infofile
echo "$2" >$datafile
echo "$1" >$infofile
expiry=$(zaf_date_add "$3")
zaf_trc "Cache: Saving entry $1[$key,expiry=$expiry]"
2016-12-01 10:28:14 +01:00
touch -m -d "$expiry" $infofile
rm -f $lockfile
}
# Put object into cache from stdin and copy to stdout
# $1 key
# $2 lifetime in seconds
zaf_tocache_stdin(){
2016-12-01 10:28:14 +01:00
! [ -w $ZAF_CACHE_DIR ] && { cat; return 1; }
local key
local expiry
2016-12-01 10:28:14 +01:00
local lockfile
local infofile
local datafile
2016-04-13 12:42:12 +02:00
key=$(zaf_cache_key "$1")
2016-12-01 10:28:14 +01:00
datafile=$ZAF_CACHE_DIR/$key
lockfile=$ZAF_CACHE_DIR/${key}.lock
infofile=$ZAF_CACHE_DIR/${key}.info
[ -f $lockfile ] && return 1 # Key is locked, return
touch $lockfile
rm -f $datafile $infofile
cat >$datafile
if [ -s $datafile ]; then
expiry="$(zaf_date_add $2)"
2016-12-01 10:28:14 +01:00
echo "$1 [key=$key,expiry=$expiry]" >$infofile
zaf_trc "Cache: Saving entry $1[key=$key,expiry=$expiry]"
2016-12-01 10:28:14 +01:00
touch -m -d "$expiry" $infofile
cat $datafile
rm -f $lockfile
2016-04-13 12:42:12 +02:00
else
2016-12-01 10:28:14 +01:00
rm -f "$datafile"
2016-04-13 12:42:12 +02:00
fi
}
# Remove entry from cache
# $1 key
zaf_cache_delentry(){
! [ -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*"
}
# 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
}
# Get object from cache
# $1 key
zaf_fromcache(){
! [ -r $ZAF_CACHE_DIR ] || [ -n "$ZAF_NOCACHE" ] && return 1
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 10:28:14 +01:00
datafile=$ZAF_CACHE_DIR/$key
infofile=$ZAF_CACHE_DIR/${key}.info
if [ -f $datafile ]; then
! [ -f "$infofile" ] && { return 3; }
if [ "$infofile" -nt "$datafile" ]; then
zaf_trc "Cache: serving $1($key) from cache"
2016-12-01 10:28:14 +01:00
cat $datafile
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
}
2016-12-01 10:28:14 +01:00