当前位置:网站首页>第三章 : 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
边栏推荐
- The difference between find, matches, lookingAt matching strings in matcher
- Offensive World-PWN-new_easypwn
- Why are RELTABLESPACE values 0 for many tables displayed in sys_class?
- Is digital transformation a business buy-in?
- 华为轻量级神经网络架构GhostNet再升级,GPU上大显身手的G-GhostNet(IJCV22)
- 百年北欧奢华家电品牌ASKO智能三温区酒柜臻献七夕,共品珍馐爱意
- [Unity] [UGUI] [Display text on the screen]
- IO流篇 -- 基于io流实现文件夹拷贝(拷贝子文件夹及子文件夹内文件)满满的干货
- IDEA执行Test操作导致数据插入时出现了重复数据
- Oracle临时表空间作用
猜你喜欢

IDEA执行Test操作导致数据插入时出现了重复数据

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

Egg framework usage (2)

Marketing Suggestions | You have an August marketing calendar to check! Suggest a collection!

Oracle temporary table space role

The technological achievements of Shanghai Konan were selected into the "2021 Shanghai Network Security Industry Innovation Research Achievement Catalog" by the Municipal Commission of Economy and Inf

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

语音社交软件开发——充分发挥其价值

How can project cost control help project success?

dotnet OpenXML parsing PPT charts Getting started with area charts
随机推荐
static linking and dynamic linking
three.js debugging tool dat.gui use
CPU的亲缘性affinity
[Office] Collection of Microsoft Office download addresses (offline installation and download of Microsoft's official original version)
电竞、便捷、高效、安全,盘点OriginOS功能的关键词
基于MindSpore高效完成图像分割,实现Dice!
How ali cloud storage database automatically to speed up the loading speed of www.cxsdkt.cn how to set up the case?
无题四
【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样
Marketing Suggestions | You have an August marketing calendar to check! Suggest a collection!
Advanced usage of C language
egg框架使用(二)
Pycharm 常用外部工具
The difference between find, matches, lookingAt matching strings in matcher
js劫持数组push方法
[Strong Net Cup 2022] WP-UM
韦东山 数码相框 项目学习(六)tslib的移植
Redis源码解析:Redis Cluster
还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!
js hijacks the array push method