IM/MS/works/db/UserFriendsDB.cpp
dongl 7b54e1449e 创建了vector保存固定数量的mysql链接,使用时取vector的, 暂时解了回收链接的问题。 而不是无限的新建
封了一层回收mysql链接的函数

解决了表面上的内存泄漏

30个链接 在4MB 左右
而新建 返回到池 在2.5 左右

注:后续好好看一下 mysqlpp 的连接池内容
Grab a free connection from the pool.
This method creates a new connection if an unused one doesn't exist, and destroys any that have remained unused for too long. If there is more than one free connection, we return the most recently used one; this allows older connections to die off over time when the caller's need for connections decreases. Do not delete the returned pointer. This object manages the lifetime of connection objects it creates.
Return values

a pointer to the connection
2023-05-13 14:27:10 +08:00

117 lines
2.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Created by dongl on 23-5-4.
//
#include "UserFriendsDB.h"
#include "linkDB.h"
// 查询好友添加权限类型
char UserFriendsDB::select_add_type(uint64_t account) {
conn = LinkDB::safe_grab();
auto query = conn->query("select restrictions from im_user_friends where account=%0:account");
query.parse();
// 查看添加授权类型 0 直接添加 1 验证问题 2 账户审核
auto ret = query.store(account)[0][0][0]; // 因为 account 唯一 所以结果 至多一个
// 放回链接
// LinkDB::release(conn);
return ret;
}
// 修改好友列表数据 添加好友
/// friends {
/// uid info { }
/// }
bool UserFriendsDB::add_friends(uint64_t account, uint64_t friends) {
conn = LinkDB::safe_grab();
auto query = conn->query("update im_user_friends set friends="
"JSON_SET(friends, '$.\"%2:friends\"', "
"JSON_OBJECT('belong_grouping', 1, 'add_time', 10000, 'add_source', 1)"
")"
"where account=%1:account");
query.template_defaults[1] = account;
query.template_defaults[2] = friends;
query.parse();
auto ret = query.exec();
// 放回链接
// LinkDB::release(conn);
return ret;
}
void UserFriendsDB::insert_friends_to_be_added(uint64_t account, uint64_t friends) {
conn = LinkDB::safe_grab();
// 放回链接
// LinkDB::release(conn);
}
std::optional<rapidjson::Document> UserFriendsDB::select_friends_all(uint64_t account) {
conn = LinkDB::safe_grab();
auto q = conn->query("select friends from im_user_friends where account=%0:account");
q.parse();
auto ret = q.store(account);
if (ret.num_rows() < 1) {
return std::nullopt;
}
std::string friends;
ret[0][0].to_string(friends);
rapidjson::Document document;
document.Parse(friends.c_str());
// 放回链接
// LinkDB::release(conn);
return document;
}
std::optional<rapidjson::Document>
UserFriendsDB::select_friends_info(uint64_t account, uint64_t friends) {
conn = LinkDB::safe_grab();
auto q = conn->query("select JSON_EXTRACT(friends, '$.\"%2:friends\"') as friend_info "
"from im_user_friends where account=%1:account");
q.template_defaults[1] = account;
q.template_defaults[2] = friends;
q.parse();
// 查库
auto ret = q.store();
printf("%zu\n", ret.num_rows());
if (ret.num_rows() < 1) {
return std::nullopt;
}
// 取json字符串
std::string friend_info;
ret[0][0].to_string(friend_info);
// 解析json
rapidjson::Document document;
document.Parse(friend_info.c_str());
// 放回链接
// LinkDB::release(conn);
return document;
}