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

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

青灯夜游
Release: 2022-01-29 08:00:38
forward
2314 people have browsed it

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]
Copy after login

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;
Copy after login

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';
}
Copy after login

真正的清除空间

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;
}
Copy after login

以上便是关于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!

source:juejin.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template