redis 收發資料最簡單的例子

2021-10-12 18:47:46 字數 1677 閱讀 9550

首先第一步鏈結redis

rediscontext *context = redisconnect("127.0.0.1", 6379);

if (context->err)

第二和第三步可以不寫

//2、校驗密碼 

char *auth = "test123";

redisreply *reply = (redisreply *)rediscommand(context, "auth %s", auth);

if (null == reply)

freereplyobject(reply);

//3、選擇資料庫 - 不選擇也可

reply = (redisreply *)rediscommand(context, "select %d", 1);

if (null == reply || redis_reply_error == reply->type)

4. 下面就可以收發資料了

//發布訊息  publish 

redisreply* reply = (redisreply *)rediscommand(context, "subscribe %s %s", "spub","hello messge");

//redisreply* reply = (redisreply *)rediscommand(context, "subscribe %s %b", "spub",messge,size); //二進位制資料

if (null == reply)

freereplyobject(reply);

//訂閱訊息  可以訂閱psubscribe 

redisreply* reply = (redisreply *)rediscommand(context, "subscribe %s", "spub");

if (null == reply||reply->type != redis_reply_array)//訂閱成功返回乙個陣列標識

freereplyobject(reply);

//阻塞等待訂閱訊息

while (true)

reply = (redisreply*)_reply;

for (int nindex = 0; nindex < reply->elements; nindex++)

freereplyobject(reply);

std::cout << "***************" << std::endl;

}非訂閱模式

redisreply* reply = (redisreply *)rediscommand(context, "set %s %s", "spub","hello messge");

//redisreply* reply = (redisreply *)rediscommand(context, "set %s %b", "spub",messge,size); //二進位制資料

if (null == reply)

freereplyobject(reply);

redisreply* reply = (redisreply *)rediscommand(context, "get %s", "spub");

if (null == reply)

if (reply->len > 0)

freereplyobject(reply);

最簡單的remoting的例子

節選部分 備查 1 遠端物件 建立類庫專案 remoteobject using system namespace remoteobject 2 服務端 建立控制台專案 remoteserver using system using system.runtime.remoting namespace...

最簡單的核心模組例子

include include include static int init hello init void static void exit hello exit void module init hello init module exit hello exit static int init...

FreeMarker最簡單的例子(2)

二 通過string來建立模版物件,並執行插值處理 j a view plain copy package com.abc import freemarker.template.template import j a.io.outputstreamwriter import j a.io.strin...