Last active
April 23, 2020 22:47
-
-
Save ryanbrainard/8027626cd2947afefab7 to your computer and use it in GitHub Desktop.
Bash Cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# forked from http://stackoverflow.com/a/11901300/129492 | |
if [ $# == 0 ] || [ $1 == '-h' ] || [ $1 == '--help' ]; then | |
echo 'cache - cache the output of a command' | |
echo | |
echo 'Usage: cache [options] <command>' | |
echo | |
echo 'Options:' | |
echo ' -r, --reset [command]' | |
echo ' Reset the cache. If a command is provided,' | |
echo ' the only the cache for that command is reset;' | |
echo ' otherwise, the entire cache is reset.' | |
echo | |
echo '-h, --help' | |
echo ' Display this message' | |
exit | |
fi | |
if [ $1 == '-r' ] || [ $1 == '--reset' ]; then | |
reset=1 | |
shift | |
fi | |
cacheroot=/tmp/bashcache | |
# clear entire cache | |
if [ -n "$reset" ] && [ $# == 0 ]; then | |
rm -rf $cacheroot | |
exit | |
fi | |
mkdir -p $cacheroot | |
cachefile=$cacheroot/out | |
for i in "$@"; do | |
cachefile=${cachefile}_$(printf %s "$i" | sed 's/./\\&/g') | |
done | |
# clear specific cache | |
if [ -n "$reset" ]; then | |
rm -f $cachefile | |
fi | |
test -f "$cachefile" || "$@" > "$cachefile" | |
cat "$cachefile" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just came across this gist, and I'm curious if you've looked at bash-cache (I talk about it another answer on the linked question). It supports a number of features beyond this caching pattern, including support for environment variables, user-private caches preserving stderr as well as stdout, and asynchronous cache-warming. Feedback is welcome!