The best use of Django cache memcached

Source: Internet
Author: User
Tags memcached

Well, cough cough, this is on the win platform. The Linux platform is not tossing

First you have to install Memcache on win

Download: Click I download there are x64 and x86. Choose to use according to your own system version

Using the command line: 1, under File directory memcached-d install 2, start service (default port is 11211) net start "memcached Server" (or services.msc into service, find Memcach Ed service, on Can) for Django Plus cache A, install plugins

b, configuring the cache in setting
# settings.py中的配置CACHES = {    ‘default‘: {        ‘BACKEND‘: ‘django.core.cache.backends.memcached.MemcachedCache‘,        ‘LOCATION‘: [ ‘127.0.0.1:11211‘, ] }}



c, called in views.py (60*15 indicates that the cache time for this method is 15 minutes. Yes, don't forget to import.)

Of course, the above added a decorative caching method and can be implemented using the following method (directly in the route)

If you don't want to cache a method (page) and just want to cache a field, you can use the

Of course the needle for this method above, choose the value you want to cache is very correct. Because Memcache is the Key-value database. objects, such as database query result objects, cannot be stored.

If you want to cache objects, it's not possible to use Mongodb,redis, because Redis is also a key-value database.

The principle of caching:
When it turns out not to be:
1,django view get data from DB via models
2, then call the template to render, to form a real httpresponse
3. Write the data back to the client via an HTTP socket
  
With the Memcache:
The above is saved, and the saved HttpResponse are fetched directly from the cache pool based on the URL and returned to the client

Well, look at the effect (this is the way to demonstrate using adorners)

Re-select a city

The result, still unchanged, still in Hangzhou (the cache is effective, but it seems not suitable for me, because the site is a job platform, can not choose the city is a big problem, haha. This is just demo use)

The best use of Django cache memcached

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.