-
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 all commits
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,46 @@ command_exists() { | |
local command="$1" | ||
command -v "$command" &> /dev/null | ||
} | ||
|
||
get_tmp_dir() { | ||
local tmpdir="${TMPDIR:-${TMP:-${TEMP:-/tmp}}}" | ||
[ -d "$tmpdir" ] || local tmpdir=~/tmp | ||
echo "$tmpdir/tmux-$EUID-cpu" | ||
} | ||
|
||
get_time() { | ||
date +%s.%N | ||
} | ||
|
||
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From
man tmux
So either
TMUX_TMPDIR
or/tmp
should existThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe that's why
tmux-copycat
does that if inside tmux envTMUX_TMPDIR
turn intoTMPDIR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the back and forth
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe; though I don't really see much point in reverting - surely checking TMP & TEMP is fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with both solution