第三章 : redis数据结构种类

第三章 : redis数据结构种类第三章 : redis数据结构种类

大家好,又见面了,我是你们的朋友全栈君。

五中数据结构:字符串(String) ,字符串列表(list),有序字符串集合(sorted set),哈希(hash),字符串集合(set)。

下面介绍下string 的应用:

得到所有key

127.0.0.1:6379> keys *
1) "foo"
2) "liux"
3) "liux2"

获得值:

127.0.0.1:6379> get foo
"bar"

删除值:

127.0.0.1:6379> del foo
(integer) 1
127.0.0.1:6379> keys *
1) "liux"
2) "liux2"

设置获取值:

127.0.0.1:6379> set name xiaoyexinxin
OK
127.0.0.1:6379> get name
"xiaoyexinxin"

incr自增,integer可直接变String,但是String 不能变integer,可理解为字符串格式的数字,能自增

127.0.0.1:6379> incr n
(integer) 1
127.0.0.1:6379> get n
"1"
127.0.0.1:6379> set n 2
OK
127.0.0.1:6379> get n
"2"
127.0.0.1:6379> set n ee
OK
127.0.0.1:6379> incr n
(error) ERR value is not an integer or out of range

decr 自减,同自增

127.0.0.1:6379> decr n1
(integer) -1
127.0.0.1:6379> get n1
"-1"
127.0.0.1:6379> set n1 ee
OK
127.0.0.1:6379> get n1
"ee"
127.0.0.1:6379> desc n1
(error) ERR unknown command 'desc'

incrby 指向自增增量

127.0.0.1:6379> incrby n3 6
(integer) 6
127.0.0.1:6379> get n3
"6"

append 追加

127.0.0.1:6379> append name qwe
(integer) 4
127.0.0.1:6379> get name
"3qwe"
127.0.0.1:6379> append name 123
(integer) 7
127.0.0.1:6379> get name
"3qwe123"

同时删除多个值(key):

127.0.0.1:6379> keys *
1) "n3"
2) "n"
3) "n4"
4) "name2"
5) "liux"
6) "n1"
7) "name"
8) "liux2"
127.0.0.1:6379> del n3 n n4
(integer) 3
127.0.0.1:6379> keys *
1) "name2"
2) "liux"
3) "n1"
4) "name"
5) "liux2"
127.0.0.1:6379> 

下面我们介绍下hash类型的:

设置获取hash

127.0.0.1:6379> hset h1 username xiaoming
(integer) 1
127.0.0.1:6379> get hset h1
(error) ERR wrong number of arguments for 'get' command
127.0.0.1:6379> get h1
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> hset h1 username
(error) ERR wrong number of arguments for 'hset' command
127.0.0.1:6379> hget h1
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379> hget h1 username
"xiaoming"

设置获取多个hash

127.0.0.1:6379> hmset h2 username xiaoming2 password 123qwe
OK
127.0.0.1:6379> hget h2 username
"xiaoming2"
127.0.0.1:6379> hget h2 password
"123qwe"
127.0.0.1:6379> hgetall h2
1) "username"
2) "xiaoming2"
3) "password"
4) "123qwe"

删除一个或多个hash

127.0.0.1:6379> hdel h1 username
(integer) 1
127.0.0.1:6379> hgetall h1
(empty list or set)
127.0.0.1:6379> hdelall h2
(error) ERR unknown command 'hdelall'
127.0.0.1:6379> hdel h2 username password
(integer) 2

hash设置自增和自增量:

127.0.0.1:6379> hset h3 age 20
(integer) 1
127.0.0.1:6379> hget h3 age
"20"
127.0.0.1:6379> hincrby h1 age 5
(integer) 5
127.0.0.1:6379> hget h1 age
"5"
127.0.0.1:6379> hincrby h3 age 6
(integer) 26

hash 判断是否存在某元素,返回1为存在,返回0不存在

127.0.0.1:6379> hset h5 name xiaoming
(integer) 1
127.0.0.1:6379> hget h5 name
"xiaoming"
127.0.0.1:6379> hexists h5 name
(integer) 1
127.0.0.1:6379> hexists h5 name2
(integer) 0
127.0.0.1:6379> hget h5 name2
(nil)

获取属性个数

127.0.0.1:6379> hgetall h5
1) "name"
2) "xiaoming"
127.0.0.1:6379> hlen h5
(integer) 1

