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