search
HomeDatabaseRedisRedis data type learning: Let's talk about String principles

Redis data type learning: Let's talk about String principles

Jan 29, 2022 am 08:00 AM
redisstringStorage principletype of data

This article will take you to understand the String in the Redis data type and talk about the storage principle of the String data type. I hope it will be helpful to you!

Redis data type learning: Let's talk about String principles

#Redis is a middleware that is frequently used in work. It supports rich data structures, has extremely strong read and write performance, and tps can reach 100,000.

Today’s article analyzes and summarizes the String type, which is also one of the most used data structures. This article is analyzed based on redis5.0. [Related recommendations: Redis video tutorial]

1. Basic usage

set key value [EX seconds] [PX milliseconds] [NX|XX]

1. set is the syntax, key is the specified name, and value is Values ​​to be stored

2. EX specifies the expiration time in seconds, and PX specifies the expiration time in milliseconds

3. NX: The setting is successful only when the key does not exist

4, XX: The setting is successful only when the key exists

Summary: 5.0 supports the set command to specify the expiration time and the setting is successful only when it does not exist, that is, distributed lock addition can be achieved with one command For the lock function, setting the key and setting the expiration time in previous versions needed to be divided into two commands, making it more difficult to ensure atomicity.

2. Usage scenarios

1. Hotspot data cache, distributed session

2. Setnx Distributed lock

3, incr counter

4, Incr global id

5, Incr current limit

6, bit operation, bitmap Function, online user statistics 0/1 mark

3. Supported stored data types

Integer type, character type, float (single floating point type)

4. Different encoding types

Redis data type learning: Lets talk about String principles

Redis data type learning: Lets talk about String principles

## 5. String storage principle

In Redis, data is stored in a RedisObject class

typedef struct redisObject {    
//这个类型可以是string,也可以是hash,zset等等
unsigned type:4;    
unsigned encoding:4;    
//记录lru,lfu淘汰算法依赖的访问时间和访问频率    
unsigned lru:LRU_BITS; 
/* LRU time (relative to global lru_clock) or                            * LFU data (least significant 8 bits frequency                            * and most significant 16 bits access time). */
//引用计数器    
int refcount;    
//指向真实数据结构对象    
void *ptr;
} robj;

For String, Redis customizes a simple dynamic string data structure to store the string number.

Source code implementation: multiple data structures, each indicating that strings of different lengths can be stored.

Redis data type learning: Lets talk about String principles

len: represents the used length

alloc: the total allocated memory size

flags: represents the storage type

buf[]: Actual data

6. Differences in storage of three encodings

1. The RedisObject and SDS memory of embstr are in one piece, and they only need to be allocated once when creating

Memory , when destroyed releases the memory once , easy to find

2. Raw is RedisObject, and the SDS memory is not in the same place. When it needs to be created,

allocate memory twice , When destroyedRelease memory twice

3. The structure of embstr determines that when it needs to increase its length, RedisObject and SDS need to reallocate memory. Therefore, the data encoded by

embstr cannot be modified and is read-only.

7. When will int and embstr encoding be converted to raw

1. Int type data is no longer of int type and converted to raw

2. If the length is greater than 2^63-1, convert it to embstr

3. If the embstr character exceeds 44 bytes, convert it to raw

8. Advantages of SDS data structure

1,

Binary safe can store image shaping, floating point type

2, three encodings of String, make full use of memory and improve memory utilization

  • int Stores 8-byte long integer long, 2^63-1
  • Embstr SDS simple Dynamic String in embstr format. The memory space is continuous , read-only, as long as the modification is executed, it will be converted into raw
  • Raw, SDS, which stores strings larger than 44 bytes
3.

Don’t worry about memory overflow, sds has automatic expansion capability

4,

The time complexity of getting the string length is O(1), and the len attribute is stored

5. Prevent multiple allocations of memory through

space pre-allocation and lazy space release

6. Determine whether to end the use of the len attribute, which can contain '\0' ,operate strings.

9. Why not use the character array in c?

1. Memory needs to be allocated in advance, which may

memory overflow

2. Obtaining the length requires traversing the array,

time complexity O(n)

3. The length of the character array changes, requiring

memory reallocation

4. In the character array of c, '\0' represents the end of judgment.

Binary data storage is not safe, pictures, videos, etc. cannot be saved.

十、关于内存预分配特性

Redis data type learning: Lets talk about String principles

