What is a cache?
cache is a reserved storage location that collects temporary data to help websites, browsers, and apps load faster.
Why Memcached?
Memcached is a free, open-source, high-performance, distributed memory object caching system. Memcached is used to speed up dynamic web applications by reducing the database load.
Integration Django with Memcache:
- In your Django project, under settings.py, add the following code in the bottom of the file.
SESSIONS_ENGINE='django.contrib.sessions.backends.cache'
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
- Install memcached client for Python with your virtual environment active.
pip install python-memcached
- Download Memcached using one of the following download links and extract it to a particular folder
- http://downloads.northscale.com/memcached-win32-1.4.4-14.zip
- http://downloads.northscale.com/memcached-win64-1.4.4-14.zip
- Open Memcached folder in the terminal, then Run the following command (-m is for the amount of memory you want to dedicate and -vvv is for extreme verbose).
.\memcached.exe -m 512 -vvv

- Lets implement cache in Django application in views.py
from django.core.cache import cache
from .models import school
def my_view(request):
cache_key = 'stack' # needs to be unique
cache_time = 60 # time in seconds for cache to be valid
data = cache.get(cache_key) # returns None if no key-value pair
if not data:
data = school.objects.all()
cache.set(cache_key, data, cache_time)
return JsonResponse(data, safe=False)
- start the server and you should get much better results.