import redis
class My(object):
def __init__(self):
self.__host = '域名'#如果变量前多了两个下划线,就是私有变量,只能在类里使用,出了类就不好用了
self.__port = #端口号
self.__password = '' #密码
self.__db = #库编码
try:
self.r = redis.Redis(host=self.__host,port=self.__port,password=self.__password,db=self.__db)
except Exception as e:
print('链接redis失败,失败原因:%s'%e)
def get(self,k):#获取str类型key
res = self.r.get(k)
if res:
return res.decode()
# return None
def str_set(self,k,v):#添加str类型key
try:
res = self.r.set(k,v)
except Exception as e:
print('添加key失败,失败原因:%s'%e)
else:
return res
def str_delete(self,k):#str类型的删除key
res = self.r.delete(k)
if res:
return res
return None
def hash
python之redis操作
最新推荐文章于 2024-06-14 18:30:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)