Hiredis Subscribe/Publish with CWinThread

Programming/Redis 2016. 10. 5. 14:01 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Hiredis Subscribe/Publish



CWinThread 를 이용한 구현




struct event_base *base = event_base_new();

class CRedisConnect : public CWinThread
{
public:
	redisAsyncContext *c;
	// CWinThread extends
	virtual BOOL InitInstance();
	virtual int Run();
};

BOOL CRedisConnect::InitInstance()
{
	return TRUE;
}

int CRedisConnect::Run()
{
	c = redisAsyncConnect(redisServerIP, redisServerPort);
	if (c->err) {
		/* Let *c leak for now... */
		return 1;
	}
	redisLibeventAttach(c, base);

	int re2 = redisAsyncCommand(c, getCallback, NULL, "SUBSCRIBE foo");

	event_base_dispatch(base);

	return (0);
}



void getCallback(redisAsyncContext *c, void *r, void *privdata) {

	redisReply *reply = (redisReply *)r;
	if (reply == NULL)
	{
		LOG4CXX_DEBUG(g_log, "Subcribe reply == NULL");
	}
	// reply->type
	// #define REDIS_REPLY_STRING 1
	// #define REDIS_REPLY_ARRAY 2
	// #define REDIS_REPLY_INTEGER 3
	// #define REDIS_REPLY_NIL 4
	// #define REDIS_REPLY_STATUS 5
	// #define REDIS_REPLY_ERROR 6
	else if (reply->type == REDIS_REPLY_ERROR)
	{
		LOG4CXX_DEBUG(g_log, "Subcribe reply type == REDIS_REPLY_ERROR");
	}
	else if (reply->type == REDIS_REPLY_ARRAY)
	{
		LOG4CXX_DEBUG(g_log, "reply->elements == " << reply->elements);
		LOG4CXX_DEBUG(g_log, "reply->len == " << reply->len);
		for (int i = 0; i < reply->elements; i++)
		{
			if (reply->element[i]->str != NULL)
			LOG4CXX_DEBUG(g_log, "Subcribe reply array [" << i << "] == " << reply->element[i]->str);			
		}		
	}
	else
	{
		LOG4CXX_DEBUG(g_log, "getCallback reply str : " << reply->str);
	}
}

'Programming > Redis' 카테고리의 다른 글

Hiredis SMEMBERS 활용  (0) 2017.01.24
Redis Client Connect Test  (0) 2016.10.26
How to use Pub/sub with hiredis in C++?  (0) 2016.09.29
hiredis fatal error C1853:  (0) 2016.09.29
hiredis MFC import Visual Studio 2013  (0) 2016.09.28