Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Stackorigin – The Community of Question and Answers

Stackorigin – The Community of Question and Answers Logo Stackorigin – The Community of Question and Answers Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
Home/How to use memcached in Django?

How to use memcached in Django?

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
  1. http://downloads.northscale.com/memcached-win32-1.4.4-14.zip
  2. 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.
Share
  • Facebook

Sidebar

Ask A Question

Adv 120x600

Looking for advertising?

Consider These Things If You Appreciate What stackorigin Does: Stackorigin is a community where you can ask questions and find answers, for free to everyone. We would appreciate it if you bought us a coffee if you liked what we were doing.

Buy me a Coffe

Adv 120x600

  • Random
  • Answers
    • On: February 12, 2023
    • Answers: 0

    What are serializers in Django

    • On: August 7, 2022
    • Answers: 0

    Careprost acquista, Does careprost change eye color

    • On: August 8, 2022
    • Answers: 0

    Order discount Rebose Italy, Buy generic rebose in

    • On: December 8, 2020
    • Answer: 1

    What is difference between Django values_list vs values?

    • On: September 4, 2022
    • Answer: 1

    How to Turn on the Backlit Keyboard on a Dell

  • strapcart_online
    strapcart_online added an answer Sildenafil citrate is a common ingredient in Cenforce 150mg tablets. This drug… March 21, 2023 at 11:42 am
  • strapcart_online
    strapcart_online added an answer Vilitra 20mg tablets are used to overcome the problem of… March 18, 2023 at 6:13 am
  • ashok
    ashok added an answer Not every girl, there are some girls out there who… March 16, 2023 at 4:15 am
  • strapcart_online
    strapcart_online added an answer Tadarise 40 Tablet is used to permanently remove the problem… March 4, 2023 at 5:48 am
  • strapcart_online
    strapcart_online added an answer Men use Cenforce D tablets for the treatment of sexual… March 1, 2023 at 5:59 am

Adv 120x600

Trending Categories

Health
2042Questions
, 4Followers
Programming
1236Questions
, 0Followers
Technology
216Questions
, 3Followers
General Knowledge
131Questions
, 0Followers
Business & Finance
84Questions
, 4Followers

Adv 120×600

Stats

  • Questions 4k
  • Answers 1k
  • Best Answers 105
  • Users 100

Users

niyadas823

niyadas823

  • 0 Questions
  • 0 Answers
nirala3

nirala3

  • 2 Questions
  • 0 Answers
Perrywalton

Perrywalton

  • 1 Question
  • 0 Answers
veeraa

veeraa

  • 0 Questions
  • 0 Answers
Stewesmitz

Stewesmitz

  • 0 Questions
  • 0 Answers

Adv 120×600

Explore

  • Recent Questions
  • Most Answered
  • Answers
  • No Answers
  • Most Visited
  • Most Voted
  • Random
  • Polls

Footer

Stackorigin - The Community of Question and Answers

Stackorigin

Stackorigin is the world’s largest Q&A networking site, Stackorigin community brings you the collaboration of all the various Questions and the related Answers given by the community.

About

  • About Us
  • Contact Us
  • FAQ
  • Submit Guest Post Article on Technology, Education, Health, Apps, Gadgets, IoT, AI, Business, Digital Marketing and More

Info

  • Privacy Policy
  • Terms and Conditions
  • Community Guidelines
  • Tags

Products

  • Tutorials
  • Advertising
  • Categories
  • Corona
  • StackHow

Follow

© 2022 Stackorigin. All Rights Reserved.