一,redis与python交互

pip install redis

import redis

#连接
r = redis.StrictRedis(host="localhost",port=6379,password="yichen")

#方法1:根据数据类型的不同,调用不同的方法
#写
r.set("p1","good")

#读
print(r.get("p1"))

#lpush key value value....
#r.lpush("p1", "hello" )

#方法2:pipeline
#缓冲多条命令,然后依次执行,减少服务器-客户端之间的TCP数据包
pipe = r.pipeline()
pipe.set("p2","nice")
pipe.set("p3","handsom")
pipe.set("p4","cool")
pipe.execute()



print(r.get("p3"))



r.close()

二,封装redis

import redis

class YichenRedis():
    def __init__(self,passwd,host="localhost",port=6379):
        self.__redis = redis.StrictRedis(host=host,port=port,password=passwd)

    def set(self,key,value):
        self.__redis.set(key,value)

    def get(self,key):
        if self.__redis.exists(key):
            return self.__redis.get(key)
        else:
            return ""
Last modification:July 1st, 2020 at 04:23 am
如果觉得我的文章对你有用,请随意赞赏