1.下载django-redis模块
pip install django-redis
2.在配置文件配置
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100}
}
}
}
3.新建py文件:redis_pool.py
import redis
POOL=redis.ConnectionPool(max_connections=10)
4.使用方式
在使用的位置导入后直接使用
from django_redis import get_redis_connection
class MyResponseView(APIView):
def get(self, request):
conn = get_redis_connection()
# 从连接池中拿出一个链接
conn.incrby('a1')
conn.set('name','彭于晏')
return APIResponse()