当前位置: 首页 > news >正文

响应式网站 英文衡水移动网站建设

响应式网站 英文,衡水移动网站建设,免费软文推广平台,飞机查询网站开发的创新点文章目录 一.编写思路二.代码实践 一.编写思路 定义消息队列 名字是否持久化 定义队列持久化类(持久化到 sqlite3) 构造函数(只能成功,不能失败) 如果数据库(文件)不存在则创建打开数据库打开 msg_queue_table 数据库表 插入队列移除队列将数据库中的队列恢复到内存…

文章目录

  • 一.编写思路
  • 二.代码实践

一.编写思路

  1. 定义消息队列
    1. 名字
    2. 是否持久化
  2. 定义队列持久化类(持久化到 sqlite3)
    1. 构造函数(只能成功,不能失败)
      1. 如果数据库(文件)不存在则创建
      2. 打开数据库
      3. 打开 msg_queue_table 数据库表
    2. 插入队列
    3. 移除队列
    4. 将数据库中的队列恢复到内存中
      传入一个哈希表,key 为名字,value 为队列的智能指针,填充该哈希表
  3. 定义队列管理类(包含内存管理和持久化管理)
    1. 构造函数:从数据库中恢复队列
    2. 声明队列
    3. 移除队列
    4. 获取队列

二.代码实践

MsgQueue.hpp:

