当前位置: 首页 > news >正文

Redis的公共操作命令

目录

    • 1.Key操作命令
      • 1.1 `keys *`
      • 1.2 `exists <key]>`
      • 1.3 `type <key>`
      • 1.4 `del <key>`
      • 1.5 `unlink <key>`
      • 1.6 `ttl <key>`
      • 1.7 `expire <key> <秒数>`
      • 1.8 `move <key> <index>`
    • 2.库操作命令
      • 2.1 `select <index>`
      • 2.2 `dbsize`
      • 2.3 `flushdb`
      • 2.4 `flushall`
    • 3.其他命令
      • 3.1 `help @<type>`

1.Key操作命令

Redis是Key-Value数据库,Key都是字符串且区分大小写,关于Redis的key操作,主要有常见的以下几个

Redis的命令是不区分大小写的

1.1 keys *

查看当前库所有的Key,类似于数据库的select * from tb_xxx

127.0.0.1:6379> keys *
1) "k1"
2) "k2"

1.2 exists <key]>

Key是否存在,返回bool,1代表true,0代表false

127.0.0.1:6379> exists k1
(integer) 1
127.0.0.1:6379> exists k2
(integer) 1
127.0.0.1:6379> exists k3
(integer) 0

Redis的底层使用C语言实现,很多命令返回bool时,多用0和1表示

1.3 type <key>

key对应的value是什么类型

127.0.0.1:6379> type k1
string

1.4 del <key>

删除数据,返回bool

127.0.0.1:6379> del k2
(integer) 1

1.5 unlink <key>

非阻塞删除,仅仅将key从keyspace元数据中删除,真正的数据删除将在后续异步进行,返回bool

127.0.0.1:6379> unlink k1
(integer) 1

1.6 ttl <key>

查看key还有多少秒过期,-1代表永不过期,-2代表已过期,通常和expire命令搭配使用

127.0.0.1:6379> ttl k1
(integer) -1

1.7 expire <key> <秒数>

为指定的key设置过期时间

127.0.0.1:6379> expire k1 100
(integer) 1
127.0.0.1:6379> ttl k1
(integer) 90
127.0.0.1:6379> ttl k1
(integer) 86

1.8 move <key> <index>

将当前key移动到指定的数据库中,返回bool

127.0.0.1:6379> move k1 2
(integer) 1

2.库操作命令

2.1 select <index>

选中几号仓库。redis.conf配置文件默认Redis共16个数据库(0-15),默认选中0号库

127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> select 3
OK
127.0.0.1:6379[3]> 

2.2 dbsize

查看当前库有多少key

127.0.0.1:6379[3]> dbsize
(integer) 0
127.0.0.1:6379[3]> set k1 v1
OK
127.0.0.1:6379[3]> dbsize
(integer) 1
127.0.0.1:6379[3]> 

2.3 flushdb

清空当前库中的所有key

127.0.0.1:6379> flushdb
OK

2.4 flushall

清空整个Redis中的所有key

127.0.0.1:6379> flushall
OK

3.其他命令

3.1 help @<type>

命令行下输入help @<type>命令,redis服务器会返回该数据类型的所有用法

127.0.0.1:6379> help @string

APPEND key value
summary: Appends a string to the value of a key. Creates the key if it doesn't exist.
since: 2.0.0

DECR key
summary: Decrements the integer value of a key by one. Uses 0 as initial value if the key doesn't exist.
since: 1.0.0

DECRBY key decrement
summary: Decrements a number from the integer value of a key. Uses 0 as initial value if the key doesn't exist.
since: 1.0.0

GET key
summary: Returns the string value of a key.
since: 1.0.0

GETDEL key
summary: Returns the string value of a key after deleting the key.
since: 6.2.0

GETEX key [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|PERSIST]
summary: Returns the string value of a key after setting its expiration time.
since: 6.2.0

