search

异步API Hiredis 拥有一个套异步API方便与一些事件库协同工作. Hiredis的代码中涵盖了hiredis与libev和libevent这两个库结合使用的例子. 连接 函数 redisAsyncConnect 用来建立到redis服务器的非阻塞连接. 返回一个 redisAsyncContext 结构体指针. 因为建立

异步API

Hiredis 拥有一个套异步API方便与一些事件库协同工作. Hiredis的代码中涵盖了hiredis与libev和libevent这两个库结合使用的例子.

连接

函数 redisAsyncConnect 用来建立到redis服务器的非阻塞连接. 返回一个 redisAsyncContext 结构体指针. 因为建立的连接是非阻塞的,无法立即返回目标主机的ip和端口是否可达。所以在建立连接后,我们应该检查 err 成员,来确认连接是否存在错误。

redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
if (c->err) {
    printf("Error: %s\n", c->errstr);
    // 错误处理
}

redisAsyncContext包含一个连接断开回调函数(主动断开,或者发生错误都会调用)。此函数原型如下:

void(const redisAsyncContext *c, int status);

当用户主动断开连接时, 参数 status 被设置为 REDIS_OK ; 当发生错误而导致连接断开时 status 被设置为REDIS_ERR 。此时我们可以根据 err 成员变量判断错误产生的情况。

当我们需要进行断线重连时,可以在断开连接回调函数处理。

每个上下文(redisAsyncContext)实例只能设置一次断线回调函数,多次调用会返回REDIS_ERR错误。 使用以下函数设置断线回调函数:

int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn);

发送命令并设置回调

使用redisAsyncContext时,(每帧)发送的命令会自动pipelined(打包发送命令)。所以我们需要设置reply回调函数来进行命令执行后的处理工作。回调函数原型如下:

void(redisAsyncContext *c, void *reply, void *privdata);

参数 privdata 为用户数据,你可以设置成函数调用时所需要的任意数据。

使用以下函数发送异步命令:

int redisAsyncCommand(
  redisAsyncContext *ac, redisCallbackFn *fn, void *privdata,
  const char *format, ...);
int redisAsyncCommandArgv(
  redisAsyncContext *ac, redisCallbackFn *fn, void *privdata,
  int argc, const char **argv, const size_t *argvlen);

这两个函数和阻塞版本类似。命令成功添加到输出缓冲区时返回 REDIS_OK ,错误时返回 REDIS_ERR 。 例如:当连接被用户中断时,新命令无法被添加,所有类似 redisAsyncCommand 的函数调用都返回 REDIS_ERR错误码。

如果将回调函数设置成 NULL (? privdata,还是reply?需要实验一下 ?)内存被立即释放。当回调函数非空,则内存在调用后释放内存。reply参数只能在回调函数体中使用。

当上下文(redisAsyncContext)发生错误时所有未执行的命令所设置的回调函数都会被调用,回调函数中reply指针为空。

断开连接

异步连接可以使用以下函数终止:

void redisAsyncDisconnect(redisAsyncContext *ac);

当函数被调用时连接并不是立即被断开,而是新的命令不在被接受。(调用函数后)当所有未执行的指令都被写入到socket中,并且命令回调函数都被执行以后,连接才被中断(连接中断回调函数被调用,status为 REDIS_OK )、上下文对象被释放。.

绑定到事件库

There are a few hooks that need to be set on the context object after it is created. See the adapters/ directory for bindings to libev and libevent.

Statement
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
How does MySQL handle data replication?How does MySQL handle data replication?Apr 28, 2025 am 12:25 AM

MySQL processes data replication through three modes: asynchronous, semi-synchronous and group replication. 1) Asynchronous replication performance is high but data may be lost. 2) Semi-synchronous replication improves data security but increases latency. 3) Group replication supports multi-master replication and failover, suitable for high availability requirements.

How can you use the EXPLAIN statement to analyze query performance?How can you use the EXPLAIN statement to analyze query performance?Apr 28, 2025 am 12:24 AM

The EXPLAIN statement can be used to analyze and improve SQL query performance. 1. Execute the EXPLAIN statement to view the query plan. 2. Analyze the output results, pay attention to access type, index usage and JOIN order. 3. Create or adjust indexes based on the analysis results, optimize JOIN operations, and avoid full table scanning to improve query efficiency.

How do you back up and restore a MySQL database?How do you back up and restore a MySQL database?Apr 28, 2025 am 12:23 AM

Using mysqldump for logical backup and MySQLEnterpriseBackup for hot backup are effective ways to back up MySQL databases. 1. Use mysqldump to back up the database: mysqldump-uroot-pmydatabase>mydatabase_backup.sql. 2. Use MySQLEnterpriseBackup for hot backup: mysqlbackup--user=root-password=password--backup-dir=/path/to/backupbackup. When recovering, use the corresponding life

What are some common causes of slow queries in MySQL?What are some common causes of slow queries in MySQL?Apr 28, 2025 am 12:18 AM

The main reasons for slow MySQL query include missing or improper use of indexes, query complexity, excessive data volume and insufficient hardware resources. Optimization suggestions include: 1. Create appropriate indexes; 2. Optimize query statements; 3. Use table partitioning technology; 4. Appropriately upgrade hardware.

What are views in MySQL?What are views in MySQL?Apr 28, 2025 am 12:04 AM

MySQL view is a virtual table based on SQL query results and does not store data. 1) Views simplify complex queries, 2) Enhance data security, and 3) Maintain data consistency. Views are stored queries in databases that can be used like tables, but data is generated dynamically.

What are the differences in syntax between MySQL and other SQL dialects?What are the differences in syntax between MySQL and other SQL dialects?Apr 27, 2025 am 12:26 AM

MySQLdiffersfromotherSQLdialectsinsyntaxforLIMIT,auto-increment,stringcomparison,subqueries,andperformanceanalysis.1)MySQLusesLIMIT,whileSQLServerusesTOPandOracleusesROWNUM.2)MySQL'sAUTO_INCREMENTcontrastswithPostgreSQL'sSERIALandOracle'ssequenceandt

What is MySQL partitioning?What is MySQL partitioning?Apr 27, 2025 am 12:23 AM

MySQL partitioning improves performance and simplifies maintenance. 1) Divide large tables into small pieces by specific criteria (such as date ranges), 2) physically divide data into independent files, 3) MySQL can focus on related partitions when querying, 4) Query optimizer can skip unrelated partitions, 5) Choosing the right partition strategy and maintaining it regularly is key.

How do you grant and revoke privileges in MySQL?How do you grant and revoke privileges in MySQL?Apr 27, 2025 am 12:21 AM

How to grant and revoke permissions in MySQL? 1. Use the GRANT statement to grant permissions, such as GRANTALLPRIVILEGESONdatabase_name.TO'username'@'host'; 2. Use the REVOKE statement to revoke permissions, such as REVOKEALLPRIVILEGESONdatabase_name.FROM'username'@'host' to ensure timely communication of permission changes.

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

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!