zoukankan      html  css  js  c++  java
  • python操作redis

    redis数据都存放在内存里支持30W/s级别的读写操作

    操作redis步骤:

    1、建立链接

    2、操作

    3、关闭链接(可以忽略)

    import redis  #需要提前pip安装redis包
    ip = 'any_ip'
    port ='6379'
    password = '123456'
    db =0
    r = redis.Redis(host=ip,password=password,port=port,db=db)
    ##连接redir
    r.set('hhh','haoren') #插入string类型的数据
    r.set('users:shuaige','zhengren') #插入文件夹

    print(r.keys())#获取所有的key值,list列表
    print(r.get('hhh').decode())  #get如果不存在就返回None,redis获取到的都是bytes类型,bytes类型有decode方法转成字符串才能继续操作
    r.setex('qingdao','shandong',100)#可以设置失效时间
    r.mset(lining='lining',nike='nike')批量设置set值

    print(r.keys())#获取所有的key值,list列表
    #下面是hash类型
    r.hset(info:'user_session','duguanglong','jjjjjjjj')
    r.hget('yyy:user_session','duguanglong')#获取指定的name里面的key值
    r.hgetall('yyy:user_session')获取指定名下所有的值,#Return a Python dict of the hash's name/value pairs"
    r.delete('key') 删除指定key值
    r.hdel('user_session','duguanglong')

    下面
    import redis
    def op_Redis(host,password,k,v=False,port=6379,db=0)
      r = redis.Redis(host=host,password=password,port=port,db=0)
      if v: #判断 value是不是空
        r.set(k,v)
        res = 88
      else:
        res = r.get(k).decode()
      return res
    if __name__ = '__main__':
      s = op_Redis('any_ip','123456','ttt:houyanfan')
    print(s)

      

  • 相关阅读:
    西门子SCL读写DB数据
    LeetCode8.字符串转换整数(atoi) JavaScript
    LeetCode8.字符串转换整数(atoi) JavaScript
    WebRequestSugar
    iosblock用法
    datasci
    UINavigationController学习笔记
    iOSTab bar
    自定义tab bar控件 学习资料
    Ios tab Bar 使用方法
  • 原文地址:https://www.cnblogs.com/lingxia/p/7552844.html
Copyright © 2011-2022 走看看