Table of Contents
1 简单概述
2 操作命令
Home Database Mysql Tutorial REDIS学习笔记之STRING数据类型

REDIS学习笔记之STRING数据类型

Jun 07, 2016 pm 04:04 PM
redis string author study data notes type

作者:邹祁峰 邮箱:Qifeng.zou.job@gmail.com 博客:http://blog.csdn.net/qifengzou 日期:2014.10.11 转载请注明来自祁峰的CSDN博客 1 简单概述 String数据类型是Redis中最简单、最基础的数据类型,Redis中所有的KEY和VALUE都是String类型,且其他更为复杂

作者:邹祁峰
邮箱:Qifeng.zou.job@gmail.com
博客:http://blog.csdn.net/qifengzou
日期:2014.10.11
转载请注明来自"祁峰"的CSDN博客

1 简单概述

String数据类型是Redis中最简单、最基础的数据类型,Redis中所有的KEY和VALUE都是String类型,且其他更为复杂的数据类型(LISTS、SETS、SORTED-SETS、HASHES)也均基于String数据类型来存储的。String数据类型是二进制安全的,也就是说String可以包含任何格式的数据,比如:JPG图片、序列化的对象等。

2 操作命令

表1 String类型操作命令 

01 APPEND key value
  功能描述:将KEY对应的VALUE值减1
返回结果:APPEND操作之后,KEY对应VALUE的最终长度
注意事项:
1)时间复杂度:O(1)
2)当KEY存在,且为STRING类型时,将会将value追加到原有值末尾;
3)如果KEY不存在,将会新建KEY/VALUE。
02 DECR key
  功能描述:将KEY对应的VALUE原子性的减1
返回结果:返回执行减1操作后KEY对应的VALUE值
注意事项:
1)时间复杂度:O(1)
2)如果KEY对应VALUE能转化为数字,则执行减1操作;
3)如果KEY对应VALUE不能转化为数字,则返回错误信息;
4)该操作是原子性的。假设:当前KEY对应的VALUE值为100,当3个客户端同时执行该操作时,最终结果将会是97。
03 INCR key
  功能描述:将KEY对应的VALUE原子性的加1
返回结果:返回执行加1操作后KEY对应的VALUE值
注意事项:
1)时间复杂度:O(1)
2)如果KEY对应VALUE能转化为数字,则执行加1操作;
3)如果KEY对应VALUE不能转化为数字,则返回错误信息;
4)该操作是原子性的。假设:当前KEY对应的VALUE值为100,当3个客户端同时执行该操作时,最终结果将会是103。
04 DECRBY key decremen
  功能描述:将KEY对应的VALUE原子性的减decrement
返回结果:返回执行减操作后KEY对应的VALUE值
注意事项:
1)时间复杂度:O(1)
2)如果KEY对应VALUE能转化为数字,则执行减操作;
3)如果KEY对应VALUE不能转化为数字,则返回错误信息;
4)参数decrement不能为浮点数,只能为整数;
5)该操作是原子性的。
05 INCRBY key increment
  功能描述:将KEY对应的VALUE原子性的加increment
返回结果:返回执行加操作后KEY对应的VALUE值
注意事项:
1)时间复杂度:O(1)
2)如果KEY对应VALUE能转化为数字,则执行加操作;
3)如果KEY对应VALUE不能转化为数字,则返回错误信息;
4)参数increment不能为浮点数,只能为整数;
5)该操作是原子性的。
06 INCRBYFLOAT key increment
  功能描述:将KEY对应的VALUE原子性的加increment
返回结果:返回执行加操作后KEY对应的VALUE值
注意事项:
1)时间复杂度:O(1)
2)如果KEY对应VALUE能转化为数字,则执行加操作;
3)如果KEY对应VALUE不能转化为数字,则返回错误信息;
4)此命令的参数increment可以为浮点数,但是请注意此命令的计算精度;
5)暂时没有命令DECRBYFLOAT,如果想进行浮点数的减操作,可以模仿:INCRBYFLOAT key -5.9;
6)该操作是原子性的。
07 SET key value [EX seconds] [PX milliseconds] [NX|XX]
  功能描述:设置KEY对应的VALUE
