-->

What steps are needed to implement memcached in a

2019-08-10 16:14发布

问题:

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?

回答1:

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:

  • Using Django querysets effectively
  • Caching and QuerySets