• 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
  • 相关阅读:
    由"跨域"引出的一个终极思想(jsonp)
    SQLAlchemy 使用教程
    rbac-基于角色的权限控制系统(8种常用场景再现)
    Django中间件 (middleware)
    tcp粘包问题原因及解决办法
    细说【json&pickle】dumps,loads,dump,load的区别
    python面向对象--快速入门
    python三大器(装饰器/生成器/迭代器)
    django神器 <自定义过滤器filter 和 标签tag>
    python 基础数据类型汇总
  • 原文地址:https://www.cnblogs.com/ericblog1992/p/11398792.html
Copyright © 2020-2023  润新知