创建utils文件
from redis.client import StrictRedis
redis = StrictRedis(host='localhost',port=6379,db=0,password='')
在view视图中操作redsi数据库
from myapp.utils import redis
from django.http import HttpResponse
class Test_redis(View):
'''
测试redis存储及过期时间
'''
def get(self, request):
# ex=10 过期时间,单位秒
redis.set('nihao', 'haohaohao', ex=10)
return HttpResponse('ok')
class Test_redsi_ex(View):
'''
测试redsi的过期时间
'''
def get(self, request):
a = redis.get('nihao')
return HttpResponse(a) # 在10秒内可以获取到值,10秒过后获取到None