获取所有值:

127.0.0.1:6379> hvals h5
1) "xiaoming"


list的使用,常用的命令有两端添加,两端弹出,扩展命令:

从左开始添加,然后获取:

127.0.0.1:6379> lpush l1 a b c d
(integer) 4
127.0.0.1:6379> lpush l1 1 2 3 4
(integer) 8
127.0.0.1:6379> lrange l1 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
5) "d"
6) "c"
7) "b"
8) "a"

获取部分:

127.0.0.1:6379> lrange l1 0 4
1) "4"
2) "3"
3) "2"
4) "1"
5) "d"

从右开始添加,然后获取:

127.0.0.1:6379> rpush l2 a b c d
(integer) 4
127.0.0.1:6379> rpush 1 2 3 4
(integer) 3
127.0.0.1:6379> rrenge l2
(error) ERR unknown command 'rrenge'
127.0.0.1:6379> lrange l2
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379> lrange l2 0 -1
1) "a"
2) "b"
3) "c"
4) "d"

rpop 从右侧弹出集合元素

127.0.0.1:6379> rpop l1 
"a"
127.0.0.1:6379> LRANGE l1 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
5) "d"
6) "c"
7) "b"

lpop从左边弹出集合元素

127.0.0.1:6379> lpop l1
"4"
127.0.0.1:6379> LRANGE l1 0 -1
1) "3"
2) "2"
3) "1"
4) "d"
5) "c"
6) "b"

llen查看元素个数

127.0.0.1:6379> llen l1
(integer) 6

lpushx 集合头部(左边)插入元素单个或多个元素

127.0.0.1:6379> lpushx l1 4
(integer) 7
127.0.0.1:6379> LRANGE l1 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
5) "d"
6) "c"
7) "b"
127.0.0.1:6379> LPUSH l1 5 6
(integer) 9
127.0.0.1:6379> LRANGE l1 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
6) "1"
7) "d"
8) "c"
9) "b"

rpush 集合尾部(右端)插入元素

127.0.0.1:6379> RPUSH l1 e f
(integer) 11
127.0.0.1:6379> LRANGE l1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "3"
 5) "2"
 6) "1"
 7) "d"
 8) "c"
 9) "b"
10) "e"
11) "f"

rpushx 集合尾部(右端)插入一个元素(只能插入一个)

127.0.0.1:6379> rpushx l1 i
(integer) 12
127.0.0.1:6379> LRANGE l1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "3"
 5) "2"
 6) "1"
 7) "d"
 8) "c"
 9) "b"
10) "e"
11) "f"
12) "i"

从制定方向产出指定元素的个数:

127.0.0.1:6379> LREM l1 1 1
(integer) 1
127.0.0.1:6379> LRANGE l1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "3"
 5) "2"
 6) "d"
 7) "c"
 8) "b"
 9) "e"
10) "f"
11) "i"
127.0.0.1:6379> RPUSH l1 2 3 4 5 3 3
(integer) 17
127.0.0.1:6379> LRANGE l1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "3"
 5) "2"
 6) "d"
 7) "c"
 8) "b"
 9) "e"
10) "f"
11) "i"
12) "2"
13) "3"
14) "4"
15) "5"
16) "3"
17) "3"
127.0.0.1:6379> LREM l1 3 3
(integer) 3
127.0.0.1:6379> LRANGE l1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "2"
 5) "d"
 6) "c"
 7) "b"
 8) "e"
 9) "f"
10) "i"
11) "2"
12) "4"
13) "5"
14) "3"

上面是从左边删,下面从右边删除元素,可理解为从左往右删为正,反之为负:

127.0.0.1:6379> LREM l1 -2 3
(integer) 1
127.0.0.1:6379> LRANGE l1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "2"
 5) "d"
 6) "c"
 7) "b"
 8) "e"
 9) "f"
10) "i"
11) "2"
12) "4"
13) "5"

删除指定某个元素的所以值:

127.0.0.1:6379> LRANGE l1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "2"
 5) "d"
 6) "c"
 7) "b"
 8) "e"
 9) "f"
10) "i"
11) "2"
12) "4"
13) "2"
127.0.0.1:6379> LREM l1 0 2
(integer) 3
127.0.0.1:6379> LRANGE l1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "d"
 5) "c"
 6) "b"
 7) "e"
 8) "f"
 9) "i"
