Python 操作redis
#!/usr/bin/env pyhton
# coding:utf-8
# @Time : 2020-02-16 21:36
# @Author : LeoShi
# @Site :
# @File : redis_demo.py
# @Software : MacOS Python 3.7
import redis
# 创建链接
# 返回 b'peigy2020'
# redis_connect = redis.Redis(host='172.16.1.111', port=6379, password='password')
# decode_responses=True 返回字符串
redis_connect = redis.Redis(host='172.16.1.111', port=6379, password='password', decode_responses=True)
# 清空
redis_connect.flushall()
# 字符串操作
redis_connect.set('username', 'peigy2020')
# peigy2020
print(redis_connect.get('username'))
# 删除
redis_connect.delete('username')
# None
print(redis_connect.get('username'))
# 自增
for _ in range(10):
redis_connect.incr('user_id')
# 10
print(redis_connect.get('user_id'))
# 自减
for _ in range(10):
redis_connect.decr('user_id')
# 0
print(redis_connect.get('user_id'))
### 其他命令参照原始命令
# 事务
pip = redis_connect.pipeline()
pip.set('baidu','www.baidu.com')
pip.set('taobao','www.taobao.com')
pip.execute()
# ['taobao', 'user_id', 'baidu']
print(redis_connect.keys())