参数说明:
从 Redis 2.6.12 版本开始, SET 命令的行为可以通过一系列参数来修改:
1)EX second:设置键的过期时间为second秒
SET key value EX second 效果等同于 SETEX key second value。
2)PX millisecond:设置键的过期时间为millisecond毫秒
SET key value PX millisecond 效果等同于 PSETEX key millisecond value。
3)NX :只在键不存在时,才对键进行设置操作
SET key value NX 效果等同于 SETNX key value
4)XX :只在键已经存在时,才对键进行设置操作。
返回结果:
1)在2.6.12版本之前,总是返回OK;
2)在2.6.12版本开始,SET操作完成时才返回OK;
3)如果设置了 NX 或者 XX ,但因为条件没达到而造成设置操作未执行,那么命令返回空批量回复(NULL Bulk Reply)。
注意事项:
1)时间复杂度:O(1)
2)如果KEY已经存在,此操作将直接覆盖旧值,且无视类型;
3)对于某个原本带有生存时间(TTL)的键来说, 当 SET 命令成功在这个键上执行时, 这个键原有的 TTL 将被清除。
08 GET key
  功能描述:获取指定KEY的VALUE
返回结果:指定KEY的VALUE
注意事项:
1)时间复杂度:O(1)
2)如果该KEY不存在,返回NIL;
3)如果该KEY不是String类型,将返回错误信息。
09 GETSET key value
  功能描述:原子性的设置KEY的VALUE,同时获取该KEY的原VALUE
返回结果:该KEY的原VALUE
注意事项:
1)时间复杂度:O(1)
2)如果该KEY不存在,将新建KEY,并返回NIL;
3)如果该KEY不是String类型,将返回错误信息。
10 STRLEN key
  功能描述:获取该KEY的VALUE长度
返回结果:该KEY的VALUE长度
注意事项:
1)时间复杂度:O(1)
2)如果该KEY不存在,将返回0;
3)如果该KEY不是String类型,将返回错误信息。
11 SETEX key seconds value
  功能描述:设置KEY对应值为VALUE,同时设置该KEY的生存时间为SECONDS秒
返回结果:操作完成时才返回OK
注意事项:
1)时间复杂度:O(1)
2)如果KEY已经存在,此操作将直接覆盖旧值;
3)该命令类似如下两条命令:
SET key value
EXPIRE key seconds
不同的是,SETEX是原子性的。关联值和设置生存时间两个动作会在同一时间内完成,该命令在 Redis 用作缓存时,非常实用;
4)当 seconds 参数不合法时,返回错误。
12 SETNX key value
  功能描述:当前仅当KEY不存在时,设置KEY对应值为VALUE
返回结果:设置成功返回1,设置失败返回0
注意事项:
1)时间复杂度:O(1)
2)将 key 的值设为 value ,当且仅当 key 不存在;
3)若给定的 key 已经存在,则 SETNX 不做任何动作;
4)SETNX 是"SET if Not eXists"(如果不存在,则 SET)的简写。
13 SETBIT key offset value
  功能描述:对KEY所储存的字符串值,设置或清除指定偏移量上的位(bit)
返回结果:指定偏移量原来储存的位
注意事项:
1)时间复杂度:O(1)
2)位的设置或清除取决于 value 参数,可以是 0 也可以是 1;
3)当 key 不存在时,自动生成一个新的字符串值;
4)字符串会进行伸展(grown)以确保它可以将 value 保存在指定的偏移量上。当字符串值进行伸展时,空白位置以 0 填充;
5)offset 参数必须大于或等于 0 ,小于 2^32 (bit 映射被限制在 512 MB 之内);
6)对使用大的 offsetSETBIT操作来说,内存分配可能造成 Redis 服务器被阻塞。具体参考SETRANGE 命令,warning(警告)部分。
14 GETBIT key offset
  功能描述:对 key 所储存的字符串值,获取指定偏移量上的位(bit)
返回结果:指定偏移量上的位
注意事项:
1)时间复杂度:O(1)
2)当 offset 比字符串值的长度大,或者 key 不存在时,返回 0 ;
3)当KEY不存在时,进行GETBIT操作将返回0。
15 SETRANGE key offset value
  功能描述:用 value 参数覆写(overwrite)给定 key 所储存的字符串值,从偏移量 offset 开始
