七 7 2redis hash型別及其他常用操作

2022-08-28 13:24:18 字數 2549 閱讀 1511

#!/usr/bin/env python 

#coding:utf8

import redis

redis_config =

pool = redis.connectionpool(**redis_config)

r = redis.redis(connection_pool=pool)

#hash型別的操作 乙個name對應乙個字典

#hset hget hmset hmget

r.hset(

"dict1

","hello

","world")

print(r.hget(

"dict1

","hello"))

r.hmset(

"dict1

",)print(r.hmget(

"dict1

","key1

","hello"))

print(r.hgetall(

"dict1"))

print(r.hlen(

"dict1"))

print (r.hkeys(

"dict1"))

print(r.hvals(

"dict1"))

print(r.hexists(

"dict1

","hello"))

print(r.hexists(

"dict1

","hello11"))

r.hdel(

"dict1

","key2")

print(r.hgetall(

"dict1

"))

執行結果:

world['

value1

', '

world']

4['hello

', '

key3

', '

key1

', '

key2']

['world

', '

value3

', '

value1

', '

value2']

true

false

其他常用操作

delete(*names)

#根據name刪除redis中的任意資料型別

exists(name)

#檢測redis的name是否存在

keys(pattern='*')

#根據* ?等萬用字元匹配獲取redis的name

expire(name ,time)

# 為某個name設定超時時間

rename(src, dst)

# 重新命名

move(name, db))

將redis的某個值移動到指定的db下

# 將redis的某個值移動到指定的db下

type(name)

# 獲取name對應值的型別

例子:

import redis

redis_config =

pool = redis.connectionpool(**redis_config)

r = redis.redis(connection_pool=pool)

print(r.keys())

r.delete(

"name1")

print(r.keys())

print(r.type(

"dict1"))

# r.rename(

"cnblogs

","blog")

r.rename(

"blog

","cnblogs")

print(r.keys())

print(r.exists(

"blog

"))

執行結果:

['

set_name

', '

dict1

', '

name4

', '

blog

', '

name2

', '

name3

', '

name1

', '

name']

['set_name

', '

dict1

', '

name4

', '

blog

', '

name2

', '

name3

', '

name']

hash['

set_name

', '

dict1

', '

cnblogs

', '

name4

', '

name2

', '

name3

', '

name']

false

Redis Hash雜湊型別

redis所有的key都是字串 hash都是在redis裡的命令都是以 h開頭的 將雜湊表 key 中的字段 field 的值設為 value hset hash名稱 k v獲取儲存在雜湊表中指定欄位的值 hget hash名稱 k名同時將多個 field value 域 值 對設定到雜湊表 key...

redis Hash雜湊型別

127.0.0.1 6379 hset hash field hello world set乙個具體的key value值 integer 1 127.0.0.1 6379 hget hash field 獲取乙個值 hello world 127.0.0.1 6379 hmset hash fie...

Redis hash資料型別

1.hash型別?在redis中,hash是乙個鍵值 key value 對集合。簡單來講就是field和value的對映表,比較適合於儲存物件。hash的儲存用法 在這邊,如果你輸入的是漢語,想想也就知道我們在底層中儲存中,存到是編碼後的資料。2.常見的hash操作指令命令 解釋hdel key ...