【Redis】数据介绍 通用命令 String类型

阅读: 评论:0

【Redis】数据介绍  通用命令  String类型

【Redis】数据介绍 通用命令 String类型

文章目录

  • 一、Redis数据结构介绍
    • 1.1 Redis的5种数据结构
      • A、字符串类型string
      • B、列表类型 list
      • C、集合类型 set
      • D、 哈希类型 hash
      • E、zset(sorted set)
  • 二、Redis的通用命令
    • 2.1 KEYS
    • 2.2 DEL
    • 2.3 EXISTS
    • 2.4 EXPIRE & TTL
  • 三、String类型
    • 3.1 SET & GET & GETSET
    • 3.2 MSET & MGET
    • 3.3 INCR & INCRBY & INCRBYFLOAT
    • 3.4 SETNX & SETEX & MSETNX & PSETEX & GETEX
    • 3.6 SETRANGE & GETRANGE & SUBSTR
    • 3.7 GETDEL
    • 3.8 STRLEN
    • 3.9 APPEND
    • 3.10 LCS
  • 补充


一、Redis数据结构介绍

Redis是一个Key-Value的数据库,key一般是String类型,不过value的类型多种多样:

1.1 Redis的5种数据结构

A、字符串类型string

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


B、列表类型 list

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


C、集合类型 set

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


D、 哈希类型 hash

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


E、zset(sorted set)

Redis 有序集合zset和集合set一样也是string类型元素的集合,且不允许重复的成员。

不同的是zset的每个元素都会关联一个分数(分数可以重复),redis通过分数来为集合中的成员进行从小到大的排序。

返回顶部


二、Redis的通用命令

基本的通用命令如上图所示,下面进行详细介绍!🐌🐌🐌

2.1 KEYS

Supported glob-style patterns:

  • h?llo matches hello, hallo and hxllo
  • h*llo matches hllo and heeeello
  • h[ae]llo matches hello and hallo, but not hillo
  • h[^e]llo matches hallo, hbllo, … but not hello
  • h[a-b]llo matches hallo and hbllo

Use to escape special characters if you want to match them verbatim.

返回值:

Array reply: list of keys matching pattern.

查看符合模板的所有key,底层是类似于模糊查询的实现,容易导致阻塞,不建议在生产环境设备上使用!

返回顶部


2.2 DEL

Integer reply: The number of keys that were removed. 返回删除的key的个数

删除的时候有则删除,没有则不进行操作,返回的值为真实存在且被删除的key的个数。

返回顶部


2.3 EXISTS

Integer reply, specifically the number of keys that exist from those specified as arguments. 判断并返回指定key存在的个数

返回顶部


2.4 EXPIRE & TTL

EXPIRE:给key设置一个有效期,有效期到期后会自动删除该key

TTL:查看一个key的剩余有效期

当TTL返回值为 -2 的时候,表示这个key已经不存在了~

当TTL回值为 -1 的时候,表示这个key永久有效~

返回顶部


三、String类型

字符串类型是Redis中最基本的数据类型,它能存储任何形式的字符串,包括二进制数据、序列化后的数据、JSON化的对象甚至是一张图片。

字符串类型的数据操作总的思想是通过key操作value,key是数据标识,value是我们感兴趣的业务数据。根据字符串的格式不同,我们可以将其分为三类:

  • string:普通字符串
  • int:整数类型,可以做自增、自减操作
  • float:浮点类型,可以做自增、自减操作

底层嗾使按字节数组的形式存储,只不过是编码方式不同,最大空间不能超过512M

基本的通用命令如上图所示,下面进行详细介绍!🐌🐌🐌


3.1 SET & GET & GETSET


Options

The SET command supports a set of options that modify its behavior:

  • EX seconds – Set the specified expire time, in seconds.
  • PX milliseconds – Set the specified expire time, in milliseconds.
  • EXAT timestamp-seconds – Set the specified Unix time at which the key will expire, in seconds.
  • PXAT timestamp-milliseconds – Set the specified Unix time at which the key will expire, in milliseconds.
  • NX – Only set the key if it does not already exist.
  • XX – Only set the key if it already exist.
  • KEEPTTL – Retain the time to live associated with the key.
  • GET – Return the old string stored at key, or nil if key did not exist. An error is returned and SET aborted if the value stored at key is not a string.

Note: Since the SET command options can replace SETNX, SETEX, PSETEX, GETSET, it is possible that in future versions of Redis these commands will be deprecated and finally removed.


Return

Simple string reply: OK if SET was executed correctly.

Null reply: (nil) if the SET operation was not performed because the user specified the NX or XX option but the condition was not met.

If the command is issued with the GET option, the above does not apply. It will instead reply as follows, regardless if the SET was actually performed:

Bulk string reply: the old string value stored at key.

Null reply: (nil) if the key did not exist.


Return

Bulk string reply: the value of key, or nil when key does not exist.


简单地说:SET-添加或修改已经存在的一个String类型的键值对;GET-根据key获取String类型的value。


