In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "introduction and specific use of Django project cache optimization", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Django project cache optimization introduction and specific use" bar!
First, why use caching
You can think about the request response process of Django:
→ user browser enter URL address
→ Web server forwards HTTP request to uWSGI server
→ uWSGI server forwards Request requests to Django applications
→ Django middleware handles Request requests
→ View View processing
→ model class Models acquires data
→ template Template rendering
→ returns again through Django middleware
→ uWSGI server returns Response to Web server
The → Web server responds to the client's HTTP request.
The two most time-consuming links are usually business logic processing in the view and getting data from Models (SQL query). For requests for the same purpose, that is, requests for the same business processing logic and SQL query, repeated calculations are performed each time, and the data is read from the hard disk rather than memory.
So using caching has the following benefits:
Reduce server load
Avoid double counting
Improve system performanc
Second, how to cache
Very simply, when a Request request comes, it will be queried in the cache first, and then returned; if not, it will be queried in the database and processed, and then the results will be cached (for the next request) before returning. If you interpret it in pseudo code, that's it.
Type of cache 1. Memcached is the most efficient and fastest cache 2. Database caching database cache, which refers to caching data to data tables, such as the MySQL used in your project, to create tables in MySQL to specifically cache data. Does not mean to use Redis database 3. Filesystem caching file system cache, the cache will store key values in separate files 4. Local-memory caching uses system memory cache 5. Dummy caching (for development) fake cache, only used in the development process to debug the cache interface, data is not really cached 6. Using a custom cache backend custom cache back-end, such as using Redis
So the question is, what kind of cache should I use?
In fact, there are only two commonly used: Memcached or Reids, the other basically do not have to consider. Redis is widely used in China. It supports two persistence methods: RDB and AOF, and supports highly available clusters. The technology and solution are very mature. Memcached is pure memory storage, does not support persistence, does not support distribution, but it has high memory management efficiency.
Fourth, cache granularity classification
Per-site cache caches the whole website. As long as you set the configuration of adding the cache middle key in the project, the system will cache the whole site automatically.
Per-view cache caches a view
Fragments of Template fragment caching templates, such as Django templates that inherit base.html, are usually the same navigation bar
Low-level cache API low-level cache API, such as caching the results of a function, or an API interface
So the question comes again, what cache granularity should I choose in the project?
Whether you need caching or not is simple, depending on whether the content changes. If the data for the entire view is usually unchanged, view caching is used, and template fragment caching is used for some template fragments, etc. So there are multiple cache granularities in a project.
5. Django cache settings
Here we only talk about Redis and Memcached, and the other seldom used ones are not listed. It is important to note that the corresponding caching service should be installed on the system, and the package of connection caching service (such as django-redis or pymemcache) should be installed in the Django development environment.
Memcache cache settings
The simplest thing is to configure addresses and ports, not necessarily on the local machine, but also on servers or clusters on other network segments.
Unix Socket can also be used on this machine.
You can also use polymorphic servers, caching on different ports
Redis cache settings
Take a screenshot of the configuration of Redis cache in the practical course and cache the data of the website.
Caching of Django channels Channel layer
Celery cache, broker, and task execution results
VI. Use of caches
For the cached data of the Django project, we do not need to directly manipulate the underlying cached data, for example, using native Redis or Memcached commands, we only need to use the cached API provided by Django. Just as we use Django ORM, there is no need to care that the underlying database is MySQL, PostgreSQL or SQLite,ORM statements are the same.
For example, access to the cache
Use cach
In addition to set (), get () also has get_or_set (), get_many (), set_many (), delete (), delete_many (), clear (), touch (), incr (), decr (), close () operations. You can take a look at the official website documents one by one under the operation, it should be noted that some API is not available in every Django version, for example, cache.touch () is only available in Django 2.1.
VII. Performance evaluation of Django cache optimization
If the cache is used, what is the effect? what indicators can be measured? What tool is used to measure it?
Django Debug Toolbar
This has been mentioned to you in previous Python learning tutorials!
Django-debug-toolbar is an open source tool that can display the details of django's processing of request/response on Kanban, such as CPU time consuming in the current request response, settings/headers/request information, template files used in the current request, static files, specific SQL statements and execution time, etc. Kanban can be flexibly configured to show what information.
The column on the right is django-debug-tool 's Kanban.
There are 9 SQL queries on this page, which takes 8.62ms.
Cache hit 3 times, cache.get (), it takes about 0.91ms
Thank you for your reading, the above is the content of "introduction and specific use of Django project cache optimization". After the study of this article, I believe you have a deeper understanding of the introduction and specific use of Django project cache optimization, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 249
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.