• redis操作


    一、操作字符串:

    import redis
    r = redis.Redis(host="iphost",port=6379,password="HK139bc&*",decode_responses=True) #decode_responses为True 就不需要再用decode转了,否则返回的就是字节b'MLing'
    #字符串类型
    r.set("lcl_session","MLing",500) #添加数据
    session = r.get("lcl_session") #查询数据
    print(session)
    r.delete("lcl_session") #删除数据
    print(r.get("lcl_session")) #删除之后,再查询是None
    # new_session = session.decode() #decode_responses为True 就不需要再用decode转了
    # print(new_session)
     
    运行结果:
    MLing
    None
    

      

    expire和expireat的区别:

    expire函数设置过期时间为10秒。10秒后,ex1将会失效

    expireat设置一个具体的时间,15年9月8日15点19分10秒,过了这个时间,ex2将失效

    import redis  
    import datetime  
    import time  
      
    pool=redis.ConnectionPool(host='iphost',port=6379,db=0)  
    r = redis.StrictRedis(connection_pool=pool)  
    extime = datetime.datetime(2015,9,8,15,19,10)  
    print r.expire('ex1', 10)  
    print extime.strftime('%Y-%m-%d %H:%M:%S %f')    
    print r.expireat('ex2', extime)  

     

    二、操作哈希

    #哈希类型
    import redis
    r = redis.Redis(host="iphost",port=6379,password="HK139bc&*",decode_responses=True) #decode_responses为True 就不需要再用decode转了,否则返回的就是字节b'MLing'
    r.hset("ssy_student","wanghe","xxx")
    r.hset("ssy_student","lj","xxx11")
    r.hset("ssy_student","cmc","xxx1122")
    r.hset("ssy_student","ccx","xxx11223")
    # print(r.hget("ssy_student","cmc").decode())  #decode_responses为True 就不需要再用decode转了
    print(r.hget("ssy_student","cmc"))#获取某个字段的值
    print(r.hgetall("ssy_student")) #获取ssy_student下的所有数据
    r.hdel("ssy_student","ccx") #删除haxi中的某一个字段和它的值
    r.delete('sys_student')#删除整个大key print(r.hgetall("ssy_student"))#获取ssy_student下的所有数据 运行结果: xxx1122 {'c': '1111', 'd': '2222', 'ccx': 'xxx11223', 'wanghe': 'xxx', 'e': '3333', 'lj': 'xxx11', 'cmc': 'xxx1122'} {'c': '1111', 'd': '2222', 'wanghe': 'xxx', 'e': '3333', 'lj': 'xxx11', 'cmc': 'xxx1122'}

     三、常用的方法

    print(r.keys()) #所有的key
    print(r.keys('*session*'))#模糊匹配
    print(r.exists("lcl_session"))#key是否存在
    print(r.type("lj_session"))
    print(r.type("ssy_student"))
    r.expire("ssy_student",50)#指定某个key的过期时间

    r.flushall() #清空所有数据库里面的key
    r.flushdb() #清空当前数据库里面的所有key
    r.hmset(key,{'id':12,'name':78})#一次插入多个小key(hash)

     四、redis迁移

    import redis
    r1 = redis.Redis(host="10.24.3.99",port=6379,password="HK139bc&*",decode_responses=True)
    r2 = redis.Redis(host="10.24.3.99",port=6379,password="HK139bc&*",decode_responses=True,db=6)
    
    
    for k in r1.keys():
        key_type = r1.type(k)
        if key_type == 'string':
            value = r1.get(k)
            r2.set(k,value)
        elif key_type == 'hash':
            value = r1.hgetall(k) #
            r2.hmset(k,value)#
        else:
            pass
    

      

  • 相关阅读:
    关于在windows平台下将应用制作成windows服务及服务依赖的感想
    mysql 变量赋值的三种方法
    如何上传本地jar至远程仓库供其他项目使用
    maven的标准
    修改idea的缓存
    前端的网站
    读取简单的xml
    IDEA 自动设置compile target变成1.5
    注解导出优化版(推荐,十分强大)
    Linux命令干货!!!最常用的命令
  • 原文地址:https://www.cnblogs.com/MLing/p/12968279.html
Copyright © 2020-2023  润新知