您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

redis数据库与python交互

bubuko 2022/1/25 19:21:26 python 字数 1298 阅读 908 来源 http://www.bubuko.com/infolist-5-1.html

一、安装包 pip3 install redis 二、调用模块 from redis import * sr = StrictRedis(host='localhost', port=6379, db=0) sr = StrictRedis() # 简写 三、使用StrictRedis对象操作str ...

一、安装包

pip3 install redis

二、调用模块

from redis import *  

sr = StrictRedis(host=‘localhost‘, port=6379, db=0) 

sr = StrictRedis()   # 简写

三、使用StrictRedis对象操作string类型

from redis import *

if __name__ == ‘__main__‘:
      try:
            sr = StrictRedis()
            # 添加一个key为name,value为hao
            res = sr.set(‘name‘, ‘hao‘)   # res为bool,添加是否成功
      except Exception as e:
            print(e)

四、使用redis存储session

  • 安装redis-session
pip3 install django-redis-sessions
  • 修改django项目settings文件,增加如下选项
# 设置redis存储session信息
SESSION_ENGINE = ‘redis_session.session‘
# redis服务的ip地址
SESSION_REDIS_HOST = ‘localhost‘
# 端口
SESSION_REDIS_PORT = 6379
# redis对应数据库
SESSION_REDIS_DB = 2
SESSION_REDIS_PASSWORD = ‘‘
SESSION_REDIS_PREFIX = ‘session‘

redis数据库与python交互

原文:https://www.cnblogs.com/haoshch/p/14053693.html


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