search
HomeDatabaseRedisRedis data type learning: Let's talk about String principles
Redis data type learning: Let's talk about String principlesJan 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
es和redis区别es和redis区别Jul 06, 2019 pm 01:45 PM

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

一起来聊聊Redis有什么优势和特点一起来聊聊Redis有什么优势和特点May 16, 2022 pm 06:04 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

实例详解Redis Cluster集群收缩主从节点实例详解Redis Cluster集群收缩主从节点Apr 21, 2022 pm 06:23 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

Redis实现排行榜及相同积分按时间排序功能的实现Redis实现排行榜及相同积分按时间排序功能的实现Aug 22, 2022 pm 05:51 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

详细解析Redis中命令的原子性详细解析Redis中命令的原子性Jun 01, 2022 am 11:58 AM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

一文搞懂redis的bitmap一文搞懂redis的bitmapApr 27, 2022 pm 07:48 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

实例详解Redis实现排行榜及相同积分按时间排序功能的实现实例详解Redis实现排行榜及相同积分按时间排序功能的实现Aug 26, 2022 pm 02:09 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

一起聊聊Redis实现秒杀的问题一起聊聊Redis实现秒杀的问题May 27, 2022 am 11:40 AM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于实现秒杀的相关内容,包括了秒杀逻辑、存在的链接超时、超卖和库存遗留的问题,下面一起来看一下,希望对大家有帮助。

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools