面试题答案
一键面试import redis
# 连接Redis
r = redis.Redis(host='localhost', port=6379, db=0)
# 开启事务
pipe = r.pipeline()
try:
# 设置字符串类型的键值对
pipe.set('my_key', 'my_value')
# 在事务内读取这个键的值
pipe.get('my_key')
# 提交事务
result = pipe.execute()
print("设置操作成功,读取的值为:", result[1].decode('utf-8'))
except redis.exceptions.RedisError as e:
print("事务执行失败:", e)