Python redis 操作

import redis
import json
import pickle

# pool来管理对一个redis server的所有连接,避免每次建立、释放连接的开销
# 加上 decode_responses=True,写入的键值对中的value为str类型,不加这个参数写入的则为字节类型。
REDIS_HOST = '127.0.0.1'
REDIS_PORT = 6379
REDIS_PASS = '123456'

def redisUtils(key):
    pool = redis.ConnectionPool(host=REDIS_HOST,port=REDIS_PORT,password=REDIS_PASS,max_connections=10000,decode_responses=False)
    db = redis.Redis(connection_pool=pool)
    return db.get(key)


_a = redisUtils(key=":1:test01.com")

# 由于取出的值是 byes 类型,需要转换一下
_b = json.loads(pickle.loads(_a))
print(_b)
print(_b['redirect'])

更多请参考: https://www.jianshu.com/p/2639549bedc8

Last updated