Cache invalidation with Django staticgenerator
2/6/2011After installing staticgenerator, my site has become noticeably faster. Since Nginx can directly serve cached files, the big Django processes on this server are kept idle for real work, not regenerating every single page.
The last piece was removing the cache after modifying a post. Removing the entire cache is the best method for this site, so I don't have to worry about category pages, author pages, sitemaps and the Random Entries widget that might include links to posts that don't exist anymore.
I don't post frequently enough that it matters to me, and since I'm using Disqus (which is awesome), I also don't have to delete the cache for every posted comment. Using Wordpress and WP-supercache, it's common to disable caching for logged-in users and those who've posted a comment. On some Wordpress sites, the caching tends to lag the comments, since it's overwhelming the server to keep regenerating the cache for each comment.
Anyway, here's what I wound up using. The documentation on signals is a bit spartan, so if I messed something up, please let me know in the comments.
from django.db.models import signals
from staticgenerator import recursive_delete
def delete_index(sender, *args, **kwargs):
recursive_delete('/')
signals.post_save.connect(delete_index)
signals.post_delete.connect(delete_index)