快速,持续,稳定,傻瓜式
支持Mysql,Sqlserver数据同步

数据库应用之–Redis+mysql实现大量数据的读写,以及高并发

请联系QQ:1793040 索取软件

#include "stdafx.h"
#include "DBHandle.h"


DBHandle::DBHandle()
{
    m_pRedisContext = NULL;
}

DBHandle::~DBHandle()
{ 
    if (m_pRedisContext != NULL)
    {
        m_pRedisContext = NULL;
    } 
}

bool DBHandle::connectRedis(string strIp, int iPort, string strPwd)
{
    //redis默认监听端口为6387 可以再配置文件中修改 
    char szBuf[32]= {};
    strcpy_s(szBuf, sizeof(strIp), strIp.c_str());
    m_pRedisContext = redisConnect(szBuf, iPort);
    if (NULL==m_pRedisContext || m_pRedisContext->err)
    {
        return false;
    }

    //输入Redis密码
    strcpy_s(szBuf, sizeof(strPwd), strPwd.c_str());
    redisReply *pRedisReply=(redisReply*)redisCommand(m_pRedisContext, "AUTH %s", szBuf);
    if (NULL != pRedisReply)
    {
        freeReplyObject(pRedisReply);
    }
    if (NULL==pRedisReply->str)
    {
        return false;
    }
    return true;

}

void DBHandle::freeRedis()
{
    redisFree(m_pRedisContext);
    if (m_pRedisContext != NULL)
    {
        m_pRedisContext = NULL;
    }
}

int DBHandle::getRedisDBSize()
{
    //查看list长度
    int iListLen=0;
    //redisReply *pRedisReply=(redisReply *)redisCommand(m_pRedisContext, "LLen datalist");
    redisReply *pRedisReply=(redisReply *)redisCommand(m_pRedisContext, "DBSIZE");
    if (NULL != pRedisReply)
    {
        if (NULL==pRedisReply->integer)
        {
            return false;
        }
        iListLen =pRedisReply->integer;
        freeReplyObject(pRedisReply);
    }
    if (NULL== pRedisReply)
    {
        printf("%s \r\n", m_pRedisContext->errstr);
        return false;
    }
    
    return iListLen;
}

bool DBHandle::writeHsetToRedis()
{
    bool bFlag=connectRedis("127.0.0.1", 6379, "123456");
    if (false== bFlag)
    {
        return false;
    }

    time_t st =time(NULL);//
    stTestData data= {};
    int i=1;
    while (i<100000)
    {

        data.iHeight = i;
        data.iWidth =30;
        char szBuf[64]= {};
        sprintf_s(szBuf, "width%d", i);
        strcpy_s(data.szValue, 64, szBuf);
        sprintf_s(data.szHValue, "%s%d", "heighttest", i);

        //向Redis写入数据hset location (interger)1 "width"
        sprintf_s(szBuf, "hset location%d value %s", i, data.szValue);
        redisReply *pRedisReply=(redisReply *)redisCommand(m_pRedisContext, szBuf);
        if (NULL != pRedisReply)
        {
            freeReplyObject(pRedisReply);
        }
        i++;
    }

    printf("write finish");
    readDataFromRedis();

    time_t et = time(NULL);
    int iUsed=st- et;
    printf("used time is %d", iUsed);
    freeRedis();
    return true;

}


bool DBHandle::readDataFromRedis()
{
    

    printf("read start");
    
    int iSize= getListSize();
    if (iSize <=0)
    {
        return false;
    }
    bool bSuc= connectMysql();
    if (bSuc==false)
    {
        return false;
    }

    int iCount=iSize;//计数
    while (iCount > 0)
    {
        //用get命令获取数据
        redisReply *pRedisReply=(redisReply*)redisCommand(m_pRedisContext, "RPOP datalist");
        if (NULL== pRedisReply)
        {
            return false;
        }
        if (NULL !=pRedisReply->str)
        {
            string str=pRedisReply->str;
            insertDataToMysql(str);
            freeReplyObject(pRedisReply);
        }
        iCount--;
    }
    
    printf("read finish");
    
    return true;

}

bool DBHandle::connectMysql()
{
    mysql_init(&m_mysql);

    // Connects to a MySQL server
    const char host[]="192.168.4.8";
    const char user[]="root";
    const char passwd[]="123456";
    const char db[]="topproductline";
    unsigned int port=3306;
    const char *unix_socket= NULL;
    unsigned long client_flag=0;

    
    if (mysql_real_connect(&m_mysql, host, user, passwd, db, port, unix_socket, client_flag)) {
        printf("The connection was successful.\n");
        return true;
    }
    else {
        printf("Error connecting to database:%s\n", mysql_error(&m_mysql));
        return false;
    }
}

void DBHandle::FreeMysqlConnect()
{
    mysql_free_result(res);
    mysql_close(&m_mysql);
}

bool DBHandle::insertDataToMysql(string strData)
{
    char szQuery[256]={0};
    sprintf_s(szQuery, "insert into a_test (type) values ('%s');", strData.c_str());
    if (mysql_query(&m_mysql, szQuery)) {
        printf("Query failed (%s)\n", mysql_error(&m_mysql));
        return false;
    }
    else {
        printf("Insert success\n");
        return true;
    }
}

相关推荐

咨询软件
 
QQ在线咨询
售前咨询热线
QQ1793040