首页 技术 正文
技术 2022年11月10日
0 收藏 802 点赞 2,133 浏览 1326 个字

django-redis 基于 BSD 许可, 是一个使 Django 支持 Redis cache/session 后端的全功能组件.

1,安装 django-redis 最简单的方法就是用 pip :

pip install django-redis

2,作为 cache backend 使用配置:

为了使用 django-redis , 你应该将你的 django cache setting 改成这样:

 CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}

3  作为 session backend 使用配置

django-redis  使用规范

Django 默认可以使用任何 cache backend 作为 session backend, 将 django-redis 作为 session 储存后端不用安装任何额外的 backend

 SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"

4,使用 django-redis 进行测试

from django_redis import get_redis_connectionredies_conn = get_redis_connection('verify_codes')  #这里的名字是setting 里的配置的name
添加:
redis_conn.setex(key,timeout,value) #键 过期时间(s) 值
取值:
redis_conn.setex.get('key')
删除:
redies_conn.delete('key')

Django-redis管道的使用

 from django_redis import get_redis_connection
#创建redis连接对象
#说明:verify_codes表示settings文件中的redis配置的选择
redis_conn = get_redis_connection('verify_codes')
#管道的使用
pl = redis_conn.pipeline()
pl.setex("sms_%s" % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)
pl.setex('send_flag_%s' % mobile, constants.SEND_SMS_CODE_INTERVAL, 1)
#传递指令, 写入redis
pl.execute()

Redis 管道技术:

  • Redis是一种基于客户端-服务端模型以及请求/响应协议的TCP服务。这意味着通常情况下一个请求会遵循以下步骤:
  • 客户端向服务端发送一个查询请求,并监听Socket返回,通常是以阻塞模式,等待服务端响应。
  • 服务端处理命令,并将结果返回给客户端。
  • Redis 管道技术可以在服务端未响应时,客户端可以继续向服务端发送请求,并最终一次性读取所有服务端的响应。
  • 管道技术最显著的优势是提高了 redis 服务的性能。
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290