Django模块连接redis

1 安装django-redis

bash 复制代码
pip3 install django-redis

2、配置settings.py

python 复制代码
CACHES={
    "default":{
        "BACKEND":"django_redis.cache.RedisCache",
        "LOCATION":"redis://"+redis_host+":"+redis_port,            #redis主机地址和端口,数据在local_settings中
        "OPTIONS":{
            "CLIENT_CLASS":"django_redis.client.DefaultClient",
            "CONNECTION_POOL_KWARGS":{
                "max_connections":1000,
                "encoding":"utf-8"
            },
            "PASSWORD":redis_password                                #redis密码,数据在local_settings中
        }
    }
}

3、连接redis

python 复制代码
conn=get_redis_connection('default')                  #这个default就是上一步中定义的redis
conn.set(phoneNum,code,ex=30)                         #设置键值
print(conn.get(phoneNum).decode('utf-8'))             #读取键值
相关推荐
程序员海军2 小时前
用了两年多 GPT,我最近开始换模型了,还折腾了个求职避坑 Agent
前端·人工智能·后端
码士集团小青2 小时前
SpringBoot 集成缓存性能之王 Caffeine
spring boot·后端·缓存
excel4 小时前
IndexNow:让搜索引擎更快发现网站更新的主动通知协议
前端·后端·架构
Json____4 小时前
用 FastAPI + Vue3 打造一套完整的在线考试系统:从组卷到阅卷的业务闭环实践
后端·node.js·fastapi·it学习·wwwoop.com
小灰灰搞电子5 小时前
Rust+Slint 实现FTP客户端调试助手源码分享
开发语言·后端·rust
外收内放5 小时前
面向对象高级(实战案例)
开发语言·python·学习
专业程序开发源6 小时前
springboot欧缇蔓月子中心管理系统82697-计算机课程设计、毕业设计
java·spring boot·后端·python·spring·django·课程设计
FPGA小迷弟6 小时前
用 Claude Code 重构遗留系统:从评估到落地的完整实践指南
c++·python·ai·plotly·django·ai编程·claude
卷无止境6 小时前
面向arm64桌面客户端开发全景:语言、框架与生态
后端·python
fanruitian6 小时前
python else用法
开发语言·python