当前位置:网站首页>Redis:Redis的数据结构、key的操作命令
Redis:Redis的数据结构、key的操作命令
2022-07-03 13:29:00 【dengfengling999】
目录:
(1)Redis的5种数据结构
(2)key的操作命令1
(3)key的操作命令2
(4)key的操作命令3
(1)Redis的5种数据结构

1.字符串类型 string
字符串类型是Redis中最基本的数据结构,它能存储任何类型的数据,包括二进制数
据,序列化后的数据,JSON化的对象甚至是一张图片。最大512M。

2.列表类型 list
Redis列表是简单的字符串列表,按照插入顺序排序,元素可以重复。你可以添加一个元素到列表的头部(左边)或者尾部(右边),底层是个链表结构。

3.集合类型 set
Redis的Set是string类型的无序无重复集合。

4.哈希类型 hash
Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象。

5.有序集合类型 zset (sorted set)
Redis 有序集合zset和集合set一样也是string类型元素的集合,且不允许重复的成员。
不同的是zset的每个元素都会关联一个分数(分数可以重复),redis通过分数来为集合中的成员进行从小到大的排序。

Redis有一系列的命令来操作Redis的数据结构
(2)key的操作命令1

1.keys
语法:keys pattern
作用:查找所有符合模式pattern的key. pattern可以使用通配符。
通配符:
- *:表示0或多个字符,例如:keys * 查询所有的key。
- ?:表示单个字符,例如:wo?d , 匹配 word , wood
- [] :表示选择[]内的一个字符,例如wo[or]d, 匹配word, wood, 不匹配wold、woord

keys * :查询所有的key

keys k * :通配匹配,查询所有以k开头的key

keys h*o :查询所有以h开头,以o结尾的key

keys h?o :查询所有以h开头,o结尾,并且中间只有一个字符的key

keys h[abc]llo :查询所有以h开头,以llo结尾,并且h后面只能去abc中一个字符的key

(3)key的操作命令2

1.exists
语法:exists key[key…]
作用:判断key是否存在
返回值:整数,存在key返回1,其他返回0。使用多个key,返回存在的key的数量。

例如:返回0或1

返回存在个数:

Redis默认有16个库,有这种需求把一个数据移动到第二个库:(用的不多)
2.move
语法:move key db
作用:移动key到指定的数据库,移动的key在原库被删除。
返回值:移动成功返回1,失败返回0.

把k移动到2号库:

3.ttl
语法:ttl key
作用:查看key的剩余生存时间(ttl: time to live),以秒为单位。
返回值:
- -1 :没有设置key的生存时间, key永不过期。
- -2:key不存在

例如:2号库没有k1,返回-2,1号库有k1,返回-1

4.expire
语法:expire key seconds
作用:设置key的生存时间,超过时间,key自动删除。单位是秒。
返回值:设置成功返回数字 1,其他情况是 0 。

给k2设置存在时间:设置20秒超过时间删除

(3)key的操作命令3
1.type
语法:type key
作用:查看key所存储值的数据类型
返回值:字符串表示的数据类型
- none (key不存在)
- string (字符串)
- list (列表)
- set (集合)
- zset (有序集)
- hash (哈希表)

例如:

2.rename (key不推荐修改)
语法:rename key newkey
作用:将key改为名newkey。当 key 和 newkey 相同,或者 key 不存在时,返回一个错误。
当 newkey 已经存在时, RENAME 命令将覆盖旧值。

例如:重命名hello:

3.del
语法:del key [key…]
作用:删除存在的key,不存在的key忽略。
返回值:数字,删除的key的数量。

例如:不存在的忽略

边栏推荐
- Collection of mobile adaptation related articles
- Metal organic framework MOFs loaded with non steroidal anti-inflammatory drugs | zif-8 wrapped Prussian blue loaded quercetin (preparation method)
- QT learning 21 standard dialog box in QT (Part 2)
- Spring cup eight school league
- Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off
- Richview trvstyle liststyle list style (bullet number)
- Leetcode-1175. Prime Arrangements
- Qt学习17 对话框及其类型
- Doxorubicin loaded on metal organic framework MIL-88 DOX | folic acid modified uio-66-nh2 doxorubicin loaded [email
- Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
猜你喜欢

Comprehensively develop the main channel of digital economy and digital group, and actively promote the utonmos digital Tibet market

Dlopen() implements dynamic loading of third-party libraries

Ocean CMS vulnerability - search php

Use vscode to view hex or UTF-8 codes
![[développement technologique - 24]: caractéristiques des technologies de communication Internet des objets existantes](/img/f3/a219fe8e7438b8974d2226b4c3d4a4.png)
[développement technologique - 24]: caractéristiques des technologies de communication Internet des objets existantes

解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)

Qt学习18 登录对话框实例分析

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

Failure of vector insertion element iterator in STL

Installation impression notes
随机推荐
又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
Multi person collaborative data annotation based on Baidu brain easydata from scratch
Golang — 命令行工具cobra
MIL-100( Fe) 包裹小分子阿司匹林形成[email protected](Fe)|甘草次酸修饰金属有机框架材料UiO-66-NH2(简称UiO-66-NH2-GA)
Formation of mil-100 (FE) coated small molecule aspirin [email protected] (FE) | glycyrrhetinic acid modified metal organ
Sequence table (implemented in C language)
3D vision - 2 Introduction to pose estimation - openpose includes installation, compilation and use (single frame, real-time video)
Halcon combined with C # to detect surface defects -- Halcon routine autobahn
Go language web development series 25: Gin framework: using MD5 to verify the signature for the interface station
IBEM mathematical formula detection data set
GoLand 2021.2 configure go (go1.17.6)
Depth and breadth first traversal of tree (regardless of binary tree)
Qt学习19 Qt 中的标准对话框(上)
MySQL 数据处理值增删改
MySQL data processing value addition, deletion and modification
“又土又穷”的草根高校,凭什么被称为“东北小清华”?
解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
信创产业现状、分析与预测
可编程逻辑器件软件测试
Use vscode to view hex or UTF-8 codes