One, environmental preparation:
CentOS7, CMake compiler
Mysql uses the official C client and does not use the C ++ library, which relies on boost
RPM: mysql-community-devel-5.7.18-1.el7.x86_64. RPM: mysql-community-devel-5.7.18-1.el7.x86_64. RPM: mysql-community-devel-5.7.18-1.el7.x86_64
Downloads.mysql.com/archives/co… \
Redis uses its own hiredis library, but also c language implementation
Github.com/antirez/red… \
Github.com/redis/hired… \
\
Second, the complete project source code: download.csdn.net/download/li…
\
CMakeLists.txt\
Cmake_minimum_required (VERSION 2.8) # Project main reference TeamTalk #https://github.com/meili/TeamTalk/tree/master/server/src/db_proxy_server PROJECT(mysqlredis_pool) AUX_SOURCE_DIRECTORY(.src_list) #mysql client, we don't use the c++ version because it relies on boost library, Too wordy # mysql connector - c + + - 1.1.8 - Linux - el7 - x86-64 - bit. Tar. Gz - https://downloads.mysql.com/archives/c-cpp/ # - it is recommended to use c version https://downloads.mysql.com/archives/c-c/ # mysql - community - devel - 5.7.18-1. El7. X86_64. RPM comes with c # installation instructions -- customer end files and library http://blog.csdn.net/libaineu2004/article/details/76212125 SET(MYSQL_INCLUDE_DIR /usr/include/mysql) SET(MYSQL_LIB The/usr/lib64 / mysql) # redis client, use the c version - https://github.com/redis/hiredis # installation instructions http://blog.csdn.net/libaineu2004/article/details/76209267 SET(REDIS_INCLUDE_DIR /usr/local/include/hiredis/) SET(REDIS_LIB /usr/local/lib/) INCLUDE_DIRECTORIES(${MYSQL_INCLUDE_DIR} ${REDIS_INCLUDE_DIR}) LINK_DIRECTORIES(${MYSQL_LIB} ${REDIS_LIB}) #ADD_EXECUTABLE must be placed before target_link_DIRECTORIES, ADD_EXECUTABLE(${PROJECT_NAME} ${SRC_LIST}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} mysqlClient hiredis pthread)Copy the code
\
main.cpp
#include "DBPool.h"
#include "CachePool.h"
#include "ThreadPool.h"
#include "ProxyTask.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h> //daemonize
#include <string>
#include <string.h>
void handler(uint32_t conn_uuid)
{
printf("hello,%d\n", conn_uuid);
CDBManager* pDBManger = CDBManager::getInstance();
if (!pDBManger) {
printf("DBManager init failed");
return;
}
CDBConn* pDBConn = pDBManger->GetDBConn("teamtalk_master");//get a db conn
if (pDBConn) {
string strSql = "select * from IMUser";
CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str());
if (pResultSet)
{
delete pResultSet;
}
pDBManger->RelDBConn(pDBConn);//注意,con用完之后一定要记得归还,ReleaseConnection
}
}
int main(int argc, char* argv[])
{
signal(SIGPIPE, SIG_IGN);
//案例1,仅仅使用mysql连接池,不使用线程池
CDBManager* pDBManger = CDBManager::getInstance();
if (!pDBManger) {
printf("DBManager init failed");
return -1;
}
CDBConn* pDBConn = pDBManger->GetDBConn("teamtalk_master");//get a db conn
if (pDBConn) {
string strSql = "select * from IMUser";
CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str());
if (pResultSet)
{
string strResult, strSalt;
uint32_t nId, nGender, nDeptId, nStatus;
string strNick, strAvatar, strEmail, strRealName, strTel, strDomain,strSignInfo;
while (pResultSet->Next()) {
nId = pResultSet->GetInt("id");
strResult = pResultSet->GetString("password");
strSalt = pResultSet->GetString("salt");
strNick = pResultSet->GetString("nick");
nGender = pResultSet->GetInt("sex");
strRealName = pResultSet->GetString("name");
strDomain = pResultSet->GetString("domain");
strTel = pResultSet->GetString("phone");
strEmail = pResultSet->GetString("email");
strAvatar = pResultSet->GetString("avatar");
nDeptId = pResultSet->GetInt("departId");
nStatus = pResultSet->GetInt("status");
strSignInfo = pResultSet->GetString("sign_info");
}
delete pResultSet;
}
pDBManger->RelDBConn(pDBConn);//注意,con用完之后一定要记得归还,ReleaseConnection
}
//案例2,使用mysql连接池,也使用线程池
//top -H -p <pid>The terminal views a particular process<pid>CThreadPool thread_pool; thread_pool.Init(10); CTask* pTask1 = new CProxyTask(1, handler); //void CWorkerThread::Execute() Deletes CTask* thread_pool.addTask (pTask1); CTask* pTask2 = new CProxyTask(2, handler); thread_pool.AddTask(pTask2); CTask* pTask3 = new CProxyTask(3, handler); thread_pool.AddTask(pTask3); / / case 3, simply use redis connection pool, do not use the thread pool CacheManager * pCacheManager = CacheManager: : getInstance (); if (! pCacheManager) { printf("CacheManager init failed"); return -1; } CacheConn* pCacheConn = pCacheManager->GetCacheConn("redis"); if (pCacheConn) { string strVal = "sodesiname"; pCacheConn->set("total_user_update", strVal); string str = pCacheConn->get("total_user_update"); printf("%s\n", str.c_str()); pCacheManager->RelCacheConn(pCacheConn); Printf ("\ninput:\n"); char s[20]; gets(s); // Wait for the input string until the carriage return ends. Return 0; }Copy the code
\
Welcome to the sister article
Windows C/C++ programming implementation of mysql, Redis client connection pool and thread pool (★firecat recommended ★) \
\