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
pip install django-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", } } }
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 |
---|---|---|
Ordinary TCP suite Interface connection | redis://[[username]:[password]]@localhost:6379/0 | |
SSL mode TCP socket connection | rediss://[[username]:[password]]@localhost:6379/0 | |
Unix domain socket connection | unix://[[username]:[password]]@/path/to/socket.sock?db=0 |
SESSION_ENGINE = "django.contrib.sessions.backends.cache" SESSION_CACHE_ALIAS = "default"3.3 redis connection timeout settingThe 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读写操作超时时间,单位为秒 } } }4. Using redis4.1 Use the default redisIf 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"))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"))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 yourself5. Connection poolThe 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 better5.1 Configuring the connection poolTo 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} } } }5.2 Using connection poolWe 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)5.3 Custom connection poolThe 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): passAfter you have this class, you need to specify it in the Django configuration file
"OPTIONS": { "CONNECTION_POOL_CLASS": "XXX.XXX.MyPool", }
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!