1. Description
Redis, as a cache database, plays a great role in all aspects. Python supports operating redis. If you use Django, there is a redis library specially designed for Django, namely django- redis
2. Installation
pip install django-redis
Copy after login
3. Configuration
3.1 Configure redis
Open the Django configuration file, such as setting.py, and set CACHES in it Item
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
Copy after login
Multiple redis connection information can be configured in one CACHES. Each one has its own alias (alias). The "default" above is the alias. At that time, you can connect to different redis databases through different aliases
LOCATION is the connection information, including ip port user password, etc. If the user password is not required, you can omit it. django-redis supports three connection protocols, as follows
Protocol | Description | Example |
##redis:// | Ordinary TCP suite Interface connection | redis://[[username]:[password]]@localhost:6379/0 |
rediss | SSL mode TCP socket connection | rediss://[[username]:[password]]@localhost:6379/0 |
rediss:// | Unix domain socket connection | unix://[[username]:[password]]@/path/to/socket.sock?db=0 |
3.2 Use redis to store session
Django’s default Session is stored in the sql database, but we all know that ordinary data will be stored on the hard disk, and the speed is not that fast. If you want to To change it to be stored in redis, you only need to configure it in the configuration file
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"
Copy after login
3.3 redis connection timeout setting
The number of seconds for the connection timeout can be specified in the configuration item, SOCKET_CONNECT_TIMEOUT indicates the connection The timeout of redis, SOCKET_TIMEOUT represents the timeout of read and write operations using redis
CACHES = {
"default": {
# ...
"OPTIONS": {
"SOCKET_CONNECT_TIMEOUT": 5, # 连接redis超时时间,单位为秒
"SOCKET_TIMEOUT": 5, # redis读写操作超时时间,单位为秒
}
}
}
Copy after login
4. Using redis
4.1 Use the default redis
If you want to use the default redis , that is, the redis with the alias "default" set in the configuration file can refer to the cache in django.core.cache
from django.core.cache import cache
cache.set("name", "冰冷的希望", timeout=None)
print(cache.get("name"))
Copy after login
4.2 Use the specified redis (native redis)
When you Multiple redis connections are written in the configuration file. You can specify which redis to use through alias
from django_redis import get_redis_connection
redis_conn = get_redis_connection("chain_info")
redis_conn.set("name", "icy_hope")
print(redis_conn.get("name"))
Copy after login
It should be noted that the client obtained through get_redis_connection() is a native Redis client, although basically all are supported Native redis command, but the data it returns is of byte type, you need to decode it yourself
5. Connection pool
The advantage of using the connection pool is that you don’t need to manage the connection object, it will automatically create some connections Objects and reused as much as possible, so the performance will be relatively better
5.1 Configuring the connection pool
To use the connection pool, first write the maximum connection pool size in the Django configuration file Number of connections
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
...
"OPTIONS": {
"CONNECTION_POOL_KWARGS": {"max_connections": 100}
}
}
}
Copy after login
5.2 Using connection pool
We can determine which redis to use through the connection alias, and then just execute the command normally. We don’t need to care about which connection instances it creates, but you can pass The _created_connections attribute of connection_pool checks how many connection instances are currently created
from django_redis import get_redis_connection
redis_conn = get_redis_connection("default")
redis_conn.set("name", "冰冷的希望")
print(redis_conn.get("name"))
# 查看目前已创建的连接数量
connection_pool = redis_conn.connection_pool
print(connection_pool._created_connections)
Copy after login
5.3 Custom connection pool
The default connection class of Django-redis is DefaultClient, if you have higher customization requirements , you can create a new class of your own and inherit ConnectionPool
from redis.connection import ConnectionPool
class MyPool(ConnectionPool):
pass
Copy after login
After you have this class, you need to specify it in the Django configuration file
"OPTIONS": {
"CONNECTION_POOL_CLASS": "XXX.XXX.MyPool",
}
Copy after login
The above is the detailed content of How to use django redis. For more information, please follow other related articles on the PHP Chinese website!