RedisJobMonitor.cpp 4.37 KB
#include "RedisJobMonitor.h"
#include <winsock2.h>
#include "hiredis.h"

///////////////////////////////////////////////////////////////////////////

CRedisConnector::CRedisConnector(const std::string& redis_ip, int redis_port) : ctx_(NULL)
{
    // 建立REDIS服务器连接
    ctx_ = redisConnect(redis_ip.c_str(), redis_port);
    if (ctx_->err)
    {
        redisFree(ctx_);
        ctx_ = NULL;
    }
    else
    {
        struct timeval timeout = {3, 0}; // 3 seconds
        redisSetTimeout(ctx_, timeout);
    }
}

CRedisConnector::~CRedisConnector(void)
{
    // 断开REDIS服务连接
    if (ctx_)
    {
        redisFree(ctx_);
    }
}


// 获取REDIS服务器连接句柄
redisContext * CRedisConnector::get(void)
{
    return ctx_;
}

// 测试REDIS服务器是否连接成功
bool CRedisConnector::ping(void)
{
	bool is_ok = false;
    
    if (ctx_)
    {
        redisReply * reply = (redisReply *)redisCommand(ctx_, "PING");
        if (reply)
        {
            freeReplyObject(reply);
            is_ok = true;
        }
    }

    return is_ok;
}

///////////////////////////////////////////////////////////////////////////

// 获取任务键值
int CBaseRedisJobMonitor::get_job_val(redisContext * ctx, const std::string& job_key, int default_job_val)
{
    int job_val = default_job_val;

    if (ctx)
    {
        redisReply * reply = (redisReply *)redisCommand(ctx, "GET %s", job_key.c_str());
        if (reply)
        {
            if (reply->type == REDIS_REPLY_STRING)
            {
                job_val = atoi(reply->str);
            }
            freeReplyObject(reply);
        }
    }

    return job_val;
}

// 获取任务键值
DWORD CBaseRedisJobMonitor::get_job_val(redisContext * ctx, const std::string& job_key, DWORD default_job_val)
{
	DWORD job_val = default_job_val;

	if (ctx)
	{
		redisReply * reply = (redisReply *)redisCommand(ctx, "GET %s", job_key.c_str());
		if (reply)
		{
			if (reply->type == REDIS_REPLY_STRING)
			{
				sscanf_s(reply->str, "%lu", &job_val);
			}
			freeReplyObject(reply);
		}
	}

	return job_val;
}

std::string CBaseRedisJobMonitor::get_job_val(redisContext * ctx, const std::string& job_key, std::string default_job_val)
{
	std::string job_val = default_job_val;

	if (ctx)
	{
		redisReply * reply = (redisReply *)redisCommand(ctx, "GET %s", job_key.c_str());
		if (reply)
		{
			if (reply->type == REDIS_REPLY_STRING)
			{
                job_val = reply->str;
			}
			freeReplyObject(reply);
		}
	}

	return job_val;
}

// 设置任务键值
bool CBaseRedisJobMonitor::set_job_val(redisContext * ctx, const std::string& job_key, int job_val)
{
    if (ctx)
    {
        redisReply * reply = (redisReply *)redisCommand(ctx, "SET %s %d", job_key.c_str(), job_val);
        if (reply)
        {
            freeReplyObject(reply);
            return true;
        }
    }

    return false;
}

bool CBaseRedisJobMonitor::set_job_val(redisContext * ctx, const std::string& job_key, DWORD job_val)
{
	if (ctx)
	{
		redisReply * reply = (redisReply *)redisCommand(ctx, "SET %s %lu", job_key.c_str(), job_val);
		if (reply)
		{
			freeReplyObject(reply);
			return true;
		}
	}

	return false;
}

bool CBaseRedisJobMonitor::set_job_val(redisContext * ctx, const std::string& job_key, std::string job_val)
{
    if (ctx)
    {
        redisReply * reply = (redisReply *)redisCommand(ctx, "SET %s %s", job_key.c_str(), job_val.c_str());
        if (reply)
        {
            freeReplyObject(reply);
            return true;
        }
    }

    return false;
}

bool CBaseRedisJobMonitor::del_key(redisContext * ctx, const std::string& job_key)
{
    int ret = 0;
    if (ctx)
    {
        redisReply * reply = (redisReply *)redisCommand(ctx, "DEL %s", job_key.c_str());
        if (reply)
        {
            if (reply->type == REDIS_REPLY_INTEGER)
            {
                ret = reply->integer;
            }
            freeReplyObject(reply);
        }
    }

    if (ret == 1)
    {
        return true;
    }

    return false;
}

bool CBaseRedisJobMonitor::exist_key(redisContext * ctx, const std::string& job_key)
{
    int ret = 0;
    if (ctx)
    {
        redisReply * reply = (redisReply *)redisCommand(ctx, "EXISTS %s", job_key.c_str());
        if (reply)
        {
            if (reply->type == REDIS_REPLY_INTEGER)
            {
                ret = reply->integer;
            }
            freeReplyObject(reply);
        }
    }

    if (ret == 1)
    {
        return true;
    }

    return false;
}