Detailed explanation of Django's caching mechanism in Python web combat

e87fc3348cd6485abef0df87cb709ccb.png


Keywords: Python, web development, Django, caching


1. What is cache? Why is caching needed?

In web development, a cache is a temporary storage area used to store data. It improves the performance and responsiveness of the application and reduces the load on the server.

When a user visits a web page, the server performs a series of operations to generate dynamic content, and these operations may include database queries, complex calculations, and so on.

It would consume a lot of time and computing resources if these operations were re-performed every time the user requested them. By using the cache, these calculation results can be temporarily saved, and the data in the cache will be returned directly when the user requests the next time, thus saving time and resources.

1.1 Types and Application Scenarios of Caching

In Django, you can use several types of caches, including in-memory caches, database caches, file caches, and more. Different types of caching are suitable for different scenarios. Below, I will introduce several common cache types and their application scenarios.

1.1.1 Memory cache

Memory cache is to store data in memory, and the reading speed is very fast. It is suitable for data that is frequently read but does not need to be stored permanently, such as some dynamically generated page content, user login status, etc. In Django, you can use a third-party library  django-redis to implement memory caching. For details, please refer to the official documentation.

1.1.2 Database cache

Database cache is to store data in the database, which can realize the persistent storage of data. It is suitable for data that needs to be stored for a long time, such as user information, article content, etc. In Django, you can use the built-in database cache backend to implement database caching with simple configuration.

1.1.3 File cache

File caching stores data in the file system and is suitable for caching a large number of static files, such as images, CSS, JavaScript, etc. It can reduce server pressure and improve file reading speed. Django provides a built-in file caching backend, which is very convenient to use.

1.2 The basic principle of Django caching mechanism

The basic principle of Django's caching mechanism is actually very simple, that is, "store and reuse". Let's look at a code first:

from django.core.cache import cache

# 存储数据到缓存
cache.set('my_key', 'hello, world!', 30)

# 从缓存中获取数据
value = cache.get('my_key')

In the above code, cache is first imported from django.core.cache. Then use the cache.set method to store a key-value pair into the cache, where 'my_key' is the key, 'hello, world!' is the value, and 30 is the validity period of the cache (unit: second). Then use the cache.get method to get the corresponding value from the cache through the key.

1.3 Application Scenarios of Django Caching

In Django Web development, caching can be applied in many scenarios, such as:

  • Cache the results of querying the database to reduce the query pressure on the database

  • Caches dynamically generated HTML pages to improve page load speed

  • Cache the calculation results to reduce the time of repeated calculations

2. Cache configuration and use of Django

Now let's see how to configure and use caching in Django.

2.1 Cache configuration

2.1.1 Memcached cache configuration

In the Django configuration file, we can  CACHES configure the cache backend by setting items. The following is the configuration of memcached:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    }
}

The above configuration uses Memcached as the cache backend, and the address is local  127.0.0.1:11211.
BACKEND is the cache backend we chose, LOCATION and is the address and port of the Memcached server. You can choose different cache backends according to your needs and configure them accordingly.

2.1.2 Redis cache configuration

  • Install the Redis client library for Python:

pip install redis
  • settings.pyConfigure cache settings in your Django project's file:

CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': 'redis://localhost:6379/0',  # Redis服务器的连接地址
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
        }
    }
}

The parameter here LOCATIONspecifies the connection address of the Redis server. You can modify it to the address and port number of your Redis server according to the actual situation.

  • Configure Redis as session storage backend (optional):

If you want to use Redis as a storage backend for Django sessions, settings.pyadd the following to the file:

SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
SESSION_CACHE_ALIAS = 'default'

This way Django will use the Redis cache as the session storage backend.

  • Run the migration command:

After configuring the Redis cache, run the following command to apply the changes to the cache settings:

python manage.py migrate

2.2 Cache usage

 In Django, you can cache the output of view functions by using decorators  @cache_page or helper functions  . cache_pageFor example

from django.views.decorators.cache import cache_page

@cache_page(60 * 15)  # 缓存 15 分钟
def my_view(request):
    # 处理视图函数的逻辑
    return HttpResponse('Hello, World!')

The above code uses  cache_page a decorator to  my_view cache the output of the view function, and the cache time is 15 minutes. When the user accesses the view function, if the cache is valid, the cached result will be returned directly, otherwise the logic of the view function will be executed and the cache will be updated.

In addition to basic storage and retrieval of data, Django also provides some other methods that can help us use the cache better. For example:

  • Use  cache.add the method to add data to the cache when the key does not exist

  • Use  cache.get_or_set the method to add data to the cache when the key does not exist, and return a value

  • cache.delete A key can be removed from the cache using  the method

  • Use  cache.clear the method to clear all caches

Here are a few code snippets for how to use them:

  • 1. Use cache.addthe method to add data to the cache:

from django.core.cache import cache

def add_data_to_cache(key, value):
    # 将数据添加到缓存中,如果键已存在,则不进行任何操作
    cache.add(key, value)
  • 2. Use cache.get_or_setthe method to add data to the cache and return a value:

from django.core.cache import cache

def get_or_set_data_from_cache(key, default_value):
    # 尝试从缓存中获取键的值,如果不存在,则将默认值设置到缓存,并返回默认值
    value = cache.get(key)
    if value is None:
        value = default_value
        cache.set(key, value)
    return value
  • 3. cache.deleteDelete a key from the cache using the method:

from django.core.cache import cache

def delete_data_from_cache(key):
    # 从缓存中删除指定的键
    cache.delete(key)
  • 4. Use cache.clearthe method to clear all caches:

from django.core.cache import cache

def clear_cache():
    # 清空所有缓存
    cache.clear()

3. Caching precautions and optimization techniques

There are some things to be aware of when using caching, and some optimization techniques to apply to improve the effectiveness and performance of caching.

3.1 Cache Granularity Control

In the cache design, it is necessary to reasonably control the granularity of the cache. If the cache granularity is too large, it may lead to a low cache hit rate and waste storage space; if the cache granularity is too small, it may cause frequent cache failures and increase server load. Therefore, it is necessary to reasonably divide the cache granularity according to specific business scenarios and data characteristics.

3.2 Cache update strategy

When data changes, we need to update the cache in time to maintain data consistency. In Django, you can use semaphores  post_save, pre_delete etc. to listen for model changes and automatically update the cache when changes occur. In addition, you can manually update the cache to select an appropriate cache update strategy according to specific business needs.

3.3 Cache invalidation processing

The cache has a certain life cycle. When the cache expires or is deleted, a corresponding processing mechanism is required. You can use  cache.get methods to get cached values ​​and determine whether the cache is valid. If the cache fails, we can regenerate the data and update the cache to ensure the timeliness and accuracy of the data.

4. Technical summary

This article explains the caching mechanism of Django, and introduces in detail the common types of caching in Django and their application scenarios, configuration and use, as well as precautions and optimization techniques about caching. Friends who are interested can like, favorite, follow and forward, thank you.

Guess you like

Origin blog.csdn.net/Rocky006/article/details/132201244