Reputation: 4435
So: I always deploy my Django apps with memcached running alongside. For my purposes, they're always running on the same instance. I'm enabling the cache with these lines in settings.py
:
CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
CACHE_MIDDLEWARE_SECONDS = 60
CACHE_MIDDLEWARE_KEY_PREFIX = "the_name_of_the_app"
SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"
... I can clearly see the performance benefits when memcache is running. But this is the most simplistic of caching strategies, according to the django docs on the subject -- where should I go from here to get more performance? Does one have to manually festoon one's models or querysets with memcache accessors, or is there a strategically better way?
I like memorized and have had some luck with it -- but caching in django remains a black box to me. Please show me the way forward, if it's a path you're familiar with.
Upvotes: 2
Views: 375
Reputation: 8158
Memcache isn't going to magically make your website faster. The absolute first thing you are going to need to do is figuring out what is expensive. A simple way to do this is to decorate your views or other functions to see how long they generally take to execute, for example:
def print_latency(f):
def wrapped(*args, **kwargs):
try:
start = time.time()
r = f(*args, **kwargs)
finally:
print >>sys.stderr, 'Latency (%s): %.3fs' % (f.__name__, time.time() - start)
return r
wrapped.__name__ = f.__name__
return wrapped
@print_latency
def my_view(request):
...
The next thing to do is figure out what resources are cacheable, i.e., what data doesn't change or doesn't cause a horrible experience if it does? Then start at the top. Can you cache the templates? If not, can you cache the views? If not, can you cache the database calls? The latter is probably where you will end up unless you have a super-simple website.
Then you'll need to see if your calls to the db are very cacheable. You might need to break up complex queries into simpler ones.
Caching isn't magic and isn't perfect. You may have worse latency with caching but better throughput. Or on average latency might be better but the worst latencies might be horrible.
Upvotes: 2