安裝模塊
pip install redis
基本代碼塊
from redis import StrictRedis
sr=StrictRedis(host='127.0.0.1',port=6379)
# 設置字符串
# 終端命令:set key value
# set() 如果鍵存在,則修改鍵的值,如果不存在,則添加數據
ret =sr.set("age",20)
print(ret)
# 獲取字符串
# 終端命令:get key
age_byte=sr.get("age")
age=age_byte.decode()#獲取到的數據爲二進制,需要解碼
print(age)
# 刪除指定鍵對應的數據,不僅可以刪除字符串
# del key
ret=sr.delete('name')
print(ret)#成功返回1,失敗返回0
# 其他的類型數據的操作,可以自己去根據文檔擴展
# 新增的一個hash成員
# 終端命令:hset 鍵 域1 值1
sr.hset('dict',"hello",'word')