GETSET

Bulk string reply: the old value stored at key, or nil when key did not exist.

GETSET可以与INCR一起用于原子重置计数。例如:每当发生某个事件时,一个进程可能会对键mycounter调用INCR,但我们有时需要获取计数器的值并将其原子重置为零。

返回顶部


3.2 MSET & MGET

set、get的批处理命令

返回顶部


3.3 INCR & INCRBY & INCRBYFLOAT

INCR:让一个整型的key自增1

INCRBY:让一个整型的key自增指定步长

INCRBYFLOAT:让一个浮点类型的数字自增并指定步长

相反的是DECR、DECRBY(或者正增长负数)

返回顶部


3.4 SETNX & SETEX & MSETNX & PSETEX & GETEX

只有当key不存在的时候才执行增加!

  • 其实NX就是set的一个参数,组合了一下

  • 同理SETEXsetexpire的组合,将设置键值和有效期结合了一下

  • MSETNX就是SETNX的批处理

  • PSETEX的工作方式与SETEX完全相同,唯一的区别是过期时间是以毫秒而不是秒为单位指定的。


GETEX:

获取键的值的时候,并可以选择设置其过期时间。

Options

The GETEX command supports a set of options that modify its behavior:

  • EX seconds – Set the specified expire time, in seconds.
  • PX milliseconds – Set the specified expire time, in milliseconds.
  • EXAT timestamp-seconds – Set the specified Unix time at which the key will expire, in seconds.
  • PXAT timestamp-milliseconds – Set the specified Unix time at which the key will expire, in milliseconds.
  • PERSIST – Remove the time to live associated with the key.

Return

Bulk string reply: the value of key, or nil when key does not exist.

  • 与SETEX的区别在于设置有效期限的时间节点

返回顶部


3.6 SETRANGE & GETRANGE & SUBSTR

SETRANGE

覆盖存储在键处的字符串的一部分,从指定的偏移量开始,覆盖整个值长度。如果偏移量大于键处字符串的当前长度,则该字符串将填充零字节以使偏移量适合。不存在的键被视为空字符串,因此此命令将确保它保存的字符串足够大,以便能够在偏移处设置值。

请注意,您可以设置的最大偏移量为 2^29-1(536870911),因为Redis字符串限制为512 MB。如果需要超出此大小,可以使用多个键。

  • 当key并没有预先设置的时候,正常执行,确实部分0填充


GETRANGE

返回存储在键处的字符串值的子字符串,该值由偏移量开始和结束(包括两者)确定。可以使用负偏移量来提供从字符串末尾开始的偏移量。So -1表示最后一个字符-2表示倒数第二个字符,依此类推。

  • 该函数通过将结果范围限制为字符串的实际长度来处理超出范围的请求。
  • As of Redis version 2.0.0, SUBSTR is regarded as deprecated.It can be replaced by GETRANGE when migrating or writing new code.

返回顶部


3.7 GETDEL

Get the value of key and delete the key. This command is similar to GET, except for the fact that it also deletes the key on success (if and only if the key’s value type is a string).

获取键的值并删除键。此命令类似于GET,只是它还可以在成功时删除键(当且仅当键的值类型为字符串时)。

有种过河拆桥的感觉~


3.8 STRLEN

返回键处存储的字符串值的长度。当键包含非字符串值时,将返回错误。

返回顶部


3.9 APPEND

如果键已经存在并且是字符串,则此命令会将值附加到字符串的末尾。若键不存在,则创建该键并将其设置为空字符串,所以APPEND将类似于此特殊情况下的set。

返回顶部


3.10 LCS

Available since: 7.0.0

LCS命令实现最长的通用子序列算法。请注意,这与最长通用字符串算法不同,因为字符串中的匹配字符不需要是连续的。

例如,“foo”和“fao”之间的LCS是“fo”,因为从左到右扫描两个字符串,所以最长的公共字符集由第一个“f”和第二个“o”组成。

为了评估两个字符串的相似程度,LCS非常有用。字符串可以表示很多东西。例如,如果两个字符串是DNA序列,LCS将提供两个DNA序列之间的相似性度量。如果字符串表示某个用户编辑的某些文本,则LCS可以表示新文本与旧文本相比的不同程度,依此类推。

请注意,此算法以O(N*M)时间运行,其中N是第一个字符串的长度,M是第二个字符串的长度。

返回顶部



补充

常见的所有命令可以在命令行客户端通过 help @xxx 进行查询:

127.0.0.1:6379> help
redis-cli 6.2.6
To get help about Redis commands type:"help @<group>" to get a list of commands in <group>"help <command>" for help on <command>"help <tab>" to get a list of possible help topics"quit" to exitTo set redis-cli preferences:":set hints" enable online hints":set nohints" disable online hints
Set your preferences in ~/.redisclirc

也可以查看官网:/


本文发布于:2024-01-31 07:16:16,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170665657626578.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:命令   类型   数据   Redis   String
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23