大家好,又见面了,我是你们的朋友全栈君。
五中数据结构:字符串(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"
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106133.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...