In today’s article we are talking about cache which should not be confused with cash. However, it is equally important and and help your WordPress site’s cash flow. Oh so not I have your attention, GOOD! Unfortunately you are still skeptical
Believe me that having the right balance of cache will truly improve your site’s bottom line.The first step is understanding what a cache like memcache is so that we can comprehend how our websites can benefit from using it. According to the dictionary cache as it pertains to computer systems is defined as follows:
Computing an auxiliary memory from which high-speed retrieval is possible.
While there are numerous caching systems available for WordPress, memcache probably one of the best because it works across multiple servers and can support multiple web servers. In otherwords there is a distributed factor that the file system based caches like litecache and WP Super Cache can not touch. In addition memcache has the innate for helping lift load off of your MySql server by helping PHP cache common db queries. In my book that would a win win * 2.
I began this endeavor at the WebPageTest site and ran a test that returned results like this:
Well as impressive as the three A’s were, there was plenty of room for improvement. I began tweaking things and retesting as I went along. The first step was to turn on the appropriate apache compression settings with add-ons like mod_deflate and mod_gzip. I created respectively identified conf files in the Includes directory of my apache22 installation. After restarting apache we achieved another A.
File reads are performance hogs.
Fixing the static content portion was also fairly easy after a bit of shoveling with Google. All of the recommendations I found pointed to adding these static declarations to your site’s .htaccess file. I take issue with this because it is suboptimal. If you have access to restarting apache as I’ve mentioned above then place these settings in their own conf inside of your Includes directory. This is better because when you restart apache it reads these settings into it’s main configuration ONCE and for all sites it serves, in lieu of reading your .htaccess EVERY time a request is made for that particular site.
The next step was to fix some image compression issues. Unfortunately the two previous mods don’t really help with images as they are generally already compressed. In this case all that I could do was shrink those images down and convert them from PNG to JPG. Honestly there is nothing wrong with png images but when the image is of sufficient size for some reason the jpg version compresses better. It’s just a personal observation so take it with a grain of salt if you don’t believe me.
snippet from apache22/Includes/static_cache.conf: # 480 weeks <filesmatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"=""> Header set Cache-Control "max-age=290304000, public" # 2 DAYS <filesmatch ".(xml|txt)$"=""> Header set Cache-Control "max-age=172800, public, must-revalidate" # 2 HOURS <filesmatch ".(html|htm)$"=""> Header set Cache-Control "max-age=7200, must-revalidate"
Ok so at this point I was relatively happy with the site’s performance. I mean I can not afford a CDN so the big black X really doesn’t bother me. Unfortunately for some reason I just couldn’t let things be. Thus I began researching memcache. I user memcached when I worked at Etsy but unfortunately I didn’t get to really tinker with setting it up. Fortunately on FreeBSD the ports system makes this relatively trivial if you have the ports in stalled as well as portmaster.
I simply ran sudo portmaster in the port directory for memcached and it took care of ALL the dependencies upgrading the stale ones and installing the startup scripts etc… What it does not do it add the necessary configuration settings to your rc.conf thus I had to add these lines manually.
memcached_enable="YES" memcached_flags="-l 127.0.0.1"
Although I believe that they are self explanatory, it’s typical BSD to not automatically enable a newly installed service. It basically encourages you to ensure that your configuration options are properly set before you launch the service. In any event if you are not on a BSD then no big deal we can’t all be perfect. At this point you can launch memcache.
sudo /usr/local/etc/rc.d/memcached start
You can do a ps ax to check to see if the process is actually running but I found this handy alternative that let’s you query the services directly.
echo "stats settings" | nc localhost 11211 STAT maxbytes 67108864 STAT maxconns 1024 STAT tcpport 11211 STAT udpport 11211 STAT inter 127.0.0.1 STAT verbosity 0 STAT oldest 0 STAT evictions on STAT domain_socket NULL STAT umask 700 STAT growth_factor 1.25 STAT chunk_size 48 STAT num_threads 4 STAT num_threads_per_udp 4 STAT stat_key_prefix : STAT detail_enabled no STAT reqs_per_event 20 STAT cas_enabled yes STAT tcp_backlog 1024 STAT binding_protocol auto-negotiate STAT auth_enabled_sasl no STAT item_size_max 1048576 STAT maxconns_fast no STAT hashpower_init 0 STAT slab_reassign no STAT slab_automove 0 END
Neat hunh? Unfortunately, we are still no where near ready to make WordPress play nice with memcache. We still need to ensure that we have the PECL memcached extension installed so that PHP will know how to access the service. Once again you can install this from the ports and as usual I recommend using portmaster if for no other reason than the auto dependency resolution. Oh and remember that since this is a PHP extension you MUST restart apache in order to activate it. You can run the following commands to double check that it the extension is available.
sudo apachectl restart php -m |grep memcache
Ok now for the WordPress fun. You need to download and install the Memchaced Object Cache and Batcache plugins. Both plugins include detailed readme files and honestly this articles has fallen into the tl;dr domain. One thing to keep in mind I had WP Super Cache installed and it’s advanced-cache.php will conflict with batcache’s version so I renamed it to super-advanced-cache.php to avoid the conflict. At some point in the future I will consider shutting down the WPSC, but for now it’s alive with everything else.
At this point I believe that I have pushed the site about as far as I can and it will be a matter of time fo the system to prove itself fully. I mean when the test was run the cache was fresh so pretty much empty. I will keep an eye on things over time to see if it yields all of the promise that I hope.
Leave a Reply