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]
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.
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
Integer type, character type, float (single floating point type)
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;
len: represents the used length
alloc: the total allocated memory size flags: represents the storage typebuf[]: Actual data6. Differences in storage of three encodings1. 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
allocate memory twice , When destroyed
Release memory twice
embstr cannot be modified and is read-only.
Binary safe can store image shaping, floating point type
Stores 8-byte long integer long, 2^63-1
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
, 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
memory 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!