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.
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!