#pragma once
#include "../common/Log.hpp"
#include "../common/Util.hpp"
#include "../common/Util.hpp"
#include <memory>
#include <unordered_map>
#include <mutex>
namespace ns_data
{class MsgQueue;using MsgQueuePtr = std::shared_ptr<MsgQueue>;/************* 定义消息队列* ****************/struct MsgQueue{std::string _name;bool _isDurable;MsgQueue(const std::string &name, bool isDurable): _name(name),_isDurable(isDurable){}};/****************** 定义消息队列持久化类* ******************/class MsgQueueMapper{private:ns_util::Sqlite3Util _sqlite;public:MsgQueueMapper(const std::string &dbName): _sqlite(dbName){// 确保数据库文件已经存在,不存在就创建if (!ns_util::FileUtil::createFile(dbName)){LOG(FATAL) << "create database " << dbName << " fail" << endl;exit(1);}if (!_sqlite.open()){LOG(FATAL) << "open database " << dbName << " fail" << endl;exit(1);}createTable();}/************** 插入消息队列* *************/bool insertMsgQueue(MsgQueuePtr msgQueuePtr){char insertSql[1024];sprintf(insertSql, "insert into msg_queue_table values('%s', '%d');",msgQueuePtr->_name.c_str(), msgQueuePtr->_isDurable);if (!_sqlite.exec(insertSql, nullptr, nullptr)){LOG(WARNING) << "insert MsgQueue fail, MsgQueue: " << msgQueuePtr->_name << endl;return false;}return true;}/*********** 移除消息队列* ***************/void removeMsgQueue(const std::string &name){char deleteSql[1024];sprintf(deleteSql, "delete from msg_queue_table where name='%s';", name.c_str());if (!_sqlite.exec(deleteSql, nullptr, nullptr)){LOG(WARNING) << "remove MsgQueue fail, MsgQueue: " << name << endl;}}/************ 从数据库中恢复消息队列到内存* *****************/void recoverMsgQueue(std::unordered_map<std::string, MsgQueuePtr> *mapPtr){const std::string selectSql = "select * from msg_queue_table;";if (!_sqlite.exec(selectSql.c_str(), selectCallback, mapPtr)){LOG(FATAL) << "recover MsgQueue from msg_queue_table fail" << endl;exit(1);}}/*************** 删除数据库表(仅调试)* ***************/void removeTable(){const std::string dropSql = "drop table if exists msg_queue_table;";if (_sqlite.exec(dropSql.c_str(), nullptr, nullptr)){LOG(WARNING) << "remove table msg_queue_table fail" << endl;}}private:void createTable(){const std::string createSql = "create table if not exists msg_queue_table(\name varchar(32) primary key,\durable int\);";if (!_sqlite.exec(createSql.c_str(), nullptr, nullptr)){LOG(FATAL) << "create table msg_queue_table fail" << endl;exit(1);}}static int selectCallback(void *arg, int colNum, char **line, char **fields){auto mapPtr = static_cast<std::unordered_map<std::string, MsgQueuePtr> *>(arg);std::string name = line[0];bool isDurable = std::stoi(line[1]);auto msgQueuePtr = std::make_shared<MsgQueue>(name, isDurable);mapPtr->insert({name, msgQueuePtr});return 0;}};class MsgQueueManager{private:MsgQueueMapper _mapper;std::unordered_map<std::string, MsgQueuePtr> _msgQueues;std::mutex _mtx;public:MsgQueueManager(const std::string &dbName): _mapper(dbName){_mapper.recoverMsgQueue(&_msgQueues);}/************ 声明队列* ************/bool declareMsgQueue(const std::string &name, bool isDurable){std::unique_lock<std::mutex> lck(_mtx);if (_msgQueues.count(name)){return true;}auto msgQueuePtr = std::make_shared<MsgQueue>(name, isDurable);_msgQueues[name] = msgQueuePtr;if (isDurable){return _mapper.insertMsgQueue(msgQueuePtr);}return true;}/*********** 移除队列* ***********/void removeMsgQueue(const std::string &name){std::unique_lock<std::mutex> lck(_mtx);auto it = _msgQueues.find(name);if (it == _msgQueues.end()){return;}if (it->second->_isDurable){_mapper.removeMsgQueue(name);}_msgQueues.erase(name);}/************* 获取指定队列* ***************/MsgQueuePtr getMsgQueue(const std::string &name){std::unique_lock<std::mutex> lck(_mtx);if (_msgQueues.count(name) == 0){return nullptr;}return _msgQueues[name];}/************** 清理所有队列(仅调试)* ******************/void clearMsgQueues(){std::unique_lock<std::mutex> lck(_mtx);_msgQueues.clear();_mapper.removeTable();}};}
http://www.yayakq.cn/news/809616/

相关文章:

  • 汉口网站建设 优帮云买毕业设计的网站
  • 海南做网站的公司友汇网站建设
  • 在线做爰 视频网站如何做品牌推广方案
  • 产品类网站模板网站静态界面挖取
  • vs开发网站开发教程系统炸了我成了系统
  • 南京企业制作网站wordpress wiki主题
  • 门户网站 模板之家域名解析好了怎么做网站
  • 官方静态网站模板网站开发市场成本
  • php网站开发视频教学邯郸企业做网站费用
  • 重庆博达建设集团网站国外外贸平台有哪些
  • 杂志媒体网站建设方案比较好网站制作公司
  • 什么网站可以找人做设计装修公司大全
  • 常州外贸网站建设mvc做网站前台代码
  • 视频门户网站建设服务器长沙景点大全 长沙景点排名
  • 在线网站设计wordpress通知插件
  • 专业做公司网站长沙网站建设规划
  • 免费建设网站和域名哪个网站有利于做课件
  • 中餐网站模板服装配件网站建设 中企动力
  • 网站栏目设计怎么写烟台网站排名
  • 网站做301跳转需解析常熟网站开发
  • 深圳创意网站从零开始学ui设计
  • 查询网站mx记录网站备案多少岁
  • 印象网站建设网站建设外贸广州
  • erp系统与网站对接长沙wordpress主题改中文字体
  • 重庆怎么站seo企业营销管理制度
  • 做游戏代练的网站项目投资网
  • 个人备案做视频网站可信赖的网站建设案例
  • 在百度做网站赚钱吗百度网盟推广的投放工具
  • 手机网站二级域名变装小说 wordpress
  • 电商网站建设应用无锡有什么网站