zoukankan      html  css  js  c++  java
  • 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
    

      

  • 相关阅读:
    JavaWeb--HttpSession案例
    codeforces B. Balls Game 解题报告
    hdu 1711 Number Sequence 解题报告
    codeforces B. Online Meeting 解题报告
    ZOJ 3706 Break Standard Weight 解题报告
    codeforces C. Magic Formulas 解题报告
    codeforces B. Sereja and Mirroring 解题报告
    zoj 1109 Language of FatMouse 解题报告
    hdu 1361.Parencodings 解题报告
    hdu 1004 Let the Balloon Rise 解题报告
  • 原文地址:https://www.cnblogs.com/MLing/p/12968279.html
Copyright © 2011-2022 走看看