返回结果:修改后的字符串长度
注意事项:
1)时间复杂度:对小的字符串,平摊复杂度O(1)。(关于什么字符串是”小”的,请参考 APPEND 命令) 否则为O(M), M 为 value 参数的长度
2)不存在的 key,则将其原值当作空白字符串处理;
3)SETRANGE 命令会确保字符串足够长以便将 value 设置在指定的偏移量上,如果给定 key 原来储存的字符串长度比偏移量小(比如字符串只有 5 个字符长,但你设置的 offset 是 10 ),那么原字符和偏移量之间的空白将用零字节(zerobytes, "\x00" )来填充;
4)注意你能使用的最大偏移量是 2^29-1(536870911) ,因为 Redis 字符串的大小被限制在 512 兆(megabytes)以内。如果你需要使用比这更大的空间,你可以使用多个 key ;
5)当生成一个很长的字符串时,Redis 需要分配内存空间,该操作有时候可能会造成服务器阻塞(block)。在2010年的Macbook Pro上,设置偏移量为 536870911(512MB 内存分配)耗费约 300 毫秒, 设置偏移量为 134217728(128MB 内存分配),耗费约 80 毫秒,设置偏移量 33554432(32MB 内存分配),耗费约 30 毫秒,设置偏移量为 8388608(8MB 内存分配),耗费约 8 毫秒。 注意若首次内存分配成功之后,再对同一个 key 调用 SETRANGE 操作,无须再重新内存。
16 GETRANGE key start end
  功能描述:截取 key 对应字串值的子串
返回结果:截取得出的子串
注意事项:
1)时间复杂度:O(N)(N表示要返回的字串长度)
2)字符串的截取范围由 start 和 end 两个偏移量决定(包括 start 和 end 在内);
3)负数偏移量表示从字符串最后开始计数, -1 表示最后一个字符, -2 表示倒数第二个,以此类推;
4)GETRANGE 通过保证子字符串的值域(range)不超过实际字符串的值域来处理超出范围的值域请求;
5)在
17 MSET key value [key value ...]
  功能描述:同时设置1个或多个KEY-VALUE对
返回结果:总是返回 OK (因为 MSET 不可能失败)
注意事项:
1)时间复杂度:O(N)(N表示 key-value对的数量)
2)如果某个给定 key 已经存在,那么 MSET 会用新值覆盖原来的旧值,如果这不是你所希望的效果,请考虑使用 MSETNX 命令:它只会在所有给定 key 都不存在的情况下进行设置操作;
3)MSET 是一个原子性(atomic)操作,所有给定 key 都会在同一时间内被设置,某些给定 key 被更新而另一些给定 key 没有改变的情况,不可能发生。
18 MSETNX key value [key value ...]
  功能描述:同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在
返回结果:成功返回1,失败返回0
注意事项:
1)时间复杂度:O(N)(N表示 key-value对的数量)
2)即使只有一个给定 key 已存在, MSETNX 也会拒绝执行所有给定 key 的设置操作;
3)MSETNX 是原子性的,因此它可以用作设置多个不同 key 表示不同字段(field)的唯一性逻辑对象(unique logic object),所有字段要么全被设置,要么全不被设置。
19 MGET key [key ...]
  功能描述:返回所有(一个或多个)给定 key 的值
返回结果:一个包含所有给定 key 的值的列表
注意事项:
1)时间复杂度:O(N)(N表示 key-value对的数量)
2)如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil 。因此,该命令永不失败。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to use redis lock How to use redis lock Apr 10, 2025 pm 08:39 PM

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to solve data loss with redis How to solve data loss with redis Apr 10, 2025 pm 08:24 PM

Redis data loss causes include memory failures, power outages, human errors, and hardware failures. The solutions are: 1. Store data to disk with RDB or AOF persistence; 2. Copy to multiple servers for high availability; 3. HA with Redis Sentinel or Redis Cluster; 4. Create snapshots to back up data; 5. Implement best practices such as persistence, replication, snapshots, monitoring, and security measures.

How to use the redis command line How to use the redis command line Apr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

See all articles