Django Caching with Users

96 Views Asked by At

I was wondering if there is a way to cache querysets to memcache on a site that has authenticated users and users that are not authenticated.

Basically I just need to cache queries from one table.

Any ideas would be great.

Thanks

1

There are 1 best solutions below

2
On

Check out johnny-cache. This worked great for us until we did so much writing (updating records) that we were invalidating cache constantly. At that point we just started using memcache directly, like this.

cache.set("some_unique_key", my_queryset, 3600)
cache_object = cache.get(cache_key)

If you're dealing with large querysets or objects, you might want to pickle them first.

cache.set("some_unique_key", zlib.compress(cPickle.dumps(cache_object), 1), 3600)
zipped_cache_object = cache.get(cache_key)
if zipped_cache_object:
    cache_object = cPickle.loads(zlib.decompress(zipped_cache_object))

Django's Caching Docs