v2.1.0
===================================================================== --- New Scripts -------------------------- zsh ) database backup/restore - db/postgres/pg_dump - db/postgres/pg_restore - aws/rds/create-backup - aws/rds/load-backup redis-cached curl commands - redis/curl youtube download - youtube/download - youtube/get-audio-clip --- Changes ------------------------------ - 'scwrypts' executable now reloads upon execution to prevent staleness - added various options to improve api/cli; see 'scwrypts --help' for more --- Bug Fixes ---------------------------- - fixed an issue with .config settings' visibility to non-zsh scripts - fixed an issue with command arguments globbing too early
This commit is contained in:
22
zsh/redis/common.zsh
Normal file
22
zsh/redis/common.zsh
Normal file
@ -0,0 +1,22 @@
|
||||
_DEPENDENCIES+=(
|
||||
redis-cli
|
||||
)
|
||||
_REQUIRED_ENV+=()
|
||||
source ${0:a:h}/../common.zsh
|
||||
|
||||
[ ! $SCWRYPTS_CACHE_HOST ] && SCWRYPTS_CACHE_HOST=localhost
|
||||
[ ! $SCWRYPTS_CACHE_PORT ] && SCWRYPTS_CACHE_PORT=6379
|
||||
#####################################################################
|
||||
|
||||
_REDIS() {
|
||||
local ARGS=()
|
||||
|
||||
ARGS+=(-h $SCWRYPTS_CACHE_HOST)
|
||||
ARGS+=(-p $SCWRYPTS_CACHE_PORT)
|
||||
|
||||
[ $SCWRYPTS_CACHE_AUTH ] && ARGS+=(-a $SCWRYPTS_CACHE_AUTH)
|
||||
|
||||
redis-cli ${ARGS[@]} $@
|
||||
}
|
||||
|
||||
CACHE_ENABLED=$(_REDIS ping 2>&1 | grep -qi pong && echo 1 || echo 0)
|
48
zsh/redis/curl
Executable file
48
zsh/redis/curl
Executable file
@ -0,0 +1,48 @@
|
||||
#!/bin/zsh
|
||||
_DEPENDENCIES+=()
|
||||
_REQUIRED_ENV+=()
|
||||
source ${0:a:h}/common.zsh
|
||||
#####################################################################
|
||||
|
||||
CURL_WITH_CACHE() {
|
||||
[ ! $TTL ] && TTL=10
|
||||
[[ $CACHE_ENABLED -eq 0 ]] && {
|
||||
curl $@
|
||||
return $?
|
||||
}
|
||||
|
||||
local ARGS=()
|
||||
local URL
|
||||
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
case $1 in
|
||||
-- ) shift 1 ;;
|
||||
--*= ) ARGS+=($1); shift 1 ;;
|
||||
--* ) ARGS+=($1 $2); shift 2 ;;
|
||||
-* ) ARGS+=($1); shift 1 ;;
|
||||
* ) URL=$1; break ;;
|
||||
esac
|
||||
done
|
||||
|
||||
local KEY=$(GET_URL_KEY $URL)
|
||||
local OUTPUT=$(_REDIS get $KEY 2>&1)
|
||||
[ $OUTPUT ] && {
|
||||
[[ ${#ARGS[@]} -gt 0 ]] && __WARN "cache hit found; ignoring arguments ($ARGS)"
|
||||
echo $OUTPUT
|
||||
return
|
||||
}
|
||||
|
||||
local OUTPUT=$(curl -s $@)
|
||||
[ ! $OUTPUT ] && return 1
|
||||
|
||||
_REDIS set $KEY "$OUTPUT" >/dev/null
|
||||
_REDIS expire $KEY $TTL >/dev/null
|
||||
|
||||
echo $OUTPUT
|
||||
}
|
||||
|
||||
GET_URL_KEY() { echo "scwrypts:curl:$1" | sed 's/\s\+/+/g'; }
|
||||
|
||||
#####################################################################
|
||||
CURL_WITH_CACHE $@
|
Reference in New Issue
Block a user