Archive for tag "code"

11Apr

Setting and deleting cache in Django with tags

The Django cache system is real help when you need to keep your site as fast as possible. It offers multiple back-ends and is super easy to get started using. I just wish it had the ability to tag content when you are setting it in the cache. If you could do that then you could do bulk deleting of content based on tags. One use-case that comes up again and again for me is caching paginated results. If you have blog-entry1-page1 and blog-entry1-page2 both stored in the cache and want to delete them when a new entry is added what do you do? It’s hard if not impossible you know which paged set is currently in cache and we need to know the keys in order to delete them.

I searched and searched for a solution to this problem with not much help from google. I did find this post by Eric Florenzano which is close to the approach I took, but I wanted something more generic. So I decided to add two new methods cache.set_with_tags and cache.delete_by_tags to whatever cache backend you are using. Just to note this is only useful if you intend to utilize the low-level cache framework.

read more

About Steps and Numbers
avatar

Steps and Numbers is the personal blog of web developer/designer Sean Brant. I enjoy coding and designing wonderful user experiences for the web. I spend my spare time hanging out in the beautiful city of Chicago, coding random projects most of which never see the light of day, and attempting to resurrect my failed carrier as a rock musician. You can contact me if you want and I’ll try and respond.

Flickr Photos
IMG_0970.JPG
IMG_0991.JPG
IMG_1002.JPG
IMG_0990.JPG
IMG_1015.JPG
IMG_1001.JPG
Search
Feeds
Post, Links, and Quotes
Just Posts
Just Links
Just Quotes