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'))             #读取键值
相关推荐
kfaino3 小时前
码农的AI翻身(五)你好,我叫 Transformer
后端·aigc
Oneslide9 小时前
机械革命 单系统纯净重装Ubuntu(全盘覆盖,清空原有Windows)
后端
GetcharZp9 小时前
告别OOM!用Go+libvips实现30000×50000超大图片的流式瓦片服务
后端·go
IT_陈寒9 小时前
JavaScript项目实战经验分享
前端·人工智能·后端
用户479492835691510 小时前
6w star,GitHub 趋势第一的 Ponytail,这个agent插件到底在火什么
前端·后端
神奇小汤圆11 小时前
2026一线大厂Java八股文精选(附答案,高质量整理)
后端
Warson_L11 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅11 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅11 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L11 小时前
LangGraph的MessageState and HumanMessage
python