2

I have my existing Django web application that uses a MySQLDB without memcaching. I would like to implement memcaching to improve the responsiveness of this site. I see the instructions here.

However, these instructions leave me with some unanswered question(s). Is this all I need to do to get memcache working after I setup the memcached server? Or do I need to alter any of my code outside of settings.py? Does Django nicely handle all the memcaching operations behind the scenes for me whenever models are read or written? (If so, that's very cool!) How can I see what improvement the memcaching is having on the number of DB accesses?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272

1 Answers1

3

What you've done is just a set up of a Cache Backend.

In order to benefit from caching you need to find the places where it is appropriate and would have a positive impact on performance: your views, templates..you can cache the whole views, templates, template fragments etc.

If you want some automation to help you, take a look at Johnny Cache package:

Johnny Cache is a caching framework for django applications. It works with the django caching abstraction, but was developed specifically with the use of memcached in mind. Its main feature is a patch on Django’s ORM that automatically caches all reads in a consistent manner.

Or django-cache-machine package:

Cache Machine provides automatic caching and invalidation for Django models through the ORM.

There is also an interesting project called django-cacheops that is aiming to improve Django ORM caching, but it uses Redis backend.

Also, django_debug_toolbars caching panel can help you in the future.

Note that django querysets have a built-in internal cache, but it has nothing to do with a cache framework.

Further reading:

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I have also added the following to my MIDDLEWARE_CLASSES: 'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.cache.FetchFromCacheMiddleware'. The docs seem to suggest this will "cache my entire site". What does that mean? Isn't that what I'm after? – Saqib Ali Jun 01 '14 at 05:59
  • @SaqibAli this is about request/response. If the request headers allow to get the response from cache and if there was a request for the same URL with the same query parameters - django would retrieve the response from cache. Just read the whole chapter from the link you've provided. Hope that helps. – alecxe Jun 01 '14 at 06:06
  • @SaqibAli did the answer help? – alecxe Jun 05 '14 at 20:03
  • yes. It was helpful. But now my question has become more detailed/complex.. I posted a new one here: http://stackoverflow.com/questions/24071619/how-to-use-memcache-with-django-when-retrieving-updating-model-instances-with – Saqib Ali Jun 05 '14 at 23:08