安装redis

pip install redis

使用连接池连接redis,避免每次建立、释放连接的开销:

import redis
#python操作redis数据库部分功能封装
class RedisHandler:
def __init__(self, host, port, db, password, decode_responses=True):
     #建立连接
self.client = redis.ConnectionPool(host=host, port=port, db=db, password=password, decode_responses=decode_responses)
self.r = redis.StrictRedis(connection_pool=self.client)
   # 获取hash值
def hash_get(self, name, key=None, one=True):
if not one:
hash_data = self.r.hgetall(name)
else:
hash_data = self.r.hget(name, key)
return hash_data
  # 删除hash值
def hash_delete(self, name, key):
self.r.hdel(name, key)
  # 获取string值
def string_get(self, name):
str_data = self.r.get(name)
return str_data

 


  

做一棵小草,谁也撼动不了………

 

posted on 2020-05-21 16:26  小草小草随风飘摇  阅读(245)  评论(0编辑  收藏  举报