GETRANGE key start end
summary: Returns a substring of the string stored at a key.
since: 2.4.0

GETSET key value
summary: Returns the previous string value of a key after setting it to a new value.
since: 1.0.0

INCR key
summary: Increments the integer value of a key by one. Uses 0 as initial value if the key doesn't exist.
since: 1.0.0

INCRBY key increment
summary: Increments the integer value of a key by a number. Uses 0 as initial value if the key doesn't exist.
since: 1.0.0

INCRBYFLOAT key increment
summary: Increment the floating point value of a key by a number. Uses 0 as initial value if the key doesn't exist.
since: 2.6.0

LCS key1 key2 [LEN] [IDX] [MINMATCHLEN min-match-len] [WITHMATCHLEN]
summary: Finds the longest common substring.
since: 7.0.0

MGET key [key ...]
summary: Atomically returns the string values of one or more keys.
since: 1.0.0

MSET key value [key value ...]
summary: Atomically creates or modifies the string values of one or more keys.
since: 1.0.1

MSETNX key value [key value ...]
summary: Atomically modifies the string values of one or more keys only when all keys don't exist.
since: 1.0.1

PSETEX key milliseconds value
summary: Sets both string value and expiration time in milliseconds of a key. The key is created if it doesn't exist.
since: 2.6.0

SET key value [NX|XX] [GET] [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|KEEPTTL]
summary: Sets the string value of a key, ignoring its type. The key is created if it doesn't exist.
since: 1.0.0

SETEX key seconds value
summary: Sets the string value and expiration time of a key. Creates the key if it doesn't exist.
since: 2.0.0

SETNX key value
summary: Set the string value of a key only when the key doesn't exist.
since: 1.0.0

SETRANGE key offset value
summary: Overwrites a part of a string value with another by an offset. Creates the key if it doesn't exist.
since: 2.2.0

STRLEN key
summary: Returns the length of a string value.
since: 2.2.0

SUBSTR key start end
summary: Returns a substring from a string value.
since: 1.0.0

127.0.0.1:6379> 

http://www.dtcms.com/a/113073.html

相关文章:

  • 探秘AI(003)之“通义AI”全栈AI能力引领智能化变革
  • 2025-04-05 吴恩达机器学习5——逻辑回归(2):过拟合与正则化
  • 安装gpu版本的dgl
  • Python解决“组成字符串ku的最大次数”问题
  • Airflow+Spark/Flink vs. Kettle
  • (一)前端程序员转安卓开发分析和规划建议
  • Dify票据识别遇到的分支判断不准确问题
  • 破解GenAI时代工业物联网落地难题:研华IoTSuite如何用“全栈技术“重构智造未来?
  • Roo Code使用MCP服务(大模型上下文协议)
  • 深度学习处理文本(13)
  • SSL证书过期会有什么影响
  • 奈氏准则和 香农定理
  • netty中的ServerBootstrap详解
  • thinkphp8.0上传图片到阿里云对象存储(oss)
  • 2025全新开源双端系统源码:获取通讯录、相册、短信、定位及已装应用信息
  • 程序环境和预处理
  • 第二章日志分析-redis应急响应笔记
  • 贪心算法的使用条件
  • 通义灵码:引领 AI 驱动的编程革命
  • 趣味逆商测试:了解你的逆境应对能力
  • 系统思考:思考的快与慢
  • 二叉树的前序中序后序遍历
  • DeFi漏洞利用与安全防护
  • Oracle数据库数据编程SQL<8 文本编辑器Notepad++和UltraEdit(UE)对比>
  • Python 变量
  • JVM虚拟机篇(二):深入剖析Java与元空间(MetaSpace)
  • 31信号和槽_信号和槽存在的意义(1)
  • bge-m3+deepseek-v2-16b+离线语音能力实现离线文档向量化问答语音版
  • AI绘画中的LoRa是什么?
  • Maven 远程仓库推送方法