Python連線redis的兩種方式

2021-10-07 16:26:06 字數 1805 閱讀 1454

------先安裝redis庫

------pip install redis

第一種

from redis import redis

''' 一般連線,使用redis連線

'''# 這裡使用redis

db = redis(host=

"localhost"

, port=

6379

, db=

0, decode_responses=

true

)# 操作結果的資料型別預設為二進位制,除非decode_responses=true

resu = db.smembers(

'set1'

)print

(resu)

from redis import strictredis

''' 一般連線,使用strictredis連線

'''# 這裡使用strictredis

db = strictredis(host=

"localhost"

, port=

6379

, db=

0, decode_responses=

true

)

第二種

from redis import connectionpool

from redis import redis

''' 使用連線池 連線redis庫的 redis

'''# 建立pool

pool = connectionpool(host=

'localhost'

, port=

6379

, db=

0, decode_responses=

true

)# 傳入pool

db = redis(connection_pool=pool)

# 下方是獲取操作結果

resu = db.hget(

'mark'

,"name"

)resu2 = db.hmget(

'mark'

,'name'

,"age"

,"h"

)resu3 = db.hgetall(

"mark"

)# 下方是輸出操作結果

print

(resu,

type

(resu)

, sep=

" 型別是: "

)print

(resu2,

type

(resu2)

, sep=

" 型別是: "

)print

(resu3,

type

(resu3)

, sep=

" 型別是: "

)

from redis import connectionpool

from redis import strictredis

''' 使用連線池連線 redis庫的 strictredis

'''pool = connectionpool(host=

"localhost"

, port=

6379

, db=

0, decode_responses=

true

)db = strictredis(connection_pool=pool)

python連線redis解析protobuf資料

這邊說的是windows下環境配置 本人在嘗試安裝2.7.0版本後手動安裝 pip 和protobuf模組,發現protobuf2.5.0版本可以成功安裝但是protobuf2.6.1版本安裝各種報錯,所以使用protobu2.6.1 python protobuf環境配置,及其簡單使用,軟體都已經...

redis系列 redis的連線

redis 是完全開源免費的,遵守bsd協議,先進的key value持久化產品。它通常被稱為資料結構伺服器,因為值 value 可以是 字串 string 雜湊 map 列表 list 集合 sets 和 有序集合 sorted sets 等型別。redis客戶端連線比較簡單,但日常中redis的...

Python連線redis時要注意的點

一 一般連線redis情況 1 from redis import redis2 例項化redis物件 3 rdb redis host localhost port 6379,db 0 4 rdb.set name root 5 name rdb.get name 6 print name 這種情...