一 .redis在django中使用
1. 方式一(不推荐使用通过连接池)
redis_pool.py import redis Pool= redis.ConnectionPool(host=‘localhost‘, port=6379)
urls.py from django.contrib import admin from django.urls import path from myapp import views urlpatterns = [ path(‘admin/‘, admin.site.urls), path(‘index/‘, views.index), path(‘home/‘, views.home), ]
views.py from django.shortcuts import render,HttpResponse import redis from uits.redis_pool import # Pool 引入连接池 def index(request): conn = redis.Redis(connection_pool=Pool) conn.hset("k1","age",18) return HttpResponse("这是设置值") def home(request): conn = redis.Redis(connection_pool=Pool) aa=conn.hget("k1","age") print(aa) return HttpResponse("获取值")
原文地址:https://www.cnblogs.com/lovershowtime/p/11717032.html
时间: 2024-10-01 12:58:22