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'))             #读取键值
相关推荐
whinc1 小时前
Rust技术周刊 2026年第17周
后端·rust
whinc1 小时前
Rust技术周刊 2026年第18周
后端·rust
whinc1 小时前
Rust技术周刊 2026年第16周
后端·rust
jieyucx1 小时前
Go语言深度解剖:Map扩容机制全解析(增量扩容+等量扩容+渐进式迁移)
开发语言·后端·golang·map·扩容策略
YJlio2 小时前
7.4.5 Windows 11 企业网络连接与网络重置实战:远程访问、本地策略与故障恢复
前端·chrome·windows·python·edge·机器人·django
深耕AI2 小时前
【VS Code避坑指南】点击Python图标提示“没有Python环境”,选择安装uv后这堆输出到底是什么意思?
开发语言·python·uv
第一程序员2 小时前
Rust生命周期管理实战指南:从困惑到掌握
python·github
王码码20352 小时前
Go语言的内存管理:原理与实战
后端·golang·go·接口
程序员威哥2 小时前
实战!Python爬京东商品评论:从采集到情感分析+词云可视化,新手30分钟跑通
开发语言·爬虫·python·scrapy
Lee川2 小时前
打字机是怎么炼成的:Chat 流式输出深度解析
前端·后端·面试