PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 12, 2022

[FIXED] How to use memcached in Django?

 November 12, 2022     django, django-1.11, memcached, python, python-2.7     No comments   

Issue

I've seen all over problems using Memcached in Django projects which is considered to be

The fastest, most efficient type of cache supported natively by Django

For instances,

  • Why doesn't memcache work in my Django?
  • How to configure Memcache for Django on Google cloud AppEngine?
  • Django doesn't use memcached framework
  • memcache on django is not working
  • How to use memcached in django project?
  • How do you confirm django is using memcached?
  • Configuring memcached with django
  • What steps are needed to implement memcached in a Django application?
  • How to use memcached in django project?

So, how can we then use it?


Solution

This answer explains how to install Memcached on Windows 10 and how to integrate it with Django through a specific client. It was validated using Memcached 1.4.4, Python 2.7 and Django 1.11.

  1. 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',
        }
    }
    
  2. Install memcached client for Python with your virtual environment active (python-memcached)

    pip install python-memcached
    
  3. 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

This is the memcached folder in Windows

  1. Open that folder location in the Terminal or PowerShell and run

    .\memcached.exe -h
    

you should get something like this

memcached -help command

  1. 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
    

Memcache working fine

  1. In the view you want to use cache, specify it in urls.py like

    from django.conf.urls import include, url
    from django.views.decorators.cache import cache_page
    
    from .views import IndexView
    
    urlpatterns = [
        url(r'^$', cache_page(60*60)(IndexView.as_view()), name="index"),
    ]
    
  2. Go to the Django project, start the server and you should get much better results in your Time load.

Improve site performance with Memcached



Answered By - Tiago Martins Peres
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,213,758

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing