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
原文地址:https://www.cnblogs.com/ericblog1992/p/11398792.html