As I'm sure I've mentioned before, memcached is extremely cool. It's allowed me to build caching into JOTWeb2 extremely quickly. Of course, caching does have it's problems… Today, Evelyn posted a journal entry (yeah, really :-), and then was like “What do you mean I have to wait up to 5 minutes before it shows up?!?”
I made a change to the code which posts new entries so that it would restart memcached when an entry is uploaded. That should work, but I had this sneaking suspicion that there must be a way to expire entries in the memcached without restarting the daemon. After some digging, I found that memcached supports “stats” and “flush_all” commands, but that the Python API for it does not have these functions in it.
So, I made the changes and have released a fork of python-memcached which has these changes in it. I forked it because I see that there is no existing maintainer for this code. I've contacted the Danga Interactive folks about becoming the new maintainer, but until I hear back we'll just have a forked version.
I would like, at some future point, to have JOTWeb2 implement a much smarter caching mechanism, which can detect things such as file modification times, cookies and important form fields, and the presence of form fields or cookies period, and probably even have callbacks so you can do things like a database lookup to see if it needs to be freshened. For example, our journal index page would probably want to expire the cached entry if the mtime on the HTML page or the Python code changed, or the results of “SELECT stamp FROM entries ORDER BY id DESC LIMIT 1” change.
The down side of this is that each cache hit becomes more expensive because it needs to do a database query. The benefit of the current mechanism is that it's incredibly fast. However, with database connection pooling, this probably won't be too bad and will ensure completely fresh page views.
In the mean-time, I just expire the whole cache when a new entry is loaded. I could be a bit more surgical because I know what pages get touched, but for our use it's not a win for us to be that clever. In the mean-time, I've upped the page expiration times to just under a day, which should be a huge win.comments powered by Disqus