redis基本命令hash

1.hset

hset(name,key,value)

设置name对应的hash中的一个键值对,不存在则创建;存在则修改。

import redis,time
r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hset('hash1','name','wl')  # 设置值
print(r.hkeys('hash1'))  # 取hash中的所有key  ['name']
print(r.hget('hash1','name'))  # 取hash中的name值  wl

2.hsetenx

hsetnx(name,key,value)

设置那么中对应的键值对,是只能新建攸,亲。不能修改

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hsetnx('hash1','name','wl')  # 设置值
r.hsetnx('hash1','name','wl1')  # 设置值
print(r.hget('hash1','name'))  # wl

3.hmset

hmset(name,mapping),批量设置。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 设置值
print(r.hgetall('hash1'))  # {'name1': 'wl1', 'name': 'wl'}

4.hget和hmget

  • hget(name,key):获取1个key对应的值
  • hmget(name,key,args):获取多个个key对应的值
import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 设置值
print(r.hget('hash1','name'))  # wl
print(r.hmget('hash1','name','name1'))  #  ['wl', 'wl1']

5.hgetall

hgetall(name)—获取name中所有的键值对

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 设置值
print(r.hgetall('hash1'))  # {'name1': 'wl1', 'name': 'wl'}

6.hlen

hlen(name)—获取name的长度。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 设置值
print(r.hlen('hash1'))  # 获取hash中键值对的个数   2

7.hkeys

hkeys(name)—获取name中所有的个数。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 设置值
print(r.hkeys('hash1'))  # ['name', 'name1']

8.hvals

hvals(name)—获取name中所有的value。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 设置值
print(r.hvals('hash1'))  # ['wl', 'wl1']

9.hexists

hexists(name,key)

判断key对应的值是否在name中,就是判断成员是否存在。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 设置值
print(r.hexists('hash1','name'))  # True
print(r.hexists('hash1','name2'))  # False

10.hdel

hdel(name,*key)—删除指定的键值对。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1'})  # 设置值
r.hdel('hash1','name','name1')
print(r.hgetall('hsah1'))  # {}

11.hincrby

1)hincrby(name,key,amount=1),自增自减函数,将key对应的值自增或者减1。复数为减。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1','age':21})  # 设置值
r.hincrby('hash1','age',amount=1)
print(r.hgetall('hash1'))  # {'name1': 'wl1', 'age': '22', 'name': 'wl'}

2)hincrbyfloat(name,key,amount=1),自增自减函数,将key对应的值自增或者减,浮点数。复数为减。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1','age':21.0})  # 设置值
r.hincrbyfloat('hash1','age',amount=1.0)
print(r.hgetall('hash1'))  # {'name1': 'wl1', 'age': '22', 'name': 'wl'}

12.hscan(name,cursor=0,match=None,count=1)

取值查看,分片阅读。这种读取方式对于大数据量的读取非常有用,分片的读取数据可以有效的防治一次性读入超量数据导致内存撑爆。

参数:

cursor:游标-基于游标获取数据

match:匹配指定的key,None表示所有。

count:每次读取的个数,None表示redis默认的读取个数。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1','age':21.0})  # 设置值
r.hincrbyfloat('hash1','age',amount=1.0)
print(r.hscan('hash1',cursor=0,match=None,count=1))
# (0, {'name': 'wl', 'name1': 'wl1', 'age': '22'})

13.hscan_iter

hscan_iter(name,match=None,count=None)

使用yield封装hash创建生成器,实现分批获取数据。

import redis,time

r = redis.Redis(host='localhost',port=6379,decode_responses=True)
r.hmset('hash1',{'name':'wl','name1':'wl1','age':21.0})  # 设置值
r.hincrbyfloat('hash1','age',amount=1.0)

for item in r.hscan_iter('hash1'):
    print(item)
# ('name1', 'wl1') ('age', '22') ('name', 'wl')

猜你喜欢

转载自www.cnblogs.com/wl443587/p/10269562.html