1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-24 19:39:08 +01:00

Cache locking support

This commit is contained in:
Lukas Macura 2016-12-01 10:28:14 +01:00
parent 336d120bb5
commit 4842cdaee1

View File

@ -46,35 +46,56 @@ zaf_tocache(){
local key local key
local value local value
local expiry local expiry
local lockfile
local infofile
local datafile
key=$(zaf_cache_key "$1") key=$(zaf_cache_key "$1")
rm -f $ZAF_CACHE_DIR/$key $ZAF_CACHE_DIR/${key}.info datafile=$ZAF_CACHE_DIR/$key
echo "$2" >$ZAF_CACHE_DIR/$key lockfile=$ZAF_CACHE_DIR/${key}.lock
echo "$1" >$ZAF_CACHE_DIR/${key}.info 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") expiry=$(zaf_date_add "$3")
zaf_trc "Cache: Saving entry $1[$key,expiry=$expiry]" zaf_trc "Cache: Saving entry $1[$key,expiry=$expiry]"
touch -m -d "$expiry" $ZAF_CACHE_DIR/${key}.info touch -m -d "$expiry" $infofile
rm -f $lockfile
} }
# Put object into cache from stdin and copy to stdout # Put object into cache from stdin and copy to stdout
# $1 key # $1 key
# $2 lifetime in seconds # $2 lifetime in seconds
zaf_tocache_stdin(){ zaf_tocache_stdin(){
! [ -w $ZAF_CACHE_DIR ] && return 1 ! [ -w $ZAF_CACHE_DIR ] && { cat; return 1; }
local key local key
local expiry local expiry
local lockfile
local infofile
local datafile
key=$(zaf_cache_key "$1") key=$(zaf_cache_key "$1")
rm -f $ZAF_CACHE_DIR/$key $ZAF_CACHE_DIR/${key}.info datafile=$ZAF_CACHE_DIR/$key
cat >$ZAF_CACHE_DIR/$key lockfile=$ZAF_CACHE_DIR/${key}.lock
if [ -s $ZAF_CACHE_DIR/$key ]; then 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)" expiry="$(zaf_date_add $2)"
echo "$1 [key=$key,expiry=$expiry]" >$ZAF_CACHE_DIR/${key}.info echo "$1 [key=$key,expiry=$expiry]" >$infofile
zaf_trc "Cache: Saving entry $1[key=$key,expiry=$expiry]" zaf_trc "Cache: Saving entry $1[key=$key,expiry=$expiry]"
touch -m -d "$expiry" $ZAF_CACHE_DIR/$key.info touch -m -d "$expiry" $infofile
cat $ZAF_CACHE_DIR/$key cat $datafile
rm -f $lockfile
else else
rm -f "$ZAF_CACHE_DIR/$key" rm -f "$datafile"
fi fi
} }
@ -104,12 +125,18 @@ zaf_fromcache(){
! [ -r $ZAF_CACHE_DIR ] || [ -n "$ZAF_NOCACHE" ] && return 1 ! [ -r $ZAF_CACHE_DIR ] || [ -n "$ZAF_NOCACHE" ] && return 1
local key local key
local value local value
local infofile
local datafile
key=$(zaf_cache_key "$1") key=$(zaf_cache_key "$1")
if [ -f $ZAF_CACHE_DIR/$key ]; then datafile=$ZAF_CACHE_DIR/$key
! [ -f "$ZAF_CACHE_DIR/$key.info" ] && { return 3; } infofile=$ZAF_CACHE_DIR/${key}.info
if [ "$ZAF_CACHE_DIR/$key.info" -nt "$ZAF_CACHE_DIR/$key" ]; then
if [ -f $datafile ]; then
! [ -f "$infofile" ] && { return 3; }
if [ "$infofile" -nt "$datafile" ]; then
zaf_trc "Cache: serving $1($key) from cache" zaf_trc "Cache: serving $1($key) from cache"
cat $ZAF_CACHE_DIR/$key cat $datafile
else else
zaf_trc "Cache: removing old entry $1" zaf_trc "Cache: removing old entry $1"
rm -f "$ZAF_CACHE_DIR/$key*" rm -f "$ZAF_CACHE_DIR/$key*"
@ -121,3 +148,4 @@ zaf_fromcache(){
fi fi
} }