Python-Redis的Hash操作
Redis的hash是一个string类型的field和value的映射表,特别适合用于存储对象,每个hash可以存储40多亿键值对
hset(name, key, value):创建一个name的类别,类别里创建key-value键值对,如果已经存在就修改
hgetall(name):获取name对应的hash中全部的key和value
127.0.0.1:6379> hset info name jack (integer) 1 127.0.0.1:6379> hset info age 22 (integer) 1 127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "22"
hsetnx(name, key, value):当name对应的hash中不存在当前key时则创建成功(相当于添加)
127.0.0.1:6379> hsetnx info id 11 (integer) 1 127.0.0.1:6379> hsetnx info id 12 (integer) 0 127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "22" 5) "id" 6) "11"
hget(name,key):获取name对应的hash中key的value
127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "22" 5) "id" 6) "11" 127.0.0.1:6379> hget info name "jack" 127.0.0.1:6379> hget info age "22" 127.0.0.1:6379> hget info id "11"
hmset(name, **kwags):在name对应的hash中批量设置键值对
127.0.0.1:6379> hmset language China Chinese US English OK 127.0.0.1:6379> hgetall language 1) "China" 2) "Chinese" 3) "US" 4) "English"
hmget(name, keys):在name对应的hash中获取多个key的值
127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "22" 5) "id" 6) "11" 127.0.0.1:6379> hmget info name age id 1) "jack" 2) "22" 3) "11"
hlen(name):获取name对应的hash中键值对的个数
127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "22" 5) "id" 6) "11" 127.0.0.1:6379> hlen info (integer) 3
hkeys(name):获取name对应的hash中所有的key的值
127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "22" 5) "id" 6) "11" 127.0.0.1:6379> hkeys info 1) "name" 2) "age" 3) "id"
hvals(name):获取name对应的hash中所有的value的值
127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "22" 5) "id" 6) "11" 127.0.0.1:6379> hvals info 1) "jack" 2) "22" 3) "11"
hdel(name,*keys):将name对应的hash中指定key的键值对删除
127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "22" 5) "id" 6) "11" 127.0.0.1:6379> hdel info id (integer) 1 127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "22"
hexists(name, key):判断name对应的hash中是否存在这个key
127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "22" 127.0.0.1:6379> hexists info id (integer) 0
hincrby(name, key, amount=1):自增name对应的hash中的指定key的值,不存在则创建key=amount,amount为整型
127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "22" 127.0.0.1:6379> hincrby info age 2 (integer) 24 127.0.0.1:6379> hincrby info age 1 (integer) 25 127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "25"
hincrbyfloat(name, key, amount=1.0):自增name对应的hash中的指定key的值,不存在则创建key=amount,amount为浮点型
127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "25" 127.0.0.1:6379> hincrbyfloat info age 1.2 "26.2" 127.0.0.1:6379> hincrbyfloat info age 1.3 "27.5" 127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "27.5"
hscan(name, cursor=0, match=None, count=None):获取name对应的hash中匹配的key
cursor为游标(基于游标分批取获取数据),match为匹配指定key,默认None 表示所有的key,count为每次分片最少获取个数,默认None表示采用Redis的默认分片个数
127.0.0.1:6379> hgetall info 1) "name" 2) "jack" 3) "age" 4) "27.5" 5) "name_1" 6) "john" 7) "age_1" 8) "31" 9) "name_2" 10) "jane" 11) "age_2" 12) "31" 13) "name_3" 14) "Andy" 15) "age_3" 16) "11" 17) "id" 18) "11" 19) "id_1" 20) "22" 21) "id_2" 22) "33" 23) "id_3" 24) "44" 127.0.0.1:6379> hscan info 0 match a* # 匹配以a开头的 1) "0" 2) 1) "age" 2) "27.5" 3) "age_1" 4) "31" 5) "age_2" 6) "31" 7) "age_3" 8) "11" 127.0.0.1:6379> hscan info 0 match *a* # 匹配包含a的 1) "0" 2) 1) "name" 2) "jack" 3) "age" 4) "27.5" 5) "name_1" 6) "john" 7) "age_1" 8) "31" 9) "name_2" 10) "jane" 11) "age_2" 12) "31" 13) "name_3" 14) "Andy" 15) "age_3" 16) "11"
hscan_iter(name, match=None, count=None):为迭代hscan