1、redis单例模式
前提:安装 pip install redis
import redis
r = redis.Redis(host='192.168.30.153', port=6383,db=0,decode_responses=True)
list_keys = r.keys("http*")
for key in list_keys:
r.delete(key)
2、reidis集群模式
前提:安装 pip install redis-py-cluster
from rediscluster import StrictRedisCluster
install redis-py-cluster
redis_nodes = [{'host':'192.168.3.153','port':6383},
{'host':'192.168.3.153','port':6384}
]
try:
redisconn = StrictRedisCluster(startup_nodes=redis_nodes)
list_keys = redisconn.keys("http*")
for key in list_keys:
redisconn.delete(key)
except:
print("Connect Error!")
3、集群模式下另一种,更为简洁的方式
from rediscluster import StrictRedisCluster
from com.unif.conf.ConstantConfig import ConstantConfig
from com.unif.util.LogUtil import LogUtil
from com.unif.util.SendEmailUtil import SendEmailUtil
redis_nodes = ConstantConfig.redis_nodes
redis_conn = StrictRedisCluster(startup_nodes=redis_nodes)
logger = LogUtil.get_logger('RedisUtil')
class RedisUtil:
def __init__(self):
print("初始化:RedisUtil")
@staticmethod
def delete_all():
try:
list_keys = redis_conn.keys("http*")
redis_conn.delete(*redis_conn.keys('http*'))
# for key in list_keys:
# redis_conn.delete(key)
except Exception as e:
print("Connect Error!")
# 调用
# print(RedisUtil.set_nx('abc'))
RedisUtil.delete_all()