├── README.md └── nginx-cache-purge /README.md: -------------------------------------------------------------------------------- 1 | # Bash script for deleting an item or set of items from Nginx cache 2 | 3 | ## Introduction 4 | 5 | This simple script **removes** an item or set of items from 6 | [Nginx](http://nginx.org) cache, be it 7 | [`fastcgi`](http://wiki.nginx.org/HttpFcgiModule#fastcgi_cache) or 8 | [`proxy`](http://wiki.nginx.org/HttpProxyModule#proxy_cache). 9 | 10 | It accepts a 11 | [`grep` pattern](http://www.gnu.org/software/grep/manual/grep.html#Fundamental-Structure) 12 | as argument to search for cached items in the given cache directory. 13 | 14 | This script uses `grep` 15 | [**basic**](http://www.gnu.org/software/grep/manual/grep.html#Basic-vs-Extended) 16 | regular expressions. Pressuposes the use of 17 | [GNU `grep`](http://www.gnu.org/software/grep/manual/grep.html). 18 | 19 | The script **requires** `rw` (read-write) access to the cache 20 | directory. 21 | 22 | ## Usage 23 | 24 | 1. Delete `foobar.css` from the `/var/cache/nginx/baz` cache. 25 | 26 | nginx-cache-purge "foobar.cs" /var/cache/nginx/baz 27 | 28 | 2. Delete all JPEG files from the `/var/cache/nginx/img` cache. 29 | 30 | nginx-cache-purge "\.jpe*g" /var/cache/nginx/img 31 | 32 | ## Installation 33 | 34 | 1. Clone the repo: 35 | 36 | git clone git://github.com/perusio/nginx-cache-purge.git 37 | 38 | 2. Place the script in a convenient place. 39 | 40 | 3. Done. 41 | 42 | ## See also 43 | 44 | There's another [script](https://github.com/perusio/nginx-cache-inspector) 45 | on github for **inspecting** cache items. 46 | -------------------------------------------------------------------------------- /nginx-cache-purge: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # nginx-cache-purge --- A simple Bash script to delete an item or set 4 | # of items specified through a grep pattern from 5 | # the Nginx cache. 6 | 7 | # Copyright (C) 2011 António P. P. Almeida 8 | 9 | # Author: António P. P. Almeida 10 | 11 | # Permission is hereby granted, free of charge, to any person obtaining a 12 | # copy of this software and associated documentation files (the "Software"), 13 | # to deal in the Software without restriction, including without limitation 14 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, 15 | # and/or sell copies of the Software, and to permit persons to whom the 16 | # Software is furnished to do so, subject to the following conditions: 17 | 18 | # The above copyright notice and this permission notice shall be included in 19 | # all copies or substantial portions of the Software. 20 | 21 | # Except as contained in this notice, the name(s) of the above copyright 22 | # holders shall not be used in advertising or otherwise to promote the sale, 23 | # use or other dealings in this Software without prior written authorization. 24 | 25 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 26 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 27 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 28 | # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 29 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 30 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 31 | # DEALINGS IN THE SOFTWARE. 32 | 33 | SCRIPTNAME=${0##*/} 34 | 35 | function print_usage() { 36 | echo "$SCRIPTNAME ." 37 | } 38 | 39 | ## Check the number of arguments. 40 | if [ $# -ne 2 ]; then 41 | print_usage 42 | exit 1 43 | fi 44 | 45 | ## Return the files where the items are cached. 46 | ## $1 - the filename, can be a pattern . 47 | ## $2 - the cache directory. 48 | ## $3 - (optional) the number of parallel processes to run for grep. 49 | function get_cache_files() { 50 | ## The maximum number of parallel processes. 16 since the cache 51 | ## naming scheme is hex based. 52 | local max_parallel=${3-16} 53 | ## Get the cache files running grep in parallel for each top level 54 | ## cache dir. 55 | find $2 -maxdepth 1 -type d | xargs -P $max_parallel -n 1 grep -ERl "^KEY:.*$1" | sort -u 56 | } # get_cache_files 57 | 58 | ## Removes an item from the given cache zone. 59 | ## $1 - the filename, can be a pattern . 60 | ## $2 - the cache directory. 61 | function nginx_cache_purge_item() { 62 | local cache_files 63 | 64 | [ -d $2 ] || exit 2 65 | cache_files=$(get_cache_files "$1" $2) 66 | 67 | ## Act based on grep result. 68 | if [ -n "$cache_files" ]; then 69 | ## Loop over all matched items. 70 | for i in $cache_files; do 71 | [ -f $i ] || continue 72 | echo "Deleting $i from $2." 73 | rm $i 74 | done 75 | else 76 | echo "$1 is not cached." 77 | exit 3 78 | fi 79 | } # nginx_cache_purge_item 80 | 81 | ## Try to purge the given item from the cache. 82 | nginx_cache_purge_item $1 $2 83 | --------------------------------------------------------------------------------