Why use caching?
The basic tradeoff of a dynamic website is that it is dynamic. Every time a user requests a page, the server recalculates it. From an overhead processing standpoint, this is much more expensive than if you were to read an off-the-shelf standard file
Use caching to cache data that is essentially the same when accessed by multiple users first; this way, when a user accesses the page, instead of recalculating the data, the data is read directly from the cache, avoiding performance overhead.
Using the Redis database
Using redis database storage cache, first of all, redis is a key-value type database, NoSQL, and is also a memory type database, redis is loaded into the memory, operation, and asynchronous data backup to the hard disk. And we know that the read speed of memory is faster than the read speed of the hard disk, so the read speed of Redis is much faster than other file-based databases.
Configuring django's cache settings
Add.
CACHES = { "default": { "BACKEND": "django_redis.", "LOCATION": "redis://172.16.3.241:6379/9", //points to the IP address of the server with redis:port /9 means use db9 "OPTIONS": { "CLIENT_CLASS": "django_redis.", } } }
Using Django's built-in caching API (cache)
from import cache
(for) instance
In the view class or view function, first of all, do not rush to calculate the page data; but first to the cache to read the data of the page; if it returns a None; that is, there is no cache or the cached data has expired; at this point, you need to carry out a database query and other calculation services
And write the updated data to the cache, from the beginning to the end of the same page are unified using a key to access or delete;
# Cache setup and fetching pseudocode: content= (‘index_data') if content is None: Query data, etc. (‘index_data',content,timeout) # Setting up the cache # Render page
The cache sets an expiration timeout, in seconds, when the expiration time is reached, the cached data will not be used, so as to ensure that the cached data will not always be the same copy.
But if I set the timeout to 3600, i.e. 60 minutes, then within 60 minutes, if the data of the web page is updated, and the user accesses it, only the old page data will be displayed, so in order to ensure the timeliness of the cache.
You can delete the old cache when the data is changed.
For example, in django's own admin page, the save_model or delete_model method is called by default when a model is modified. So, inherit and override that method and add the
Delete the cached code, then the old page cache will be cleared when the webmaster modifies the data through the self-administration page.
E.g..
class BaseModel(): ''' Inheritance Override save_model / delete_model methods ''' def save_model(self, request, obj, form, change): super().save_model(request,obj,form,change) # Delete homepage cache ('index_data') # Remove caches for type and new product recommendations ('types_and_newsku') def delete_model(self, request, obj): super().delete_model(request,obj) #Delete Home Page Cache ('index_data') # Remove caches for type and new product recommendations ('types_and_newsku')
This is the whole content of this article.