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

    from redis_db import pool
    import redis
    import time

    con = redis.Redis(
    connection_pool=pool
    )

    try:
    # 字符串
    # con.set("country", "英国")
    # con.set("city", "伦敦")
    # # city = con.get("city").decode("utf-8")
    # con.expire("city", 5)
    # time.sleep(6)
    # city = con.get("city").decode("utf-8")
    # print(city)

    # 字符串
    # con.delete("country","city")
    # con.mset({"country":"德国","city":"柏林"})
    # result = con.mget("country",'city')
    # for one in result:
    # print(one.decode("utf-8"))

    # 列表
    # con.delete("dname")
    # con.rpush("dname","董事会","秘书处","财务部","技术部")
    # con.lpop("dname")
    # result = con.lrange("dname", 0, -1)
    # for one in result:
    # print(one.decode("utf-8"))


    # 无序集合
    # con.delete("employee")
    # con.sadd("employee",8001,8002,8003)
    # con.srem("employee",8001)
    # result = con.smembers("employee")
    # for one in result:
    # print(one.decode("utf-8"))
    #
    # # 有序集合
    # con.delete("keyword")
    # con.zadd("keyword",{"马云":0,"张朝阳":0,"丁磊":0})
    # con.zincrby("keyword", "10","马云")
    # result = con.zrevrange("keyword", 0, -1)
    # for one in result:
    # print(one.decode("utf-8"))

    # 哈希
    # con.hmset("9527",{"name":"Scott","sex":"male","age":"35"})
    # con.hset("9527","city","纽约")
    #
    # # con.hdel("9527","age")
    # exists = con.hexists("9527","name")
    # # print(exists) True
    #
    # result = con.hgetall("9527")
    # for one in result:
    # print(one.decode("utf-8"),result[one].decode("utf-8"))


    # 事务
    # pipline = con.pipeline()
    # pipline.watch("9527")
    # pipline.multi()
    #
    # pipline.hset("9527","name","Jack")
    # pipline.hset("9527","age",23)
    #
    # pipline.execute()

    except Exception as e:
    print(e)

    finally:
    if "pipline" in dir():
    pipline.reset()
    del con
  • 相关阅读:
    Android进程生命周期与ADJ
    四大组件之综述
    Linux进程pid分配法
    Linux的进程管理
    Linux硬盘管理
    Linux用户管理
    Linux命令行使用
    vim技巧5 常用操作
    vim技巧4 删除/保留文本中匹配行
    如何绘制UML图?
  • 原文地址:https://www.cnblogs.com/ericblog1992/p/11398792.html
Copyright © 2011-2022 走看看