10) "4"


lset设置指定索引的值,索引从0开始:

127.0.0.1:6379> LSET l1 3 xxxx
OK
127.0.0.1:6379> LRANGE l1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "xxxx"
 5) "c"
 6) "b"
 7) "e"
 8) "f"
 9) "i"
10) "4"

insert 在集合里之前和之后插入指定元素:

127.0.0.1:6379> LRANGE l1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "qqqq"
 5) "xxxx"
 6) "c"
 7) "b"
 8) "e"
 9) "f"
10) "i"
11) "4"
127.0.0.1:6379> linsert l1 after xxxx qqqq
(integer) 12
127.0.0.1:6379> LRANGE l1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "qqqq"
 5) "xxxx"
 6) "qqqq"
 7) "c"
 8) "b"
 9) "e"
10) "f"
11) "i"
12) "4"

rpoplpush把a集合尾部元素弹出插入到b集合头部

127.0.0.1:6379> LRANGE l1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "qqqq"
 5) "xxxx"
 6) "qqqq"
 7) "c"
 8) "b"
 9) "e"
10) "f"
11) "i"
12) "4"
127.0.0.1:6379> LRANGE l1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "qqqq"
 5) "xxxx"
 6) "qqqq"
 7) "c"
 8) "b"
 9) "e"
10) "f"
11) "i"
12) "4"
127.0.0.1:6379> LRANGE l2 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "i"
127.0.0.1:6379> RPOPLPUSH l1 l2
"4"
127.0.0.1:6379> LRANGE l1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "qqqq"
 5) "xxxx"
 6) "qqqq"
 7) "c"
 8) "b"
 9) "e"
10) "f"
11) "i"
127.0.0.1:6379> LRANGE l2 0 -1
1) "4"
2) "a"
3) "b"
4) "c"
5) "d"
6) "i"


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/106133.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 白嫖 IObit 系列软件例如 IObit Uninstaller

    白嫖 IObit 系列软件例如 IObit Uninstaller一、下载version.dllhttps://wws.lanzous.com/iFhkhm0g3qh二、将它复制到安装目录三、看到Pro即成功使用注意:右下角管理注册码别点,否则会闪退,闪退重启程序一样正常使用

    2022年10月20日
    0
  • 开心农场2激活成功教程版无限金币钥匙_开心农场2乡村度假内购激活成功教程版

    开心农场2激活成功教程版无限金币钥匙_开心农场2乡村度假内购激活成功教程版 最近开心农场非常火,同学用C#模拟鼠标点击操作做了一个小外挂,但是这样做有如下缺点:1、计算机不能做其他事情,2、必须开着浏览器,3、对所有好友点一遍的时间太慢,4、对于开发者来说技术含量低了点,呵呵。 所以我尝试着改进这种实现,我的想法是:不用开启浏览器,直接运行一个应用程序,该程序将自己伪装成一个浏览器,与服务器连接,并发送浇水、除虫等命令。这样,甚至可以使用多线程向服务器发送命令,无需…

    2022年9月13日
    0
  • RPC和REST的区别(转)

    RPC和REST的区别(转)

    2021年5月9日
    102
  • 基因突变基本知识「建议收藏」

    基因突变基本知识「建议收藏」基因、染色体、蛋白质、DNA,RNA之间的关系是什么?赛福基因公开课今天正式开讲。第一课我们来对基因检测进行基本的介绍。今天我主要从以下两个方面介绍一下基因检测的基础知识,一是基因,包括细胞、染

    2022年8月4日
    4
  • Flash与页面交互的钥匙之 AllowScriptAccess

    Flash与页面交互的钥匙之 AllowScriptAccess原文:http://cloud21.iteye.com/blog/729676今天为一个flash的问题搞了半天,flash在页面中点不开js的提示框,如果单是这一个问题,那我立刻就能确定问题所在,一

    2022年7月2日
    21
  • Django(24)永久重定向和临时重定向[通俗易懂]

    Django(24)永久重定向和临时重定向[通俗易懂]重定向重定向分为永久重定向和临时重定向,在页面上体现的操作就是浏览器会从一个页面自动跳转到另外一个页面。比如用户访问了一个需要权限的页面,但是该用户当前并没有登录,因此我们应该给他重定向到登录页面。

    2022年8月7日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号