Skip to content

Instantly share code, notes, and snippets.

@mricon
Created June 13, 2018 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mricon/28dd36fa093f9cb0748f63756f0f0a8d to your computer and use it in GitHub Desktop.
Save mricon/28dd36fa093f9cb0748f63756f0f0a8d to your computer and use it in GitHub Desktop.
#!/bin/bash
# Don't bother with anything smaller than 1MB in size
MINSIZE="1M"
# Clean up any cache files older than 1 week, regardless of size
MAXAGE="7"
# In a multisite case, avoid running all cleaners at once
if [ "$2" != '--now' ]; then
sleep $(((RANDOM % 60)))
fi
CGITRC=$1
if [ -z "$CGITRC" ]; then
CGITRC="/etc/cgitrc"
fi
CACHE_ROOT=$(grep '^cache-root=' $CGITRC | cut -d= -f2 2>/dev/null)
SNAPSHOT_TTL=$(grep '^cache-snapshot-ttl=' $CGITRC | cut -d= -f2 2>/dev/null)
STATIC_TTL=$(grep '^cache-static-ttl=' $CGITRC | cut -d= -f2 2>/dev/null)
if [ -z "$CACHE_ROOT" ]; then
CACHE_ROOT="/var/cache/cgit"
fi
if [ -z "$SNAPSHOT_TTL" ]; then
SNAPSHOT_TTL="10"
fi
for CACHEFILE in $(find $CACHE_ROOT -type f -size +$MINSIZE -mmin +$SNAPSHOT_TTL); do
if head -n1 $CACHEFILE | grep -q '/snapshot/'; then
rm -f $CACHEFILE
fi
done
if [ ! -z "$STATIC_TTL" ]; then
for CACHEFILE in $(find $CACHE_ROOT -type f -size +$MINSIZE -mmin +$STATIC_TTL); do
if head -n1 $CACHEFILE | grep -q 'id='; then
rm -f $CACHEFILE
fi
done
fi
find $CACHE_ROOT -type f -mtime +$MAXAGE -delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment