settings.py代码配置如下所示:
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': [
"redis://10.10.27.222:6380",
"redis://10.10.27.222:6381",
"redis://10.10.27.222:6382",
"redis://10.10.27.222:6383",
"redis://10.10.27.222:6384",
"redis://10.10.27.222:6385",
],
'OPTIONS': {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100},
"PASSWORD": "thisisacode",
#"DECODE_RESPONSES": True
}
}
}
views.py代码如下所示:
from django.http import HttpResponse,JsonResponse
from django_redis import get_redis_connection
#插入更新数据
def rd(request):
"""redis连接"""
con = get_redis_connection("default")
rst = con.set('musicye', 'value666', 10 * 60) # 2 分钟超时
return JsonResponse({'res': rst})
#获取数据
def get_rd_data(request):
"""redis连接"""
con = get_redis_connection("default")
rd_data = con.get('musicye')
#return JsonResponse({'res': rd_data})
#HttpResponse(result)
return HttpResponse(rd_data)
urls.py代码如下所示:
from django.contrib import admin
from django.urls import path,re_path,include
from . import views
#正则匹配一会查
#1.严格匹配开头和结尾
urlpatterns = [
#通过url函数设置url路由配置项
re_path('^rd/$', views.rd), # redis插入更新
re_path('^get_rd_data/$', views.get_rd_data), # redis获取数据
]