Skip to content

Instantly share code, notes, and snippets.

@orlandothoeny
Last active July 23, 2020 19:23
Show Gist options
  • Save orlandothoeny/488f5e447a51d16ef87c93d875ee5e61 to your computer and use it in GitHub Desktop.
Save orlandothoeny/488f5e447a51d16ef87c93d875ee5e61 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
# Parameters
# $1: The scheme and host of the site for which the cache should be warmed
warmCache () {
# E.g. https://www.example.com
local schemeAndHost="${1:?'The schemeAndHost parameter is missing'}"
printf '\e[38;5;33m=> Warming cache of %s by reading the /sitemap.xml file and sending a request to every listed url\e[0m\n' "${schemeAndHost}"
# MacOS doesn't use GNU grep.
# It can be installed using Homebrew and used as `ggrep`.
local grepCommand
if [[ "${OSTYPE}" == "darwin"* ]]; then
brew install grep
grepCommand='ggrep'
else
grepCommand='grep'
fi
# Matches <loc>URL</loc> and excludes the loc tags from the match, so we only have the actual url as match
local regex="(?<=<loc>)${schemeAndHost}.+?(?=<\/loc>$)"
local startTime=$(date +%s)
curl "${schemeAndHost}"/sitemap.xml -H 'Accept-Encoding: gzip, deflate, sdch' -H 'User-Agent: Cache Warmer Script' -H 'Connection: keep-alive' --compressed -s -o - | \
eval "${grepCommand} -o -P \"${regex}\"" | \
while read -r line; do
printf '=> Sending request to \"%s\"\n' "${line}"
curl "${line}" -H 'Accept-Encoding: gzip, deflate, sdch' -H 'User-Agent: Cache Warmer Script' -H 'Connection: keep-alive' --compressed -s -o /dev/null -w '==> Received response after %{time_starttransfer} seconds\n'
done
local endTime=$(date +%s)
printf "\e[32m=> Warming cache complete\e[0m\n"
printf "=> Took %s\n" "$(secs_to_human $(($endTime - $startTime)))"
}
# Prints given seconds as a human readable string
secs_to_human() {
local seconds=${1}
echo "$(( (${seconds} / 60) % 60 ))m $(( ${seconds} % 60 ))s"
}
warmCache "${1}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment