CacheManager
缓存管理器里存放的是通知消息和聊天消息的缓存。因为用户会不在线,因此当用户不在线的时候,有朋友发消息或者群消息的时候,这些消息没办法传送给用户。因此先把消息传送到缓存器上,当用户上线的时候,会从缓存器上把缓存的消息推送给上线的用户。
代码
通知消息缓存
struct NotifyMsgCache { int32_t userid; std::string notifymsg; };
聊天消息缓存
struct ChatMsgCache { int32_t userid; std::string chatmsg; };
两个缓存都是用户id和string进行配对
缓存器
通知消息和聊天消息都是采用list来存放。
功能函数主要有添加通知消息缓存和获取通知消息缓存
添加聊天消息缓存和获取聊天消息缓存。
class MsgCacheManager final { public: MsgCacheManager(); ~MsgCacheManager(); MsgCacheManager(const MsgCacheManager& rhs) = delete; MsgCacheManager& operator =(const MsgCacheManager& rhs) = delete; bool AddNotifyMsgCache(int32_t userid, const std::string& cache); void GetNotifyMsgCache(int32_t userid, std::list<NotifyMsgCache>& cached); bool AddChatMsgCache(int32_t userid, const std::string& cache); void GetChatMsgCache(int32_t userid, std::list<ChatMsgCache>& cached); private: std::list<NotifyMsgCache> m_listNotifyMsgCache; //通知类消息缓存,比如加好友消息 std::mutex m_mtNotifyMsgCache; std::list<ChatMsgCache> m_listChatMsgCache; //聊天消息缓存 std::mutex m_mtChatMsgCache; };
具体代码如下:
添加缓存的思路:
把要缓存的用户id和缓存消息string转化成struct NotifyMsgCache或者struct ChatMsgCache,
string后面会加上这个缓存的长度。最后放入到list中。
获取缓存的思路:
根据用户id,在list中遍历查找,如果是用户id,就放入std::list<ChatMsgCache>& cached中。最后就可以在cached中拿到缓存
#include "../base/logging.h" #include "MsgCacheManager.h" MsgCacheManager::MsgCacheManager() { } MsgCacheManager::~MsgCacheManager() { } bool MsgCacheManager::AddNotifyMsgCache(int32_t userid, const std::string& cache) { std::lock_guard<std::mutex> guard(m_mtNotifyMsgCache); NotifyMsgCache nc; nc.userid = userid; nc.notifymsg.append(cache.c_str(), cache.length());; m_listNotifyMsgCache.push_back(nc); LOG_INFO << "append notify msg to cache, userid: " << userid << ", m_mapNotifyMsgCache.size() : " << m_listNotifyMsgCache.size() << ", cache length : " << cache.length(); //TODO: 存盘或写入数据库以防止程序崩溃丢失 return true; } void MsgCacheManager::GetNotifyMsgCache(int32_t userid, std::list<NotifyMsgCache>& cached) { std::lock_guard<std::mutex> guard(m_mtNotifyMsgCache); for (auto iter = m_listNotifyMsgCache.begin(); iter != m_listNotifyMsgCache.end(); ) { if (iter->userid == userid) { cached.push_back(*iter); iter = m_listNotifyMsgCache.erase(iter); } else { iter++; } } LOG_INFO << "get notify msg cache, userid: " << userid << ", m_mapNotifyMsgCache.size(): " << m_listNotifyMsgCache.size() << ", cached size: " << cached.size(); } bool MsgCacheManager::AddChatMsgCache(int32_t userid, const std::string& cache) { std::lock_guard<std::mutex> guard(m_mtChatMsgCache); ChatMsgCache c; c.userid = userid; c.chatmsg.append(cache.c_str(), cache.length()); m_listChatMsgCache.push_back(c); LOG_INFO << "append chat msg to cache, userid: " << userid << ", m_listChatMsgCache.size() : " << m_listChatMsgCache.size() << ", cache length : " << cache.length(); //TODO: 存盘或写入数据库以防止程序崩溃丢失 return true; } void MsgCacheManager::GetChatMsgCache(int32_t userid, std::list<ChatMsgCache>& cached) { std::lock_guard<std::mutex> guard(m_mtChatMsgCache); for (auto iter = m_listChatMsgCache.begin(); iter != m_listChatMsgCache.end(); ) { if (iter->userid == userid) { cached.push_back(*iter); iter = m_listChatMsgCache.erase(iter); } else { iter++; } } LOG_INFO << "get chat msg cache, no cache, userid: " << userid << ",m_listChatMsgCache.size(): " << m_listChatMsgCache.size() << ", cached size: " << cached.size(); }