当前位置:网站首页>第三章 : 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
边栏推荐
- Which big guy has the 11G GI and ojvm patches in April or January 2020, please help?
- STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)
- 手写柯里化 - toString 理解
- 歌词整理
- 自定义过滤器和拦截器实现ThreadLocal线程封闭
- 使用工具类把对象中的null值转换为空字符串(集合也可以使用)
- MySQL transactions
- 无题十四
- NowCoderTOP35-40——持续更新ing
- 华为轻量级神经网络架构GhostNet再升级,GPU上大显身手的G-GhostNet(IJCV22)
猜你喜欢

How can project cost control help project success?

dotnet OpenXML 解析 PPT 图表 面积图入门

2022华数杯数学建模A题环形振荡器的优化设计思路思路代码分享

数据中台建设(十):数据安全管理

Technical dry goods | Hausdorff distance for image segmentation based on MindSpore

Open Source Summer | How OpenHarmony Query Device Type (eTS)

一文道清什么是SPL

What is SPL?

Oracle temporary table space role

Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
随机推荐
three objects are arranged in a spherical shape around the circumference
无题六
Custom filters and interceptors implement ThreadLocal thread closure
告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来
IDEA执行Test操作导致数据插入时出现了重复数据
Bias lock/light lock/heavy lock lock is healthier. How is locking and unlocking accomplished?
Jenkins manual (2) - software configuration
偏向锁/轻量锁/重级锁锁锁更健康,上锁解锁到底是怎么完成实现的
手把手教你纯c实现异常捕获try-catch组件
What is SPL?
Technical dry goods | Hausdorff distance for image segmentation based on MindSpore
DFINITY 基金会创始人谈熊市沉浮,DeFi 项目该何去何从
js graphics operation one (compatible with pc, mobile terminal to achieve draggable attribute drag and drop effect)
无题二
2022 Huashu Cup Mathematical Modeling Question A Optimization Design Ideas for Ring Oscillators Code Sharing
开源一夏|OpenHarmony如何查询设备类型(eTS)
Brief Analysis of WSGI Protocol
Wei Dongshan Digital Photo Frame Project Learning (6) Transplantation of tslib
Seata source code analysis: initialization process of TM RM client
hcip BGP enhancement experiment