redis hash操作 list列表操作
HSET key 子key 子value
192.168.11.5:6379> HSET stu1 name 'zhangmingda'
(integer) 1
192.168.11.5:6379> HSET stu1 age 22
(integer) 1
192.168.11.5:6379> HSET stu1 sex boy
(integer) 1
192.168.11.5:6379> HSET stu1 school huawei
(integer) 1
192.168.11.5:6379> HGETALL stu1
1) "name"
2) "zhangmingda"
3) "age"
4) "22"
5) "sex"
6) "boy"
7) "school"
8) "huawei"
192.168.11.5:6379> HKEYS stu1 看所有key
1) "name"
2) "age"
3) "sex"
4) "school"
192.168.11.5:6379> HVALS stu1 看所有value
1) "zhangmingda"
2) "22"
3) "boy"
4) "huawei"
192.168.11.5:6379> HGET stu1 name
"zhangmingda"
192.168.11.5:6379> HMSET stu2 name 'qjj' age 22 sex 'boy' 同时设置多个
OK
192.168.11.5:6379> HMGET stu2 name age sex
1) "qjj"
2) "22"
3) "boy"
192.168.11.5:6379>
192.168.11.5:6379> HSCAN stu2 0 match n*
1) "0"
2) 1) "name"
2) "qjj"
192.168.11.5:6379> HSCAN stu1 0 match n* //从0位过滤stu1里面有多少个n开头的key 以及key的值。
hscan_iter(name, match=None, count=None) 过滤后返回迭代器。循环这个迭代器再取到过滤的值
1
2
3
4
5
6
7
8
9
|
# 利用yield封装hscan创建生成器,实现分批去redis中获取数据 # 参数: # match,匹配指定key,默认None 表示所有的key # count,每次分片最少获取个数,默认None表示采用Redis的默认分片个数 # 如: # for item in r.hscan_iter('xx'): # print item |
1) "0"
2) 1) "name"
2) "zhangmingda"
列表 list操作
LPUSH <list_name> value 1 value2 value3 ....
192.168.11.5:6379> LPUSH names_list alex hanyang zhangyang hehao liliang #先入后出 ,last in first out 后入先出
(integer) 5
192.168.11.5:6379> LRANGE names_list 0 3
1) "liliang"
2) "hehao"
3) "zhangyang"
4) "hanyang"
RPUSH <list_name> value 1 value2 value3 .... #先入先出,(从列表的右边往左边存)
192.168.11.5:6379> RPUSH names_list2 alex hanyang zhangyang hehao liliang
(integer) 5
192.168.11.5:6379> LRANGE names_list2 0 -1
1) "alex"
2) "hanyang"
3) "zhangyang"
4) "hehao"
5) "liliang"
192.168.11.5:6379> LPUSHX names_list2 zhangsan #只有names_list2 这个列表存在才存数据
(integer) 6
192.168.11.5:6379> LINSERT names_list2 BEFORE zhangyang ZHANGSAN # 在names_list2这个列表的zhangyang 前面插入 ZHANGYANG
(integer) 7
192.168.11.5:6379> LRANGE names_list2 0 -1
1) "zhangsan"
2) "alex"
3) "hanyang"
4) "ZHANGSAN"
5) "zhangyang"
6) "hehao"
7) "liliang"
192.168.11.5:6379> LINSERT names_list2 AFTER zhangyang ZHANGSAN
(integer) 8
192.168.11.5:6379> LRANGE names_list2 0 -1
1) "zhangsan"
2) "alex"
3) "hanyang"
4) "ZHANGSAN"
5) "zhangyang"
6) "ZHANGSAN"
7) "hehao"
8) "liliang"
192.168.11.5:6379> LRANGE names_list2 0 -1
1) "zhangsan"
2) "alex"
3) "hanyang"
4) "ZHANGSAN"
5) "zhangyang"
6) "ZHANGSAN"
7) "hehao"
8) "liliang"
192.168.11.5:6379> LSET names_list2 6 HE-HAO #修改列表里面的某个值。
OK
192.168.11.5:6379> LRANGE names_list2 0 -1
1) "zhangsan"
2) "alex"
3) "hanyang"
4) "ZHANGSAN"
5) "zhangyang"
6) "ZHANGSAN"
7) "HE-HAO"
8) "liliang"
192.168.11.5:6379>
192.168.11.5:6379> LRANGE names_list2 0 -1 ##############################
1) "zhangsan"
2) "hanyang"
3) "ZHANGSAN"
4) "zhangyang"
5) "ZHANGSAN"
6) "HE-HAO"
7) "liliang"
192.168.11.5:6379> LREM names_list2 2 ZHANGSAN #删除names_list2 这个列表里面的两个ZHANGSAN
(integer) 2
192.168.11.5:6379> LRANGE names_list2 0 -1
1) "zhangsan"
2) "hanyang"
3) "zhangyang"
4) "HE-HAO"
5) "liliang"
192.168.11.5:6379> LRANGE names_list2 0 -1
1) "zhangsan"
2) "hanyang"
3) "zhangyang"
4) "HE-HAO"
5) "liliang"
192.168.11.5:6379> LPOP names_list2 # 从左侧pop一个数据
"zhangsan"
192.168.11.5:6379> LRANGE names_list2 0 -1
1) "hanyang"
2) "zhangyang"
3) "HE-HAO"
4) "liliang"
RPOP names_list2 从右边获取ming
192.168.11.5:6379> LRANGE names 0 -1
1) "zhang1"
2) "zhang2"
192.168.11.5:6379> LRANGE names2 0 -1
1) "wang1"
2) "wang2"
192.168.11.5:6379> RPOPLPUSH names names2 #把names 列表最右边的,放到names2列表最左边。
"zhang2"
192.168.11.5:6379> LRANGE names2 0 -1
1) "zhang2"
2) "wang1"
3) "wang2"
192.168.11.5:6379> LRANGE names 0 -1
1) "zhang1"
brpoplpush(src, dst, timeout=0) 两个进程之间,从一个列表的右侧移除一个元素并将其添加到另一个列表的左侧
192.168.11.5:6379> LRANGE names2 0 -1
1) "zhang1"
2) "zhang2"
3) "wang1"
4) "wang2"
192.168.11.5:6379> BRPOPLPUSH names names2 100
"test"
(55.93s)
192.168.11.5:6379> LRANGE names2 0 -1
1) "test"
2) "zhang1"
3) "zhang2"
4) "wang1"
5) "wang2"
4.set集合操作(集合无序,自动去重)
192.168.11.5:6379> SADD names3 alex alex jack jack zhangmingda
(integer) 3
192.168.11.5:6379> SMEMBERS names3 #查看names3中的全部集合成员
1) "zhangmingda"
2) "jack"
3) "alex"
192.168.11.5:6379>
192.168.11.5:6379> SMEMBERS names3
1) "zhangmingda"
2) "jack"
3) "alex"
192.168.11.5:6379> SADD names4 zhangmingda hehe #创建集合names4
(integer) 2
192.168.11.5:6379> SDIFF names3 names4 #查看前者有,后者没有的差集
1) "jack"
2) "alex"
192.168.11.5:6379> SMEMBERS names4
1) "hehe"
2) "zhangmingda"
3) "lisi"
192.168.11.5:6379> SMEMBERS names3
1) "alex"
2) "hehe"
3) "zhangmingda"
4) "jack"
192.168.11.5:6379> SDIFFSTORE names names3 names4 #将前者有、后者没有的放到names里面
(integer) 2
192.168.11.5:6379> SMEMBERS names
1) "jack"
2) "alex"
192.168.11.5:6379> SINTER names3 names4 #取两个集合的交集
1) "hehe"
2) "zhangmingda"
192.168.11.5:6379> SINTERSTORE jiaoji names3 names4 #取两个集合的交集,制作一个新的jiaoji集合
(integer) 2
192.168.11.5:6379> SMEMBERS jiaoji
1) "zhangmingda"
2) "hehe"
192.168.11.5:6379> SISMEMBER names3 zhangmingda #监测值是否存在于集合中
(integer) 1
192.168.11.5:6379> SMEMBERS names3
1) "hehe"
2) "zhangmingda"
3) "jack"
4) "alex"
192.168.11.5:6379>
192.168.11.5:6379> SMOVE names3 names4 hehe #将names3 里的 hehe 移动到 names4
(integer) 1
192.168.11.5:6379> SMEMBERS names3
1) "zhangmingda"
2) "jack"
3) "alex"
192.168.11.5:6379> SPOP names4 #从尾部移除一个元素
"lisi"
192.168.11.5:6379> SRANDMEMBER names3 #随机获取一个元素
"alex"
192.168.11.5:6379> SREM names3 alex #删除对应的,集合元素
(integer) 1
192.168.11.5:6379> SUNION names3 names4 #获取两个集合的交集
1) "hehe"
2) "jack"
3) "zhangmingda"
4) "lisi"
192.168.11.5:6379> SUNIONSTORE newset names3 names4 #获取两个集合的交集,结果赋给一个新的集合
(integer) 4
192.168.11.5:6379> SSCAN newset 0 match *
1) "0"
2) 1) "hehe"
2) "jack"
3) "zhangmingda"
4) "lisi"
192.168.11.5:6379> SSCAN newset 0 match h* #从0位置开始查找匹配以h开头的元素
1) "0"
2) 1) "hehe"
192.168.11.5:6379> ZADD ordered_set 99 zhangsan 50 lisi 22 wangwu #设置一个集合 ordred_set 权重大小 元素 权重 元素.......数字越小越靠前
(integer) 3
192.168.11.5:6379> ZRANGE ordered_set 0 -1 #全部获取元素
1) "wangwu"
2) "lisi"
3) "zhangsan"
192.168.11.5:6379> ZRANGE ordered_set 0 -1 withscores #获取全部元素以及权重值
1) "wangwu"
2) "22"
3) "lisi"
4) "50"
5) "zhangsan"
6) "99"
192.168.11.5:6379> ZCOUNT ordered_set 0 50 #获取权重值0 - 50 (含)的元素
(integer) 2
192.168.11.5:6379> ZRANGE ordered_set 0 -1 withscores
1) "wangwu"
2) "32"
3) "lisi"
4) "50"
5) "zhangsan"
6) "99"
192.168.11.5:6379> ZINCRBY ordered_set 3 'wangwu' #将有序集合ordered_set 元素wangwu 的权重中自增指定值
"35"
192.168.11.5:6379>
192.168.11.5:6379> ZRANGE ordered_set 0 -1 withscores
1) "wangwu"
2) "35"
3) "lisi"
4) "50"
5) "zhangsan"
6) "99"
192.168.11.5:6379> ZRANK ordered_set zhangsan #查询元素的位置是什么
(integer) 2
192.168.11.5:6379> ZREMRANGEBYRANK ordered_set 0 1 #根据位置删除一个范围的元素
(integer) 2
192.168.11.5:6379> ZRANGE ordered_set 0 -1
1) "zhangsan"
192.168.11.5:6379> ZINTERSTORE zcount 2 ordered_set ordered_set1 #计算两个或者多个有序集合中同名元素的分数,并生成一个新的集合zcount
(integer) 2
192.168.11.5:6379> ZRANGE ordered_set 0 -1 withscores
1) "zmd"
2) "11"
3) "qjj"
4) "15"
5) "zhangsan"
6) "99"
192.168.11.5:6379> ZRANGE ordered_set1 0 -1 withscores
1) "qjj"
2) "15"
3) "zhangsan"
4) "20"
192.168.11.5:6379> ZRANGE zcount 0 -1 withscores
1) "qjj"
2) "30"
3) "zhangsan"
4) "119"
其他集合常用操作 :
192.168.11.5:6379> DEL zcount # 删除key
(integer) 1
192.168.11.5:6379> RENAME names3 names3_new #重命名
OK
192.168.11.5:6379> move names3_new 2 #将key移动到另外一个库
(integer) 1
192.168.11.5:6379> SMEMBERS name3_new
(empty list or set)
192.168.11.5:6379> SELECT 2 #切换数据库 redis默认有16个库 从0 到15
OK
192.168.11.5:6379[2]> SMEMBERS names3_new #2库里面有了
1) "zhangmingda"
2) "jack"
192.168.11.5:6379> type names #看key的类型
set
192.168.11.5:6379> SCAN 0 match name* #查找匹配key
1) "0"
2) 1) "names"
2) "names4"
posted on 2018-08-17 17:47 zhangmingda 阅读(352) 评论(0) 编辑 收藏 举报