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'))             #读取键值
相关推荐
一定要AK1 天前
SpringBoot 教程 IDEA 版
spring boot·后端·intellij-idea
小白学大数据1 天前
告别复杂 XPath:DeepSeek+Python 爬虫快速实践
开发语言·爬虫·python·selenium
weixin_408099671 天前
【保姆级教程】按键精灵调用 OCR 文字识别 API(从0到1完整实战 + 可运行脚本)
java·前端·人工智能·后端·ocr·api·按键精灵
AI_Claude_code1 天前
ZLibrary访问困境方案六:自建RSS/Calibre内容同步服务器的完整指南
运维·服务器·网络·爬虫·python·tcp/ip·http
weixin_462022351 天前
Dancing under the stars: video denoising in starlight
python·计算机视觉
kishu_iOS&AI1 天前
机器学习 —— 线性回归(2)
人工智能·python·算法·机器学习·线性回归
Traving Yu1 天前
Spring源码与框架原理
java·后端·spring
网上邻居YY1 天前
深度学习DL 之 安装PyTorch·GPU版、CUDA(本人Anaconda、Python、PyCharm已提前安装好)
pytorch·经验分享·python·深度学习·pycharm·学习方法
AI、少年郎1 天前
如何用个人电脑快速训练自己的语言模型?MiniMind 全流程实战指南
人工智能·python·神经网络·ai·自然语言处理·大模型·模型训练微调
王家视频教程图书馆1 天前
rust 写gui 程序 最流行的是哪个
开发语言·后端·rust