C++实现redis发布订阅模式

在C++中实现Redis的发布-订阅模式,通常会使用一个Redis客户端库,如`hiredis`,这是一个广泛使用的、轻量级的C语言库,同样适用于C++项目。下面是一个使用`hiredis`实现发布和订阅功能的基本示例。

 

### 安装hiredis

 

首先,确保你已经安装了`hiredis`库。如果尚未安装,可以通过以下命令安装(以Unix系统为例):

 

```sh

git clone https://github.com/redis/hiredis.git

cd hiredis

make

sudo make install

```

 

### 引入hiredis头文件

 

在你的C++代码中,你需要包含hiredis的头文件:

 

```cpp

#include <hiredis/hiredis.h>

```

 

### 发布消息

 

以下是一个简单的函数,用于向Redis服务器发布消息:

 

```cpp

void publishMessage(const char* channel, const char* message) {

    redisContext* ctx = redisConnect("localhost", 6379);

    if (ctx == NULL || ctx->err) {

        if (ctx) {

            printf("Connection error: %s\n", ctx->errstr);

            redisFree(ctx);

        } else {

            printf("Connection error: can't allocate redis context\n");

        }

        return;

    }

 

    redisReply* reply = (redisReply*)redisCommand(ctx, "PUBLISH %s %s", channel, message);

    if (reply == NULL) {

        printf("Error publishing message: %s\n", ctx->errstr);

    } else {

        printf("Published message on channel %s: %s\n", channel, message);

        freeReplyObject(reply);

    }

 

    redisFree(ctx);

}

```

 

### 订阅消息

 

订阅消息通常在一个单独的线程中进行,因为订阅操作会阻塞等待新的消息到来。下面是一个订阅消息的示例,包括启动一个新的线程来处理订阅逻辑:

 

```cpp

static void* subscribeCallback(void* arg) {

    redisContext* ctx = (redisContext*)arg;

    redisReply* reply;

 

    while (REDIS_OK == redisGetReply(ctx, (void**)&reply)) {

        if (reply->type == REDIS_REPLY_ARRAY && reply->elements == 3 &&

            strcmp(reply->element[2]->str, "message") == 0) {

            printf("Received message on channel %s: %s\n",

                   reply->element[1]->str, reply->element[2]->str);

        }

        freeReplyObject(reply);

    }

 

    return NULL;

}

 

void startSubscription(const char* channel) {

    redisContext* subCtx = redisConnect("localhost", 6379);

    if (subCtx == NULL || subCtx->err) {

        printf("Connection error: %s\n", subCtx->errstr);

        redisFree(subCtx);

        return;

    }

 

    redisReply* reply = (redisReply*)redisCommand(subCtx, "SUBSCRIBE %s", channel);

    if (reply == NULL) {

        printf("Error subscribing to channel: %s\n", subCtx->errstr);

    } else {

        freeReplyObject(reply);

        pthread_t subscriberThread;

        if (pthread_create(&subscriberThread, NULL, subscribeCallback, (void*)subCtx) == 0) {

            pthread_detach(subscriberThread);

            printf("Subscribed to channel %s\n", channel);

        } else {

            printf("Failed to create subscription thread\n");

            redisFree(subCtx);

        }

    }

}

```

 

### 使用示例

 

你可以这样使用上面的函数:

 

```cpp

int main() {

    startSubscription("my-channel");

    publishMessage("my-channel", "Hello, Redis!");

 

    // 注意:在实际应用中,你可能需要添加适当的逻辑来控制程序的退出,

    // 例如,等待用户输入或使用信号处理来优雅地关闭订阅线程。

 

    return 0;

}

```

 

这段代码展示了如何创建一个订阅线程来监听特定频道的消息,并在主线程中发布消息到该频道。请根据你的具体需求调整代码,比如增加错误处理和资源管理逻辑。

 

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值