import redis
def main():
client = redis.Redis(host=‘ ‘, port= , password=‘ ‘)
# 设置一个键值对,有效时间为300秒
client.set(‘username‘, ‘hello‘, ex=300)
# 获取键名为username的有效时间
print(client.ttl(‘username‘))
# 获取键值,并以默认utf-8方式解码
print(client.get(‘username‘).decode())
if __name__ == ‘__main__‘:
main()