通过源码分析,扩容策略是字符串在长度小于 SDS_MAX_PREALLOC 之前,扩容空间采用加倍策略,也就是保留 100% 的冗余空间。当长度超过 SDS_MAX_PREALLOC 之后,为了避免加倍后的冗余空间过大而导致浪费,每次扩容只会多分配 SDS_MAX_PREALLOC大小的冗余空间。 

十一、关于惰性空间释放

惰性空间释放用于优化 SDS 的字符串缩短操作:当 SDS 的 API 需要缩短 SDS 保存的字符串时, 程序并不立即使用内存重分配来回收缩短后多出来的字节, 而是使用 free 属性将这些字节的数量记录起来,并等待将来使用。 

//仅仅设置长度,没有真正清除数据
void sdsclear(sds s) {    
//单纯设置长度为0    
sdssetlen(s, 0);    
//第一个字符设置为结束符    
s[0] = '\0';
}

真正的清除空间

sds sdsRemoveFreeSpace(sds s) 
{
struct sdshdr *sh;    
sh = (void*) (s-(sizeof(struct sdshdr)));    
// 进行内存重分配,让 buf 的长度仅仅足够保存字符串内容 
sh = zrealloc(sh, sizeof(struct sdshdr)+sh->len+1);    
// 空余空间为 0    
sh->free = 0;    
return sh->buf;
}

以上便是关于string的知识点记录,string的设计很多地方都非常巧妙,比如不同的结构体存储不同长度的字符串,不同编码类型存储不同长度的字符串,

空间预分配,空间惰性释放等,从存储结构,编码类型,内存分配策略和回收策略,作者都从性能方面做了非常多的考量设计,可想而知这也是redis为什么性能极高的原因,工作中也要学习这种追求极致性能的优良风格和设计风格。

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of Redis data type learning: Let's talk about String principles. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
Redis: Improving Application Performance and ScalabilityRedis: Improving Application Performance and ScalabilityApr 17, 2025 am 12:16 AM

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis: Exploring Its Data Model and StructureRedis: Exploring Its Data Model and StructureApr 16, 2025 am 12:09 AM

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis: Classifying Its Database ApproachRedis: Classifying Its Database ApproachApr 15, 2025 am 12:06 AM

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.

Why Use Redis? Benefits and AdvantagesWhy Use Redis? Benefits and AdvantagesApr 14, 2025 am 12:07 AM

Redis is a powerful database solution because it provides fast performance, rich data structures, high availability and scalability, persistence capabilities, and a wide range of ecosystem support. 1) Extremely fast performance: Redis's data is stored in memory and has extremely fast read and write speeds, suitable for high concurrency and low latency applications. 2) Rich data structure: supports multiple data types, such as lists, collections, etc., which are suitable for a variety of scenarios. 3) High availability and scalability: supports master-slave replication and cluster mode to achieve high availability and horizontal scalability. 4) Persistence and data security: Data persistence is achieved through RDB and AOF to ensure data integrity and reliability. 5) Wide ecosystem and community support: with a huge ecosystem and active community,

Understanding NoSQL: Key Features of RedisUnderstanding NoSQL: Key Features of RedisApr 13, 2025 am 12:17 AM

Key features of Redis include speed, flexibility and rich data structure support. 1) Speed: Redis is an in-memory database, and read and write operations are almost instantaneous, suitable for cache and session management. 2) Flexibility: Supports multiple data structures, such as strings, lists, collections, etc., which are suitable for complex data processing. 3) Data structure support: provides strings, lists, collections, hash tables, etc., which are suitable for different business needs.

Redis: Identifying Its Primary FunctionRedis: Identifying Its Primary FunctionApr 12, 2025 am 12:01 AM

The core function of Redis is a high-performance in-memory data storage and processing system. 1) High-speed data access: Redis stores data in memory and provides microsecond-level read and write speed. 2) Rich data structure: supports strings, lists, collections, etc., and adapts to a variety of application scenarios. 3) Persistence: Persist data to disk through RDB and AOF. 4) Publish subscription: Can be used in message queues or real-time communication systems.

Redis: A Guide to Popular Data StructuresRedis: A Guide to Popular Data StructuresApr 11, 2025 am 12:04 AM

Redis supports a variety of data structures, including: 1. String, suitable for storing single-value data; 2. List, suitable for queues and stacks; 3. Set, used for storing non-duplicate data; 4. Ordered Set, suitable for ranking lists and priority queues; 5. Hash table, suitable for storing object or structured data.

How to implement redis counterHow to implement redis counterApr 10, 2025 pm 10:21 PM

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft