I was working on making a cache in bash and I found this link - But this guy is attempting to generate new bash functions by introspecting their source and generating a new one as a cache. This seems to me, way overkill. Here is my implementation and it works just fine:
cache() {
local file
file="/tmp/$(printf "%s" ${@})"
if [[ -f "${file}" && $(($(date +%s) - $(date -r "$file" +%s))) -le 1800 ]]; then
cat "${file}"
else
${1} ${@:2} | tee "${file}"
fi
}
mkurls() {
local url="${1:?You must supply a URL}"
local batchSize=${2:-100}
local repoCount
repoCount=$(curl -fsnL "$url?pagelen=0&page=1" | jq -re .size)
local pages=$(((repoCount / batchSize) + 1))
printf "$url?pagelen=${batchSize}&page=%s\n" $(seq 1 "$pages")
}
bbusers() {
_users() {
mkurls https://api.bitbucket.org/2.0/teams/twengg/members/ |
xargs -n 30 -P 20 curl -fsnL | jq -ser '.[].values[] | "\(.display_name), \(.links.self.href)" ' | sort
}
cache _users $@
}
and here are the results:
$ time bbusers &> /dev/null
#output
real 0m5.670s
user 0m0.184s
sys 0m0.099s
$ time bbusers &> /dev/null # seconds later
real 0m0.053s
user 0m0.010s
sys 0m0.017s
As you can see, it's literally 100x faster. If I delete the file behind the cache:
$ rm -fR /tmp/_users
$ time bbusers &> /dev/null
real 0m4.924s
user 0m0.170s
sys 0m0.082s
It goes right back to normal. So, how can this be improved and what am I missing that warrants such a wildly complicated approach as the other guy has?