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 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
## 5. String storage principle
In Redis, data is stored in a RedisObject classtypedef 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.
len: represents the used length
Memory , when destroyed
releases the memory once , easy to find
allocate memory twice , When destroyed
Release memory twice
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 embstr3. If the embstr character exceeds 44 bytes, convert it to raw8. Advantages of SDS data structure
1,Binary safe can store image shaping, floating point type
- 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
Don’t worry about memory overflow, sds has automatic expansion capability
The time complexity of getting the string length is O(1), and the len attribute is stored
space pre-allocation and
lazy space release
9. Why not use the character array in c?
1. Memory needs to be allocated in advance, which maymemory overflow
time complexity O(n)
memory reallocation
Binary data storage is not safe, pictures, videos, etc. cannot be saved.
十、关于内存预分配特性
通过源码分析,扩容策略是字符串在长度小于 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!

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

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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 latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
