当前位置:网站首页>第三章 : redis数据结构种类
第三章 : redis数据结构种类
2022-08-05 10:04:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
五中数据结构:字符串(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 rangedecr 自减,同自增
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) 2hash设置自增和自增量:
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) 26hash 判断是否存在某元素,返回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) 6lpushx 集合头部(左边)插入元素单个或多个元素
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.cn/106133.html原文链接:https://javaforall.cn
边栏推荐
- IDEA performs the Test operation, resulting in duplicate data when data is inserted
- STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)
- Tanabata romantic date without overtime, RPA robot helps you get the job done
- 攻防世界-PWN-new_easypwn
- 自定义过滤器和拦截器实现ThreadLocal线程封闭
- Four years of weight loss record
- PAT Class B-B1019 Digital Black Hole (20)
- 公众号如何运维?公众号运维专业团队
- Is digital transformation a business buy-in?
- What is the function of the regular expression replaceFirst() method?
猜你喜欢

手把手教你纯c实现异常捕获try-catch组件

技术干货 | 基于 MindSpore 实现图像分割之豪斯多夫距离

5.部署web项目到云服务器

创建一个 Dapp,为什么要选择波卡?

Jenkins manual (2) - software configuration

C语言的高级用法

一文道清什么是SPL

皕杰报表的下拉框联动

High-quality DeFi application building guide to help developers enjoy DeFi Summer

Bias lock/light lock/heavy lock lock is healthier. How is locking and unlocking accomplished?
随机推荐
长达四年的减肥记录
What is SPL?
Pycharm 常用外部工具
mysql进阶(二十七)数据库索引原理
STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)
2022华数杯数学建模A题环形振荡器的优化设计思路思路代码分享
Our Web3 Entrepreneurship Project, Yellow
无题一
韦东山 数码相框 项目学习(六)tslib的移植
[Strong Net Cup 2022] WP-UM
PAT Grade B-B1020 Mooncake(25)
静态链接和动态链接
QSS 选择器
The difference between find, matches, lookingAt matching strings in matcher
数据中台建设(十):数据安全管理
NowCoderTOP35-40——持续更新ing
[Office] Collection of Microsoft Office download addresses (offline installation and download of Microsoft's official original version)
Redis源码解析:Redis Cluster
无题十二
What is the function of the regular expression replaceAll() method?