settings配置
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://192.168.188.128:6379/1',
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100}
},
},
}
LOCATION 后的/1 是切换redis库,默认为0
环境安装
pip install django-redis
redis测试
from django_redis import get_redis_connection
redis_conn = get_redis_connection()
class testRedis(View): # String类型
def get(self, request):
redis_conn.set('yy','123123') # rediss_conn.get('yy')
return JsonResponse({"data":"200"})
class testRedisList(View): # list类型
def get(self, request):
redis_conn.lpush('yy','123123') # redis_conn.lrange(sql, start_page, start_page + size-1)
return JsonResponse({"data":"200"})