前言 Redis(REmoteDIctionaryServer)是一个由Salvatore Sanfilippo写的key-value存储系统。 它有以下特点: 总体结构 Redis是一个单线程的服务器(除了写磁盘会开子进程,VM管理会开线程,先忽略这两块) 所以,它的服务器启动流程非常清晰,看下图,不再
前言
Redis(REmote DIctionary Server)是一个由Salvatore Sanfilippo写的key-value存储系统。
它有以下特点:
Redis是一个单线程的服务器(除了写磁盘会开子进程,VM管理会开线程,先忽略这两块)
所以,它的服务器启动流程非常清晰,看下图,不再赘述
事件循环 1. 数据结构
通过 aeEventLoop 结构来表示一个事件框架,美国服务器,分析如下:
1 typedef struct aeEventLoop { timeEventNextId; aeFileEvent events[AE_SETSIZE]; aeFiredEvent fired[AE_SETSIZE]; aeTimeEvent *timeEventHead; stop; aeBeforeSleepProc *beforesleep; 10 } aeEventLoop;
2. 事件函数分发流程redis支持 epoll、kqueue、select 三种网络模型
网络框架的代码实现主要在以下几个文件中:
ae.c / ae.h // 网络框架
ae_epoll.c // epoll模型的实现
ae_kqueue.c // kqueue模型的实现
ae_select.c // select模型的实现
程序选择哪一种,网站空间,是在编译期确定的
1 1 file ae.c #ifdef HAVE_EPOLL #ifdef HAVE_KQUEUE #include
框架函数非常简单,从初始化到结束,主要的函数就3个
aeCreateEventLoop、aeMain、aeDeleteEventLoop
其中,aeMain是事件循环的主体函数,它又会调用 aeProcessEvents函数
三个主体函数会调用 aeApiCreate、aeApiPool、aeApiFree三个接口函数进行处理
这三个接口函数又会映射到具体的某一种网络模型中,而这是在编译期确定下来的
具体如下图所示:
添加删除事件,由
aeCreateFileEvent、aeDeleteFileEvent函数完成,其函数分发流程如下图:
1. 在服务器初始化时,建立侦听socket,并绑定IP、Port
支持 TCP连接 与本机的unixSocket连接
1 if (server.port != 0) { 2 server.ipfd = anetTcpServer(server.neterr,server.port,server.bindaddr); 3 ... ... 4 } 5 if (server.unixsocket != NULL) { server.sofd = anetUnixServer(server.neterr,server.unixsocket,server.unixsocketperm); 8 ... ... 9 }
2. 侦听socket绑定到事件句柄
1 if (server.ipfd > 0 && aeCreateFileEvent(server.el,server.ipfd,AE_READABLE, ); 3 if (server.sofd > 0 && aeCreateFileEvent(server.el,server.sofd,AE_READABLE, );
其中“acceptTcpHandler”、“acceptUnixHandler” 是事件回调函数
当新连接到达时,会触发进入这两个函数
3. 再来看看 accept***Handler 里做了什么
1 void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) { 2 int cport, cfd; 3 char cip[128]; 4 REDIS_NOTUSED(el); 5 REDIS_NOTUSED(mask); 6 REDIS_NOTUSED(privdata); cfd = anetTcpAccept(server.neterr, fd, cip, &cport); 10 if (cfd == AE_ERR) { , server.neterr); 12 return; 13 } , cip, cport); }
acceptCommonHandler中做的事很简单,调用 CreateClient函数,创建新的redisClient对象
acceptCommonHandler(int fd) { 2 redisClient *c; 3 if ((c = createClient(fd)) == NULL) { ); ; 7 } 8 ... ... 9 }
在 createClient函数中,将新连接绑定到事件循环中
1 redisClient *createClient(int fd) { 2 redisClient *c = zmalloc(sizeof(redisClient)); 3 c->bufpos = 0; 4 5 anetNonBlock(NULL,fd); 6 anetTcpNoDelay(NULL,fd); 7 if (aeCreateFileEvent(server.el,fd,AE_READABLE, { 10 close(fd); 11 zfree(c); 12 return NULL; 13 }
当连接上有数据到达时,便会触发 readQueryFromClient函数,进行实际的网络数据读取与处理
3. Timer事件Redis将所有Timer绑定到事件循环中进行处理
通过函数 aeCreateTimeEvent 创建新的Timer事件
每一帧事件循环中,通过processTimeEvents函数进行处理

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

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

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment
