Skip to content
Permalink
Browse files

Implement --purge to clear a cached item

  • Loading branch information
semanticart committed Feb 5, 2020
1 parent ae8b56f commit 13dfde193c7c409de76d40aa8ca2739bd8176874
Showing with 36 additions and 3 deletions.
  1. +14 −3 cache
  2. +22 −0 test/cache.bats
17 cache
@@ -35,6 +35,10 @@ do
usage
exit 0
;;
--purge)
purge=0
shift # drop the key
;;
--stale-while-revalidate)
stale_while_revalidate="$2"
shift # drop the key
@@ -67,14 +71,21 @@ if [ -z "$cache_key" ]; then
exit 64
fi

cache_dir=${CACHE_DIR:-$TMPDIR}
cache_file="$cache_dir$cache_key"

if [ -n "$purge" ]; then
if [ -f "$cache_file" ]; then
rm "$cache_file"
fi
exit 0
fi

if [ -z "$1" ] && [ -z "$check_only" ]; then
echo "Error: You must provide a command"
exit 64
fi

cache_dir=${CACHE_DIR:-$TMPDIR}
cache_file="$cache_dir$cache_key"

fresh () {
# if the $cache_file doesn't exist, it can't be fresh
if [ ! -f "$cache_file" ]; then
@@ -237,3 +237,25 @@ wait_for_second_to_pass() {
[ "$status" -eq 1 ]
[ "$output" = "" ]
}

@test "--purge exits with 0 if the content is not yet cached" {
[ ! -f "$CACHE_DIR$TEST_KEY" ]

run ./cache --purge $TEST_KEY
[ "$status" -eq 0 ]
[ "$output" = "" ]

[ ! -f "$CACHE_DIR$TEST_KEY" ]
}

@test "--purge exits with 0 and removes the file if the content is cached" {
touch "$CACHE_DIR$TEST_KEY"

[ -f "$CACHE_DIR$TEST_KEY" ]

run ./cache --purge $TEST_KEY
[ "$status" -eq 0 ]
[ "$output" = "" ]

[ ! -f "$CACHE_DIR$TEST_KEY" ]
}

0 comments on commit 13dfde1

Please sign in to comment.
You can’t perform that action at this time.