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 小时前
HarmonyOS ArkUI Search 搜索框:联想词、历史记录与热门标签
后端
Reart2 小时前
Leetcode 714.买卖股票的最佳时机含手续费(719)
后端·算法
cui_ruicheng3 小时前
Python从入门到实战(十三):模块、包与环境管理
开发语言·python
芒鸽3 小时前
HarmonyOS ArkUI RichEditor 富文本编辑器:内联样式、Span 管理与格式工具栏
后端
花开彼岸天~3 小时前
Router 路由管理:pushUrl、replaceUrl、back 与参数传递
后端
李宸净3 小时前
Web自动化测试selenium+python
前端·python·selenium
而后笑面对3 小时前
生信bwa算法
python
wear工程师3 小时前
CompletableFuture 的 allOf 到底怎么收结果?面试别只说并行
java·后端
b130538100493 小时前
DiaryDetail2Page 卡片式详情:琥珀渐变 + 引号装饰
后端
w139548564224 小时前
AI 周报卡片:智能生成内容的 UI 呈现
后端