from redis import StrictRedis # 导包 if __name__ == '__main__': try: # 创建对象(ip和端口号,如果是本机则不用填) sr = StrictRedis() # 添加或修改一个key, key为name, value为'哈哈哈',返回True或False res = sr.set('name', '哈哈哈') print(res) # 获取key为name的值,返回对应的value值 res = sr.get('name') print(res) # 删除key为name的记录,删除成功则返回数字1 res = sr.delete('name') print(res) # 删除多个键值,成功删除几个则返回数字几 res = sr.delete('a1', 'a2') print(res) # 获取数据库当中所有的键,返回一个列表 res = sr.keys() print(res) except Exception as e: print(e)