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'))             #读取键值
相关推荐
To_OC3 小时前
Next.js + Redis 构建笔记系统:Redis Hash 实战与性能优化
redis·后端·next.js
安_4 小时前
如何构建和使用向量索引?HNSW 和 IVF 有什么区别?
python·ai
counting money5 小时前
Java IO流详解:从InputStream到文件操作实战
java·开发语言·python
坚持学习前端日记6 小时前
Python SQLAlchemy ORM 从0到1精通实战手册(基础到复杂高阶)
数据库·python·oracle
北斗落凡尘6 小时前
LangGraph 入门实战(11)--输出模式
后端·python·langchain
雪碧聊技术7 小时前
安装Python(保姆级教程)
python·版本更新·python下载
++==7 小时前
JSON和Python的 四种核心容器
python·json
demo007x7 小时前
Hermes-Agent 技术架构
前端·后端·agent
__zRainy__9 小时前
Node系列 · Node基础:net 模块
后端·node.js