configfiles/scripts/.config/scripts/cmdcache.sh
2015-10-08 10:31:53 +02:00

24 lines
426 B
Bash
Executable file

#!/bin/bash
#Arguments
CMD=$2
CACHE_TIME=$1
#Constants
TMPDIR="/tmp/cache"
[ -d "$TMPDIR" ] || mkdir -p "$TMPDIR"
HASH=$(echo "$CMD" | base64)
CACHE_FILE="$TMPDIR/$HASH"
if [ -f "$CACHE_FILE" ]; then
CACHE_AGE=$[ $(date +"%s") - $(stat -c "%Y" "$CACHE_FILE") ]
if [ $CACHE_AGE -lt $CACHE_TIME ]; then
cat "$CACHE_FILE"
else
$CMD | tee "$CACHE_FILE"
fi
else
$CMD | tee "$CACHE_FILE"
fi