import redis
class
redisstring
(object):
def__init__
(self,host,port=
6397
,dbnum=0)
: self.r = redis.strictredis(host=host,port=port,db=dbnum)
# 設定值
defsetrecord
(self,key,value)
: res = self.r.
set(key,value)
print
("class : set "
,key ,
"=",res)
# 取值
defgetvalue
(self,key)
: ret=self.r.get(key)
print
("class : "
,key ,
"=",ret)
return ret
# 設定多個值
defmsetrecord
(self,d)
: ret = self.r.mset(d)
print
("class : mset "
, d,
"=", ret)
# 取多個值
defmgetvalues
(self,keys)
: ret = self.r.mget(keys)
return ret
# 刪除
defdelrecord
(self,key)
: ret=self.r.delete(key)
print
("class : del "
,key,
"=",ret)
defmain()
: test_rds=redisstring(
"192.168.8.151"
,6379,1
) test_rds.setrecord(
"user1"
,"laoluobo"
) str0=test_rds.getvalue(
"user1"
)print
("user1 ="
,str0)
dict1=
test_rds.msetrecord(dict1)
list1 =
['user1'
,'user2'
,'user3'
] list2=test_rds.mgetvalues(list1)
print
(list2)
test_rds.delrecord(
"user1"
) str0=test_rds.getvalue(
"user1"
)print
(str0)
if __name__==
"__main__"
: main(
)# class : set user1 = true
# class : user1 = b'laoluobo'
# user1 = b'laoluobo'
# class : mset = true
# [b'laoluobo', b'juran', b'tony']
# class : del user1 = 1
# class : user1 = none
# none
import redis
class
redislist
(object):
def__init__
(self,host,port=
6397
,dbnum=0)
: self.r = redis.strictredis(host=host,port=port,db=dbnum)
# 插入記錄
deflpush
(self,key,list1)
:for item in list1:
res = self.r.lpush(key,item)
# 插入記錄
defrpush
(self,key,list1)
:for item in list1:
res = self.r.rpush(key,item)
# 彈出記錄
deflpop
(self,key)
: res = self.r.lpop(key)
defrpop
(self,key)
: res = self.r.rpop(
'common'
)# 範圍取值
deflrange
(self,key ,start,end)
: ret = self.r.lrange(key,start,end)
return ret
defmain()
: rdl=redislist(
"192.168.8.151"
,6379,1
) list1=[1
,2,3
,4,5
] rdl.lpush(
"laoluobo1"
,list1)
list2=rdl.lrange(
"laoluobo1",0
,-1)
print
("lpush : laoluobo1 ="
,list2)
rdl.rpush(
"laoluobo2"
,list1)
list2=rdl.lrange(
"laoluobo2",0
,-1)
print
("rpush : laoluobo2 ="
,list2)
rdl.lpop(
"laoluobo1"
) list2=rdl.lrange(
"laoluobo1",0
,-1)
print
("lpop : laoluobo1 ="
,list2)
rdl.lpop(
"laoluobo2"
) list2=rdl.lrange(
"laoluobo2",0
,-1)
print
("rpop : laoluobo2 ="
,list2)
if __name__==
"__main__"
: main(
)# lpush : laoluobo1 = [b'5', b'4', b'3', b'2', b'1']
# rpush : laoluobo2 = [b'1', b'2', b'3', b'4', b'5']
# lpop : laoluobo1 = [b'4', b'3', b'2', b'1']
# rpop : laoluobo2 = [b'2', b'3', b'4', b'5']
import redis
class
redishash
:def
__init__
(self,host,port=
6397
,dbnum=0)
: self.r = redis.strictredis(host=host,port=port,db=dbnum)
# 批量設值
defhset
(self,dict1)
: ret = self.r.hmset(
'mobile'
,dict1)
print
(ret)
# 批量取值
defhgetall
(self)
: ret = self.r.hgetall(
'mobile'
)return ret
# 判斷是否存在 存在返回1 不存在返回0
defhexists
(self,key,field)
: ret = self.r.hexists(key,field)
print
(ret)
defmain()
: rdh=redishash(
"192.168.8.151"
,6379,1
) key=
"mobile"
dic =
rdh.r.hset(key,dic)
dict2=rdh.r.hgetall(key)
print
(dict2)
ret=rdh.r.hexists(key,
"name"
)print
(ret)
if __name__==
"__main__"
: main(
)
程式報錯:redis.exceptions.dataerror: invalid input of type: 『dict』. convert to a bytes, string, int or float first. 鹹魚筆記 redis與Python互動
爭當鹹魚王,歡迎大神指點 redis與python互動 安裝包進入虛擬環境py django,聯網安裝包redis 呼叫模組 from redis import 這個模組中提供了strictredis物件 strict嚴格 用於連線redis伺服器,並按照不同型別提供 了不同放法,進行互動操作 st...
Redis與Python互動的具體應用方法
安裝 pip install redis 連線r redis.strictredis host localhost port 6379,db 0 import redis class teststring object def init self self.r redis.strictredis h...
redis資料庫及與python互動用法簡單示例
1.string型別 主要儲存字串 操作命令 設定鍵值 set key value 設定鍵值與過期時間 setex key seconds 單位為秒 value 設定多個鍵 met key1 value1 key2 value2 追加值append key value 獲取值get key 獲取多個...