Live Website / WordPress Data logging with Memcache

created August 20, 2013, last updated May 24, 2015.

closeThis post was last updated 9 years 4 months 3 days ago, some of the information contained here may no longer be actual and any referenced software versions may have been updated!

Memcache is a bit like Jesus, all things to all men. Blasphemy aside, I love using Memcache for PHP application solutions. It is versatile, simple to use and very fast.

This year I have used memcache for a whole bunch of great solutions. I wrote a memcache database wrapper class to automatically use memcache to cache database queries – this increased the page loading time of a website considerably.

I used memcache for simple counters, i.e. page visits and media plays – much faster than using a database.

I used it  to cache media playlists in memory for quick lookup and matching of current playing tracks on a live darwin media stream and I used it to cache Magento product data used on eBay listings.

Today I have been using it to cache logging data (eBay product views) that can be quickly retrieved and displayed live using Ajax. My PHP logging class can be called from any PHP app to log any sort of data using memcache for super fast cached storage and retrieval.

The class logs data in arrays and saves them to memcache, the data can be set to expire whenever you like, the client then sets a timerange i.e. 30 minutes and queries memcache for all log data stored in that time range and displays it in real time using Ajax requests.

Because all the logging is done in memory it is very fast to log and to extract the data – much faster than using a database or writing to a logfile. At the moment I am not interested in storing the data so when the cache TTL expires the data is gone, but If required the data could be extracted asynchronously via a cronjob to a more permanent storage medium.

This method of caching log data and viewing it live could be used for any data you like not just web related stats, e.g. live orders, live product stocks, live tweets, etc. etc.

In the example below you see live logging data for the last 30 mins from this WordPress blog – the logging class is being called by WordPress every time a page loads, the iframe is loading a simple webpage that uses jQuery and Ajax to retrieve the log data every 10 seconds. Look at all those pesky bots!


This site uses Akismet to reduce spam. Learn how your comment data is processed.