Commits

Shlomi Fish committed 61d024c

Add the cache function

Comments (0)

Files changed (1)

shlomif-settings/bash-aliases/user_aliases.bash

 }
 alias ifconfig='/sbin/ifconfig'
 alias caddy="ruby $HOME/progs/perl/Quizzes/Golf/golf.shinh.org/caddy-tool-for-golfers/caddy/caddy.rb"
+
+# What this function does is cache the result of a command in a file, and
+# use the file to output the results in case it exists.
+# Format is: cache "$basename_to_cache_in" $cmd $arg1 $arg2 $arg3...
+cache()
+{
+    local cache_fn="$1"
+    shift
+
+    local dir="${CACHE_DIR:-.}"
+
+    if ! test -d "$dir"; then
+        mkdir -p "$dir"
+    fi
+
+    local fn="$dir/$cache_fn"
+    if ! test -f "$fn" ; then
+        "$@" > "$fn"
+    fi
+    cat "$fn"
+}