I wrote a bash script this morning to report the size of WordPress cache folders, the number of files they contain, read each file to prime the OS disk cache, and optionally flush the OS disk cache as well. This might be a script you could execute to email a daily/weekly report of cache folder sizes, or perhaps execute during/after booting a server to prime the OS disk cache, or even on a regular schedule to make sure the OS cache is always primed. The script also has a “flush” argument to sync and drop the OS disk cache, which isn’t very useful (to me) except to see the difference in speed between a clean and primed cache (about 11s vs 0.4s for all websites on my server).
$ wp-content-cache.sh -h
purpose: display size and number of files in WP cache folders prime and optionally flush the OS disk cache.
syntax: /usr/local/bin/wp-content-cache.sh [-h|--help] [-f|--flush] {sitedirs}
--flush: sync and drop the OS disk cache before priming.
{sitedirs}: optional path to website(s) base folder (default is /export/www/*).
$ wp-content-cache.sh /export/www/surniaulula/ surniaulula: wordpress/wp-content/plugins/blogroll-links-favicons/cache 8.0K 1 files wordpress/wp-content/plugins/bulletproof-security/admin/cache 4.0K 0 files wordpress/wp-content/plugins/db-cache-reloaded-fix/cache 156K 18 files wordpress/wp-content/plugins/bwp-minify/cache 592K 20 files wordpress/wp-content/plugins/wp-wikibox/cache 4.0K 0 files wordpress/wp-content/plugins/bwp-google-xml-sitemaps/cache 36K 8 files wordpress/wp-content/cache 5.5M 108 files wordpress/wp-content/widget-cache 96K 11 files wordpress/wp-content/gallery/cache 48K 3 files real 0m0.130s user 0m0.038s sys 0m0.092s
#!/bin/sh
#
# /usr/local/bin/wp-content-cache.sh
#
# Copyright 2012 - Jean-Sebastien Morisset - https://surniaulula.com/
#
# This script is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This script is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details at http://www.gnu.org/licenses/.
base_dir="/export/www/*"
wp_content="/wordpress/wp-content"
# read command line opts
while :
do
for arg in "$@"
do
case $arg in
-h|--help)
echo -e "\npurpose: display size and number of files in WP cache folders prime and optionally flush the OS disk cache.\n"
echo -e " syntax: $0 [-h|--help] [-f|--flush] {sitedirs}\n"
printf "%15s: %s\n" "--flush" "sync and drop the OS disk cache before priming."
printf "%15s: %s\n\n" "{sitedirs}" "optional path to website(s) base folder (default is $base_dir)."
exit 0
;;
-f|--flush)
flush="1"
shift 1
;;
-*) echo "error: unrecognized command line argument."
exit 1
;;
*) args[$(( i++ ))]="$1"
shift 1
;;
esac
continue 2
done
break
done
# reset $1, $2, etc. with left-over parameters
set -- "${args[@]}"
# set default sitedir(s) if none specified
[ -z "$@" ] && set -- $base_dir
if [ -n "$flush" ]
then
echo "(sync'ing disks)"
sync
echo "(droping caches)"
if [ -w /proc/sys/vm/drop_caches ]
then
#
# (excerpt from http://www.bramschoenmakers.nl/node/310)
#
# Erase the page cache. This part of the disk cache contains actual file contents.
#
# echo 1 >/proc/sys/vm/drop_caches
#
# Erase the inode and dentry caches (short: dcache). An inode is the data
# structure used by the kernel to represent a file. It does not contain the
# file's data, only attributes like the name, dates and permissions. A dentry
# is a data structure to represent a file path, which maps to an actual file.
#
# echo 2 >/proc/sys/vm/drop_caches
#
# Erase both the page cache, as the inode cache and dcache.
#
echo 3 >/proc/sys/vm/drop_caches
else
echo "error: cannot write to /proc/sys/vm/drop_caches file!"
fi
fi
time {
for website in "$@"
do
if [ -d "$website$wp_content" ]
then
echo "`basename $website`:"
# find any directory named "cache" anywhere under wp-content, or "*-cache" directly
# under wp-content (for "widget-cache" and others).
find "$website$wp_content/" \( -name "cache" -o -regex "^$website$wp_content/[^/]*-cache" \) \
-type d -print | while read cache_dir
do
printf "\t%-65s %6s %6s files\n" "`echo \"$cache_dir\"|sed \"s!$website/!!\"`" \
"`du -sh "$cache_dir/"|cut -f 1`" "`find "$cache_dir/" -type f -print|wc -l`"
find "$cache_dir/" -type f -exec cat {} \; >/dev/null
done
fi
done
}
You can download the wp-content-cache.sh script here.