close
Redis 安裝已在下述連結中介紹,請安裝Redis後再進行後續操作。(記得要啟動 redis)
環境資訊 |
OS: Windows 10 Python: 3.7.4 |
下述程式呈現初始化redis流程、紀錄、存取與刪除
import redis
# 建立 Connection pool
pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
# 取用 connection pool 建立 redis 物件
r = redis.Redis(connection_pool=pool)
# redis 紀錄資料
r.set("Ken","Ken Test") #Key是Ken, value是Test字串
r.set("Alice","Alice Test") #Key是Ken, value是Test字串
r.set("Alex","Alex Test") #Key是Ken, value是Test字串
# redis 取用資料, 取出的 type 是 bytes
output = r.get("Ken")
print(output)
print(type(output))
# redis 取用資料, decode() 才會變成 string
output = r.get("Ken").decode()
print(output)
print(type(output))
# redis 刪除資料
r.delete("Ken")
# 設定自動刪除時間
r.expire("Alice",300)
|
執行結果:
b'Ken Test' <class 'bytes'> Ken Test <class 'str'> |
Redis-cli command line 介面使用
選擇 redis 資料夾中的: redis-cli.exe
瀏覽redis資料: keys *
清空redis: flushall
結果:
全站熱搜