-
Notifications
You must be signed in to change notification settings - Fork 70
add cached eval #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add cached eval #56
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,3 +62,44 @@ command_exists() { | |
local command="$1" | ||
command -v "$command" &> /dev/null | ||
} | ||
|
||
get_tmp_dir() { | ||
echo "${TMPDIR:-/tmp}/tmux-$EUID-cpu" | ||
} | ||
|
||
get_time() { | ||
date +%s | ||
casperdcl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
get_cache_val(){ | ||
local key="$1" | ||
# seconds after which cache is invalidated | ||
local timeout="${2:-2}" | ||
local cache="$(get_tmp_dir)/$key" | ||
if [ -f "$cache" ]; then | ||
awk -v cache="$(head -n1 "$cache")" -v timeout=$timeout -v now=$(get_time) \ | ||
'BEGIN {if (now - timeout < cache) exit 0; exit 1}' \ | ||
&& tail -n+2 "$cache" | ||
fi | ||
} | ||
|
||
put_cache_val(){ | ||
local key="$1" | ||
local val="${@:2}" | ||
local tmpdir="$(get_tmp_dir)" | ||
[ ! -d "$tmpdir" ] && mkdir -p "$tmpdir" && chmod 0700 "$tmpdir" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's never global. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one of the main reasons I looked at |
||
echo "$(get_time)" > "$tmpdir/$key" | ||
echo -n "$val" >> "$tmpdir/$key" | ||
echo -n "$val" | ||
} | ||
|
||
cached_eval(){ | ||
local command="$1" | ||
local key="$(basename "$command")" | ||
local val="$(get_cache_val "$key")" | ||
if [ -z "$val" ]; then | ||
put_cache_val "$key" "$($command "${@:2}")" | ||
else | ||
echo -n "$val" | ||
fi | ||
} |
Uh oh!
There was an error while loading. Please reload this page.